PlotterEditor.jsx 6.82 KB
Newer Older
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// @flow
import * as React from 'react';
import {
	Row,
	Col,
	Button,
	Form, FormGroup,
	Label, Input,
	Card, CardHeader, CardBody,
	TabContent, TabPane, Nav, NavItem, NavLink,
	Alert,
	UncontrolledDropdown, DropdownToggle, DropdownMenu,
	InputGroup, InputGroupAddon,
} from 'reactstrap';

import { connect } from 'react-redux';

import cn from 'classnames';

20
import { changeObjFieldName, generateNewKey, copyObj } from '@helpers';
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
21
22
23
24
25
import { BUILTIN_TYPES, ANALYZER_RESULT_TYPES, getValidPlotterObj as getValidObj } from '@helpers/beat';
import type { BeatObject } from '@helpers/beat';

import * as Selectors from '@store/selectors.js';

26
import * as Actions from '@store/actions.js';
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
27
28
29
30
31
32
import CacheInput from '../CacheInput.jsx';
import ValidSchemaBadge from '../ValidSchemaBadge.jsx';
import DeleteInputBtn from '../DeleteInputBtn.jsx';
import TypedField from '../TypedField.jsx';
import TemplateButton from '../EntityTemplateGenerationButton.jsx';
import InfoTooltip from '../InfoTooltip.jsx';
33
import ParameterCreate from '../ParameterCreate.jsx';
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
34
35
36
37
38
39

type Props = {
	data: BeatObject,
	libraries: BeatObject[],
	plotDfNames: string[],
	saveFunc: (BeatObject) => any,
40
	updateFunc: (BeatObject) => any,
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
};

type State = {
	choiceCache: string,
};

export class PlotterEditor extends React.Component<Props, State> {
	constructor(props: Props) {
		//console.log(`Creating AlgDetail`);
		super(props);
	}

	state = {
		choiceCache: '',
	}

	setContents = (newContents: any) => {
58
59
60
61
62
		this.props.updateFunc({
			...this.props.data,
			contents: {
				...this.props.data.contents,
				...newContents,
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
			}
		});
	}

	updateDescription = (desc: string) => {
		this.setContents({ description: desc });
	}

	updateDataformat = (df: string) => {
		this.setContents({ dataformat: df });
	}

	// 4 different functions:
	// creates a parameter (provide name & value)
	// updates a parameter (provide name & value)
	// renames a parameter (provide name & value & the old name)
	// deletes a parameter (provide null as name, value is optional, the name)
	updateParameter = (name: null | string, value: any, oldName: ?string) => {
81
		const params = copyObj(this.props.data.contents.parameters);
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
82
83
84
85
86
87
88
		if(oldName)
			delete params[oldName];
		if(name !== null)
			params[name] = value;
		this.setContents({ parameters: params });
	}

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
161
162
163
	// helper to change a value in the "contents" subobject of a plotter
	// (this is where the vast majority of change happens)
	changeContentsVal = (field: string, val: any) => {
		const newCache = {
			...this.props.data,
			contents: {
				...this.props.data.contents,
				[field]: val
			}
		};

		this.props.updateFunc(newCache);
	}


