-
Notifications
You must be signed in to change notification settings - Fork 12
/
MappingForm.jsx
378 lines (362 loc) · 13.7 KB
/
MappingForm.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import React from 'react';
import alertifyjs from 'alertifyjs';
import Autocomplete from '@material-ui/lab/Autocomplete';
import { TextField, Button, CircularProgress } from '@material-ui/core';
import {
set, get, map, orderBy, cloneDeep, isEmpty, pickBy
} from 'lodash';
import APIService from '../../services/APIService';
import { getCurrentURL } from '../../common/utils';
class MappingForm extends React.Component {
constructor(props) {
super(props);
this.state = {
fieldErrors: {},
mapTypes: [],
selected_map_type: null,
fields: {
id: '',
map_type: '',
external_id: '',
from_concept_url: '',
from_concept_code: '',
from_concept_name: '',
from_source_url: '',
from_source_version: '',
to_concept_url: '',
to_concept_code: '',
to_concept_name: '',
to_source_url: '',
to_source_version: '',
comment: '',
}
}
}
componentDidMount() {
this.fetchMapTypes();
if(this.props.edit && this.props.mapping)
this.setFieldsForEdit()
}
fetchMapTypes() {
APIService.sources('MapTypes').concepts()
.get(null, null, {limit: 1000})
.then(response => this.setState({
mapTypes: orderBy(map(response.data, mt => ({id: mt.id, name: mt.id})), 'name')
}))
}
setFieldsForEdit() {
const { mapping } = this.props;
const attrs = [
'id', 'map_type', 'external_id',
'from_concept_url', 'from_concept_code', 'from_concept_name',
'from_source_url', 'from_source_version',
'to_concept_url', 'to_concept_code', 'to_concept_name',
'to_source_url', 'to_source_version',
]
const newState = {...this.state}
newState.selected_map_type = {id: mapping.map_type, name: mapping.map_type}
attrs.forEach(attr => set(newState.fields, attr, get(mapping, attr, '') || ''))
this.setState(newState);
}
getIdHelperText() {
const id = this.state.fields.id || "[generated-mapping-id]"
return (
<span>
<span>Alphanumeric characters, hyphens, periods, and underscores are allowed.</span>
<br />
<span>
<span>Your new mapping will live at: <br />
{
`${getCurrentURL()}/mappings/`
}
</span>
<span><b>{id}</b>/</span>
</span>
</span>
)
}
onTextFieldChange = event => {
this.setFieldValue(event.target.id, event.target.value)
}
onAutoCompleteChange = (id, item) => {
this.setFieldValue(id, get(item, 'id', ''), true)
}
setFieldValue(id, value, setObject=false) {
const newState = {...this.state}
set(newState, id, value)
const fieldName = get(id.split('fields.'), '1')
if(fieldName && !isEmpty(value) && get(newState.fieldErrors, fieldName))
newState.fieldErrors[fieldName] = null
if(setObject)
newState[`selected_${fieldName}`] = {id: value, name: value}
this.setState(newState)
}
onSubmit = event => {
event.preventDefault();
event.stopPropagation();
const { parentURL, edit } = this.props
let fields = cloneDeep(this.state.fields);
const form = document.getElementsByTagName('form')[0];
form.reportValidity()
const isFormValid = form.checkValidity()
if(parentURL && isFormValid) {
if(edit)
fields.update_comment = fields.comment
fields = pickBy(fields, value => value)
let service = APIService.new().overrideURL(parentURL)
if(edit) {
service.put(fields).then(response => this.handleSubmitResponse(response))
return false;
} else {
service.appendToUrl('mappings/').post(fields).then(response => this.handleSubmitResponse(response))
}
}
}
handleSubmitResponse(response) {
const { edit, reloadOnSuccess, onCancel } = this.props
if(response.status === 201 || response.status === 200) { // success
const verb = edit ? 'updated' : 'created'
const successMsg = `Successfully ${verb} mapping`;
const message = reloadOnSuccess ? successMsg + '. Reloading..' : successMsg;
onCancel();
alertifyjs.success(message, 1, () => {
if(reloadOnSuccess)
window.location.reload()
})
} else { // error
const genericError = get(response, '__all__')
if(genericError) {
alertifyjs.error(genericError.join('\n'))
} else {
this.setState(
{fieldErrors: response || {}},
() => alertifyjs.error('Please fill mandatory fields.')
)
}
}
}
render() {
const { fields, fieldErrors, selected_map_type, mapTypes } = this.state;
const { onCancel, edit } = this.props;
const isLoading = isEmpty(mapTypes);
const header = edit ? `Edit Mapping: ${fields.id}` : 'New Mapping'
return (
<div className='col-md-12' style={{marginBottom: '30px'}}>
<div className='col-md-12 no-side-padding'>
<h2>{header}</h2>
</div>
{
isLoading ?
<div style={{width: '100%', textAlign: 'center', marginTop: '100px'}}>
<CircularProgress />
</div>:
<div className='col-md-12 no-side-padding'>
<form>
{
!edit &&
<div className='col-md-12 no-side-padding'>
<TextField
error={Boolean(fieldErrors.id)}
id="fields.id"
label="Mapping ID"
placeholder="e.g. A15.0"
helperText={this.getIdHelperText()}
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.id}
disabled={edit}
/>
</div>
}
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<Autocomplete
getOptionSelected={(option, value) => option.id === get(value, 'id')}
value={selected_map_type}
id="fields.map_type"
options={mapTypes}
getOptionLabel={(option) => option.name}
fullWidth
required
renderInput={
params => <TextField
{...params}
error={Boolean(fieldErrors.map_type)}
required
label="Map Type"
variant="outlined"
fullWidth
/>
}
onChange={(event, item) => this.onAutoCompleteChange('fields.map_type', item)}
/>
</div>
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<TextField
id="fields.external_id"
label="External ID"
placeholder="e.g. UUID from external system"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.external_id}
/>
</div>
{
edit &&
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<TextField
id="fields.comment"
label="Update Comment"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.comment}
required
/>
</div>
}
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<h3 className='divider'>
<span className='text'>From Concept</span>
</h3>
</div>
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<div className='col-md-8 no-left-padding'>
<TextField
id="fields.from_source_url"
label="From Source URL"
placeholder="e.g. /orgs/MyOrg/sources/MySource/"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.from_source_url}
/>
</div>
<div className='col-md-4 no-side-padding'>
<TextField
id="fields.from_source_version"
label="From Source Version"
placeholder="e.g. 2.46"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.from_source_version}
/>
</div>
</div>
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<TextField
id="fields.from_concept_url"
label="From Concept URL"
placeholder="e.g. /orgs/MyOrg/sources/MySource/concepts/1234/"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.from_concept_url}
/>
</div>
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<div className='col-md-8 no-left-padding'>
<TextField
id="fields.from_concept_name"
label="From Concept Name"
placeholder="e.g. A14"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.from_concept_name}
/>
</div>
<div className='col-md-4 no-side-padding'>
<TextField
id="fields.from_concept_code"
label="From Concept Code"
placeholder="e.g. 1234"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.from_concept_code}
/>
</div>
</div>
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<h3 className='divider'>
<span className='text'>To Concept</span>
</h3>
</div>
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<div className='col-md-8 no-left-padding'>
<TextField
id="fields.to_source_url"
label="To Source URL"
placeholder="e.g. /orgs/IHTSDO/sources/SNOMED-CT/"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.to_source_url}
/>
</div>
<div className='col-md-4 no-side-padding'>
<TextField
id="fields.to_source_version"
label="To Source Version"
placeholder="e.g. v1.2"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.to_source_version}
/>
</div>
</div>
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<TextField
id="fields.to_concept_url"
label="To Concept URL"
placeholder="e.g. /orgs/CIEL/sources/CIEL/concepts/32/"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.to_concept_url}
/>
</div>
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<div className='col-md-8 no-left-padding'>
<TextField
id="fields.to_concept_name"
label="To Concept Name"
placeholder="e.g. Tuberculosis of lung, confirmed by culture only"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.to_concept_name}
/>
</div>
<div className='col-md-4 no-side-padding'>
<TextField
id="fields.to_concept_code"
label="To Concept Code"
placeholder="e.g. A15.1"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.to_concept_code}
/>
</div>
</div>
<div className='col-md-12' style={{textAlign: 'center', margin: '20px 0'}}>
<Button style={{margin: '0 10px'}} color='primary' variant='outlined' type='submit' onClick={this.onSubmit}>
{edit ? 'Update' : 'Create'}
</Button>
<Button style={{margin: '0 10px'}} variant='outlined' onClick={onCancel}>
Cancel
</Button>
</div>
</form>
</div>
}
</div>
)
}
}
export default MappingForm;