EntityDetail.jsx 5.04 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,
Jaden's avatar
Jaden committed
12
13
} from 'reactstrap';

14
15
16
17
import { connect } from 'react-redux';

import cn from 'classnames';

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

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

26
import type { BeatObject } from '@helpers/beat';
27
import { getDefaultEntityObject } from '@helpers/beat';
28
import ValidSchemaBadge from './ValidSchemaBadge.jsx';
29

30
31
32
33
34
35
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
36
import PlotterEditorContainer from './plotter';
37

Jaden's avatar
Jaden committed
38
type Props = {
39
	match: any,
40
	history: any,
41
	getEntityObject: () => BeatObject,
42
	updateFunc: (BeatObject) => any,
Jaden's avatar
Jaden committed
43
44
};

45
type Tab = '0' | '1' | '2';
Jaden's avatar
Jaden committed
46

47
48
49
type State = {
	activeTab: Tab,
};
50

51
export class EntityDetail extends React.Component<Props, State> {
52
53
	constructor(props: Props){
		super(props);
54
55
	}

56
57
58
	state = {
		activeTab: '0',
	}
59

60
61
62
63
64
65
	switchToTab = (tab: Tab) => {
		this.setState({
			activeTab: tab
		});
	}

66
67
68
69
	saveChanges = (newObj: BeatObject) => {
		this.props.updateFunc(newObj);
	}

70
71
	render () {
		return (
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
			<Container>
				<Row>
					<Col>
						<h4 style={{'textAlign': 'center'}}>
							<span style={{'textTransform': 'capitalize'}}>
								{ this.props.match.params.entity }
							</span>
							{' '}
							<pre style={{display: 'inline'}}>
								{ this.props.getEntityObject().name }
							</pre>
							<ValidSchemaBadge entity={this.props.match.params.entity} obj={this.props.getEntityObject()} />
						</h4>
					</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')}
								>
									Documentation
								</NavLink>
							</NavItem>
							<NavItem>
								<NavLink
									className={cn({ active: this.state.activeTab === '2' })}
									onClick={() => this.switchToTab('2')}
								>
									Raw JSON
								</NavLink>
							</NavItem>
						</Nav>
115
						<TabContent activeTab={this.state.activeTab} className='mt-2'>
116
117
118
							<TabPane tabId='0'>
								{
									this.props.match.params.entity === 'algorithm' &&
119
										<AlgorithmEditorContainer
120
121
122
123
124
125
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
								{
									this.props.match.params.entity === 'dataformat' &&
126
										<DataformatEditorContainer
127
128
129
130
131
132
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
								{
									this.props.match.params.entity === 'library' &&
133
										<LibraryEditorContainer
134
135
136
137
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
138
139
								{
									this.props.match.params.entity === 'database' &&
140
										<DatabaseEditorContainer
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
141
142
143
144
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
145
146
147
148
149
150
151
								{
									this.props.match.params.entity === 'experiment' &&
										<ExperimentEditorContainer
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
152
153
154
155
156
157
158
								{
									this.props.match.params.entity === 'toolchain' &&
										<ToolchainEditorContainer
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
159
160
161
162
163
164
165
								{
									this.props.match.params.entity === 'plotter' &&
										<PlotterEditorContainer
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
166
167
							</TabPane>
							<TabPane tabId='1'>
168
169
								<Row>
									<Col>
170
										<h4 style={{textAlign: 'center'}}>Under construction 😄</h4>
171
172
									</Col>
								</Row>
173
174
175
176
177
178
179
180
							</TabPane>
							<TabPane tabId='2'>
								<pre>{ JSON.stringify(this.props.getEntityObject(), null, 4) }</pre>
							</TabPane>
						</TabContent>
					</Col>
				</Row>
			</Container>
181
182
183
184
		);
	}
}

185
const mapStateToProps = (state, ownProps) => {
186
187
188
	const entity = ownProps.match.params.entity;
	const name = ownProps.match.params.name;
	const obj = {
189
		getEntityObject: (): BeatObject => Selectors[`${ entity }Get`](state).find(o => o.name === name) || getDefaultEntityObject(),
190
191
192
193
194
	};

	return obj;
};

195
const mapDispatchToProps = (dispatch, ownProps) => ({
196
197
198
199
200
201
	updateFunc: (obj) => {
		dispatch(Actions[`${ ownProps.match.params.entity }Update`](ownProps.match.params.name, obj));
		ownProps.history.push(`/${ ownProps.match.params.entity }/${ obj.name }`);
	},
});

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