EntityDetail.jsx 3.9 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';
33

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

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

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

47
48
49
50
51
52
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: {}},
	};
53

54
55
	return obj;
};
56

57
58
59
60
61
62
63
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 }`);
	},
});

64
65
66
class EntityDetail extends React.Component<Props, State> {
	constructor(props: Props){
		super(props);
67
68
	}

69
70
71
	state = {
		activeTab: '0',
	}
72

73
74
75
76
77
78
	switchToTab = (tab: Tab) => {
		this.setState({
			activeTab: tab
		});
	}

79
80
81
82
	saveChanges = (newObj: BeatObject) => {
		this.props.updateFunc(newObj);
	}

83
84
	render () {
		return (
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
			<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>
						<TabContent activeTab={this.state.activeTab}>
							<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}
										/>
								}
							</TabPane>
							<TabPane tabId='1'>
							</TabPane>
							<TabPane tabId='2'>
								<pre>{ JSON.stringify(this.props.getEntityObject(), null, 4) }</pre>
							</TabPane>
						</TabContent>
					</Col>
				</Row>
			</Container>
161
162
163
164
		);
	}
}

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