EntityDetail.jsx 4.64 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

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

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

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

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

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

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

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

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

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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 }`);
	},
});

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