EntityDetail.jsx 4.91 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
18
import { connect } from 'react-redux';
import type { MapStateToProps, MapDispatchToProps } from 'react-redux';

import cn from 'classnames';

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

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

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

30
31
32
33
import DataformatEditorContainer from './DataformatEditor.jsx';
import AlgorithmEditorContainer from './AlgorithmEditor.jsx';
import LibraryEditorContainer from './LibraryEditor.jsx';
import DatabaseEditorContainer from './DatabaseEditor.jsx';
34
import ExperimentEditorContainer from './ExperimentEditor.jsx';
35
import ToolchainEditorContainer from './ToolchainEditor.jsx';
36

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

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

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

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

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

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

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

69
70
	render () {
		return (
71
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
			<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>
114
						<TabContent activeTab={this.state.activeTab} className='mt-2'>
115
116
117
							<TabPane tabId='0'>
								{
									this.props.match.params.entity === 'algorithm' &&
118
										<AlgorithmEditorContainer
119
120
121
122
123
124
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
								{
									this.props.match.params.entity === 'dataformat' &&
125
										<DataformatEditorContainer
126
127
128
129
130
131
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
								{
									this.props.match.params.entity === 'library' &&
132
										<LibraryEditorContainer
133
134
135
136
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
137
138
								{
									this.props.match.params.entity === 'database' &&
139
										<DatabaseEditorContainer
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
140
141
142
143
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
144
145
146
147
148
149
150
								{
									this.props.match.params.entity === 'experiment' &&
										<ExperimentEditorContainer
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
151
152
153
154
155
156
157
								{
									this.props.match.params.entity === 'toolchain' &&
										<ToolchainEditorContainer
											data={this.props.getEntityObject()}
											saveFunc={this.saveChanges}
										/>
								}
158
159
							</TabPane>
							<TabPane tabId='1'>
160
161
162
163
164
								<Row>
									<Col>
										<h4 style={{textAlign: 'center'}}>Under construction :)</h4>
									</Col>
								</Row>
165
166
167
168
169
170
171
172
							</TabPane>
							<TabPane tabId='2'>
								<pre>{ JSON.stringify(this.props.getEntityObject(), null, 4) }</pre>
							</TabPane>
						</TabContent>
					</Col>
				</Row>
			</Container>
173
174
175
176
		);
	}
}

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const mapStateToProps: MapStateToProps<*,*,*> = (state, ownProps) => {
	const entity = ownProps.match.params.entity;
	const name = ownProps.match.params.name;
	const obj = {
		getEntityObject: (): BeatObject => Selectors[`${ entity }Get`](state).find(o => o.name === name) || {name: '', contents: {}},
	};

	return obj;
};

const mapDispatchToProps: MapDispatchToProps<*,*,*> = (dispatch, ownProps) => ({
	updateFunc: (obj) => {
		dispatch(Actions[`${ ownProps.match.params.entity }Update`](ownProps.match.params.name, obj));
		ownProps.history.push(`/${ ownProps.match.params.entity }/${ obj.name }`);
	},
});

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