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

Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
30
31
import DataformatEditor from './DataformatEditor.jsx';
import AlgorithmEditor from './AlgorithmEditor.jsx';
32
import LibraryEditor from './LibraryEditor.jsx';
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
33
import DatabaseEditor from './DatabaseEditor.jsx';
34

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

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

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

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

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

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

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

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

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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 }`);
	},
});

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