	renderLibraries = () => (
		<TabPane tabId='2'>
			<Row>
				<Col sm='12'>
					{
						// loop through all the used libs
						(Object.entries(this.props.data.contents.uses): [string, any][])
						.map(([name, lib], i, lEntries) => (
							<TypedField
								key={i}
								name={name}
								type={lib}
								types={this.props.libraries.map(l => l.name)}
								existingFields={lEntries.map(([n, v]) => n)}
								nameUpdateFunc={str => {
									// update the alias
									this.changeContentsVal('uses',
										changeObjFieldName(
											this.props.data.contents.uses,
											name,
											str
										)
									);
								}}
								typeUpdateFunc={str => {
									// update the chosen library for the alias
									const libs = {
										...this.props.data.contents.uses,
										[name]: str
									};
									this.changeContentsVal('uses', libs);
								}}
								deleteFunc={() => {
									const libs = { ...this.props.data.contents.uses };
									delete libs[name];
									this.changeContentsVal('uses', libs);
								}}
							/>
						))
					}
				</Col>
			</Row>
			<Button outline block
				id='newLibraryBtn'
				onClick={() => {
					const newKey = generateNewKey('library', Object.keys(this.props.data.contents.uses));
					this.changeContentsVal('uses',
						{
							...this.props.data.contents.uses,
							[newKey]: ''
						}
					);
				}}
			>
				New Library
			</Button>
		</TabPane>
	);


Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
164
165
166
167
168
169
170
171
	render = () => {
		return (
			<div>
				<div className='d-flex'>
					<Button
						className='mx-auto'
						outline
						color='secondary'
172
						onClick={() => this.props.saveFunc(this.props.data)}
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
173
					>
174
						Save Changes (Changes are <ValidSchemaBadge entity='plotter' obj={this.props.data} />)
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
175
176
					</Button>
					<TemplateButton
177
						data={this.props.data}
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
178
179
180
181
182
183
184
185
186
187
188
						entity={'plotter'}
					/>
				</div>
				<Form>
					<FormGroup tag='fieldset'>
						<FormGroup>
							<Label>Short Description</Label>
							<Input
								type='text'
								id='plDesc'
								placeholder='Plotter description...'
189
								value={this.props.data.contents.description}
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
190
191
192
193
194
195
196
197
198
								onChange={(e) => this.updateDescription(e.target.value)}
							/>
						</FormGroup>
						<FormGroup>
							<Label>Input Dataformat</Label>
							<Input
								id='plDf'
								type='select'
								className='custom-select'
199
								value={this.props.data.contents.dataformat}
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
200
201
202
203
204
205
206
207
208
								onChange={e => this.updateDataformat(e.target.value)}
							>
								{
									this.props.plotDfNames.map((df, i) =>
										<option key={i} value={df}>{ df }</option>
									)
								}
							</Input>
						</FormGroup>
209
210
211
212
						<FormGroup>
							<h5>Libraries</h5>
								{ this.renderLibraries() }
						</FormGroup>
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
213
214
215
						<FormGroup>
							<h5>Parameters</h5>
							{
216
								(Object.entries(this.props.data.contents.parameters): [string, any][]).map(([name, param], i, params) => (
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
217
218
									<Row key={i} className='mb-2'>
										<Col sm='12'>
219
											<ParameterCreate
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
220
												name={name}
221
222
223
224
												param={param}
												params={params}
												updateParameter={this.updateParameter}
											/>
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
225
226
227
228
229
230
231
										</Col>
									</Row>
								))
							}
							<Button outline block
								id='newParameterBtn'
								onClick={() => {
232
									const newKey = generateNewKey('parameter', Object.keys(this.props.data.contents.parameters || {}));
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
									this.updateParameter(
										newKey,
										{
											type: '',
											default: '',
											description: '',
										}
									);
								}}
							>
								New Parameter
							</Button>
						</FormGroup>
					</FormGroup>
				</Form>
			</div>
		);
	}
}

const mapStateToProps = (state, ownProps) => {
254
	const plotters = Selectors.plotterGet(state);
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
255
256
257
	const obj = {
		libraries: Selectors.libraryGet(state),
		plotDfNames: Selectors.dataformatGet(state).map(df => df.name).filter(df => df.startsWith('plot/')),
258
		data: plotters[ownProps.index] || getValidObj(),
Jaden DIEFENBAUGH's avatar
Jaden DIEFENBAUGH committed
259
260
261
262
	};
	return obj;
};

263
264
265
266
267
268
269
270
271
const mapDispatchToProps = (dispatch, ownProps) => ({
	// replace the obj in the Redux store with the new object
	updateFunc: (obj) => {
		console.log(`dispatching for ${ obj.name }`);
		dispatch(Actions[`plotterUpdate`](obj.name, obj));
	},
});

export default connect(mapStateToProps, mapDispatchToProps)(PlotterEditor);