EntityDetail.jsx 6.35 KB
Newer Older
Jaden's avatar
Jaden committed
1
// @flow
2
import * as React from 'react';
Jaden's avatar
Jaden committed
3
4
5
6
import {
	Container,
	Row,
	Col,
7
8
9
10
11
	TabContent,
	TabPane,
	Nav,
	NavItem,
	NavLink,
12
13
14
	Input,
	InputGroupAddon,
	InputGroup,
Jaden's avatar
Jaden committed
15
16
} from 'reactstrap';

17
18
19
20
import { connect } from 'react-redux';

import cn from 'classnames';

21
import * as Actions from '@store/actions.js';
22
import * as Selectors from '@store/selectors.js';
Jaden's avatar
Jaden committed
23
24
25
26
27
28

import {
	Route,
	Link
} from 'react-router-dom';

29
import type { BeatObject, BeatEntity } from '@helpers/beat';
30
import { getDefaultEntityObject, pluralize } from '@helpers/beat';
31
import { genModuleApiFuncs } from '@helpers/api';
32
import ValidSchemaBadge from './ValidSchemaBadge.jsx';
33

34
35
36
37
38
39
import DataformatEditorContainer from './dataformat';
import AlgorithmEditorContainer from './algorithm';
import LibraryEditorContainer from './library';
import DatabaseEditorContainer from './database';
import ExperimentEditorContainer from './experiment';
import ToolchainEditorContainer from './toolchain';
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
40
import PlotterEditorContainer from './plotter';
41
import PlotterparameterEditorContainer from './plotterparameter';
42

Jaden's avatar
Jaden committed
43
type Props = {
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
44
45
	// the match object from react-router
	// used to find which object to render
46
	match: any,
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
47
48
	// the history object from react-router
	// used to push new history to
49
	history: any,
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
50
	// gets the object based on the current URL
51
	getEntityObject: () => BeatObject,
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
52
	// updates the current object
53
	updateFunc: (BeatObject) => any,
54
55
	// the current BEAT entity being show
	entity: BeatEntity,
56
57
	// the absolute path to the user's prefix folder
	prefix: string,
Jaden's avatar
Jaden committed
58
59
};

Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
60
// 3 tabs so far: editor, docs, raw json
61
type Tab = '0' | '1' | '2';
Jaden's avatar
Jaden committed
62

63
type State = {
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
64
	// which tab is active
65
66
	activeTab: Tab,
};
67

Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
68
69
// wrapper for editors
// doesnt really do much now besides the raw json tab
70
export class EntityDetail extends React.Component<Props, State> {
71
72
	constructor(props: Props){
		super(props);
73
74
	}

75
76
77
	state = {
		activeTab: '0',
	}
78

79
80
81
82
83
84
	switchToTab = (tab: Tab) => {
		this.setState({
			activeTab: tab
		});
	}

85
86
	saveChanges = (newObj: BeatObject) => {
		this.props.updateFunc(newObj);
87
88
		const put = genModuleApiFuncs(this.props.entity).put;
		return put([newObj]);
89
90
	}

91
	render () {
92
		const name = this.props.match.params.name;
93
		return (
94
			<Container>
95
				<Row className='mb-1'>
96
					<Col>
97
98
						{/* title line (name & validation info) */}
						<h4 className='text-center'>
99
							<span style={{'textTransform': 'capitalize'}}>
100
								{ this.props.entity }
101
102
103
							</span>
							{' '}
							<pre style={{display: 'inline'}}>
104
								{ name }
105
							</pre>
106
							<ValidSchemaBadge entity={this.props.entity} obj={this.props.getEntityObject()} />
107
						</h4>
108
109
110
111
112
113
114
115
116
						{/* path line */}
						<InputGroup>
							<InputGroupAddon addonType='prepend'>Path:</InputGroupAddon>
							<Input
								readOnly
								width='80'
								value={`${ this.props.prefix }/${ pluralize(this.props.entity) }/${ name }`}
							/>
						</InputGroup>
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
					</Col>
				</Row>
				<Row>
					<Col sm='12'>
						<Nav tabs className='nav-fill'>
							<NavItem>
								<NavLink
									className={cn({ active: this.state.activeTab === '0' })}
									onClick={() => this.switchToTab('0')}
								>
									Editor
								</NavLink>
							</NavItem>
							<NavItem>
								<NavLink
									className={cn({ active: this.state.activeTab === '1' })}
									onClick={() => this.switchToTab('1')}
								>
									Raw JSON
								</NavLink>
							</NavItem>
						</Nav>
139
						<TabContent activeTab={this.state.activeTab} className='mt-2'>
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
140
							{/* Uses the appropriate editor, could probably be done nicer */}
141
142
							<TabPane tabId='0'>
								{
143
									this.props.entity === 'algorithm' &&
144
										<AlgorithmEditorContainer
145
146
147
148
149
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
								{
150
									this.props.entity === 'dataformat' &&
151
										<DataformatEditorContainer
152
153
154
155
156
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
								{
157
									this.props.entity === 'library' &&
158
										<LibraryEditorContainer
159
160
161
162
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
163
								{
164
									this.props.entity === 'database' &&
165
										<DatabaseEditorContainer
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
166
167
168
169
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
170
								{
171
									this.props.entity === 'experiment' &&
172
173
174
175
176
										<ExperimentEditorContainer
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
177
								{
178
									this.props.entity === 'toolchain' &&
179
180
181
182
183
										<ToolchainEditorContainer
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
184
								{
185
									this.props.entity === 'plotter' &&
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
186
187
188
189
190
										<PlotterEditorContainer
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
191
								{
192
									this.props.entity === 'plotterparameter' &&
193
194
195
196
197
										<PlotterparameterEditorContainer
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
198
199
200
201
202
203
204
205
							</TabPane>
							<TabPane tabId='1'>
								<pre>{ JSON.stringify(this.props.getEntityObject(), null, 4) }</pre>
							</TabPane>
						</TabContent>
					</Col>
				</Row>
			</Container>
206
207
208
209
		);
	}
}

210
const mapStateToProps = (state, ownProps) => {
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
211
	// which beat entity is currently active: 'algorithm', 'plotter', 'database', etc.
212
	const entity = ownProps.match.params.entity;
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
213
	// the name of the object ("atnt/1", "plot/isoroc/1", "user/tc/test/1/exp", etc.)
214
215
	const name = ownProps.match.params.name;
	const obj = {
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
216
217
		// uses a selector based off the entity and finds the obj given the name
		// if the obj doesnt exist (huge edge case) just return a default obj to not break everything
218
		getEntityObject: (): BeatObject => Selectors[`${ entity }Get`](state).find(o => o.name === name) || getDefaultEntityObject(),
219
		entity,
220
		prefix: Selectors.settingsGet(state).prefix,
221
222
223
224
225
	};

	return obj;
};

226
const mapDispatchToProps = (dispatch, ownProps) => ({
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
227
	// replace the obj in the Redux store with the new object
228
229
230
231
232
233
	updateFunc: (obj) => {
		dispatch(Actions[`${ ownProps.match.params.entity }Update`](ownProps.match.params.name, obj));
		ownProps.history.push(`/${ ownProps.match.params.entity }/${ obj.name }`);
	},
});

234
export default connect(mapStateToProps, mapDispatchToProps)(EntityDetail);