-
Notifications
You must be signed in to change notification settings - Fork 13
/
MappingForm.jsx
601 lines (570 loc) · 23 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
import React from 'react';
import alertifyjs from 'alertifyjs';
import Autocomplete from '@mui/material/Autocomplete';
import { TextField, Button, CircularProgress, IconButton, Tooltip } from '@mui/material';
import {
Add as AddIcon,
SwapVert as SwapIcon,
} from '@mui/icons-material';
import {
set, get, cloneDeep, isEmpty, pickBy, pullAt, map
} from 'lodash';
import APIService from '../../services/APIService';
import { arrayToObject, fetchMapTypes } from '../../common/utils';
import ExtrasForm from '../common/ExtrasForm';
import OwnerParentSelection from '../common/OwnerParentSelection';
const EXTRAS_MODEL = {key: '', value: ''}
const ANCHOR_UNDERLINE_STYLES = {textDecoration: 'underline', cursor: 'pointer'}
class MappingForm extends React.Component {
constructor(props) {
super(props);
this.state = {
manualMnemonic: false,
manualExternalId: false,
fieldErrors: {},
mapTypes: [],
selected_map_type: null,
parent: 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: '',
extras: [],
sort_weight: ''
}
}
}
componentDidMount() {
fetchMapTypes(data => this.setState({mapTypes: data}))
if((this.props.edit && this.props.mapping) || this.props.copyFrom)
this.setFieldsForEdit()
if(this.props.selectedConcepts)
this.setFieldsFromSelectedConcepts()
if(!this.props.edit)
this.setState({parent: this.props.source})
}
setFieldsForEdit() {
const { mapping, edit, copyFrom } = this.props;
const instance = edit ? mapping : copyFrom
const attrs = [
'id', 'map_type', 'external_id',
'from_concept_url', 'from_concept_name',
'from_source_url', 'from_source_version',
'to_concept_url', 'to_concept_name',
'to_source_url', 'to_source_version', 'sort_weight'
]
const newState = {...this.state}
attrs.forEach(attr => set(newState.fields, attr, get(instance, attr, '') || ''))
newState.fields.to_concept_code = instance.to_concept_code ? decodeURIComponent(instance.to_concept_code) : instance.to_concept_code
newState.fields.from_concept_code = instance.from_concept_code ? decodeURIComponent(instance.from_concept_code) : instance.from_concept_code
if(!edit)
newState.fields.id = ''
newState.selected_map_type = {id: instance.map_type, name: instance.map_type}
newState.fields.extras = isEmpty(instance.extras) ? newState.fields.extras : map(instance.extras, (v, k) => ({key: k, value: v}))
this.setState(newState);
}
setFieldsFromSelectedConcepts() {
const { selectedConcepts } = this.props;
const fromConcept = get(selectedConcepts, '0')
const toConcept = get(selectedConcepts, '1')
const newState = {...this.state}
if(fromConcept) {
newState.fields.from_concept_url = fromConcept.url
newState.fields.from_concept_name = fromConcept.display_name
}
if(toConcept) {
newState.fields.to_concept_url = toConcept.url
newState.fields.to_concept_name = toConcept.display_name
}
this.setState(newState)
}
swapConcepts = () => {
const fields = cloneDeep(this.state.fields)
const newState = {...this.state}
newState.fields.from_concept_url = fields.to_concept_url
newState.fields.from_concept_code = fields.to_concept_code
newState.fields.from_concept_name = fields.to_concept_name
newState.fields.from_source_url = fields.to_source_url
newState.fields.from_source_version = fields.to_source_version
newState.fields.to_concept_url = fields.from_concept_url
newState.fields.to_concept_code = fields.from_concept_code
newState.fields.to_concept_name = fields.from_concept_name
newState.fields.to_source_url = fields.from_source_url
newState.fields.to_source_version = fields.from_source_version
this.setState(newState)
}
getIdHelperText() {
const { parent, fields } = this.state
const { edit, source } = this.props
const id = fields.id || "[generated-mapping-id]"
const parentURL = edit ? this.props.parentURL : get(parent, 'url');
return (
<span>
{
source.autoid_mapping_mnemonic &&
<React.Fragment>
<a onClick={this.toggleManualMnemonic} style={ANCHOR_UNDERLINE_STYLES}>Auto-assign</a><br/>
</React.Fragment>
}
{
source.autoid_mapping_mnemonic === 'sequential' &&
<React.Fragment>
<span>This is optional since the parent repository is set can to take care of generating the ID. The ID will be generated to next in sequence.</span><br/>
</React.Fragment>
}
{
source.autoid_mapping_mnemonic === 'uuid' &&
<React.Fragment>
<span>This is optional since the parent repository is set to take care of generating the ID. The ID will be generated in UUID format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.</span><br/>
</React.Fragment>
}
<span>Alphanumeric characters, @, hyphens, periods, and underscores are allowed.</span>
<br />
{
parentURL &&
<span>
<span>Your new mapping will live at: <br />
{ `${window.location.origin}/#${parentURL}mappings/` }
</span>
<span><b>{id}</b>/</span>
</span>
}
</span>
)
}
getExternalIdHelperText() {
const { source } = this.props
if(!source)
return ''
return (
<span>
{
source.autoid_mapping_external_id &&
<React.Fragment>
<a onClick={this.toggleManualExternalId} style={ANCHOR_UNDERLINE_STYLES}>Auto-assign</a><br/>
</React.Fragment>
}
{
source.autoid_mapping_external_id === 'sequence' &&
<span>This is optional since the parent repository is set to take care of generating the External ID. The External ID will be generated to next in sequence.</span>
}
{
source.autoid_mapping_external_id === 'uuid' &&
<span>This is optional since the parent repository is set to take care of generating the External ID. The External ID will be generated in UUID format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.</span>
}
{
!source.autoid_mapping_external_id && ''
}
</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 { parent } = this.state;
const { edit } = this.props
const parentURL = edit ? this.props.parentURL : get(parent, 'url');
let fields = cloneDeep(this.state.fields);
const form = document.getElementsByTagName('form')[0];
form.reportValidity()
const isFormValid = form.checkValidity()
if(parentURL && isFormValid) {
fields.extras = arrayToObject(fields.extras)
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))
} 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('<br />'))
} else {
this.setState(
{fieldErrors: response || {}},
() => alertifyjs.error('Please fill mandatory fields.')
)
}
}
}
onAddExtras = () => this.setFieldValue(
'fields.extras', [...this.state.fields.extras, cloneDeep(EXTRAS_MODEL)]
)
onDeleteExtras = index => {
const newState = {...this.state}
pullAt(newState.fields.extras, index)
this.setState(newState)
}
onExtrasChange = (index, key, value) => {
const newState = {...this.state}
if(key !== '__')
newState.fields.extras[index].key = key
if(value !== '__')
newState.fields.extras[index].value = value
this.setState(newState)
}
onParentChange = newParent => this.setState({parent: newParent})
getOwner = () => {
const { mapping, source, edit } = this.props;
if(edit) {
return {type: mapping.owner_type.toLowerCase(), id: mapping.owner, name: mapping.owner}
}
return {type: source.owner_type.toLowerCase(), id: source.owner, name: source.owner}
}
getParent = () => {
const { edit, mapping } = this.props;
if(edit)
return {id: mapping.source, name: mapping.source}
return this.state.parent
}
toggleManualMnemonic = () => {
const newManualMnemonic = !this.state.manualMnemonic
const newState = {...this.state}
if(!newManualMnemonic)
newState.fields.id = ''
newState.manualMnemonic = newManualMnemonic
this.setState(newState)
}
toggleManualExternalId = () => {
const newManualExternalId = !this.state.manualExternalId
const newState = {...this.state}
if(!newManualExternalId)
newState.fields.external_id = ''
newState.manualExternalId = newManualExternalId
this.setState(newState)
}
render() {
const { fields, fieldErrors, selected_map_type, mapTypes, manualMnemonic, manualExternalId } = this.state;
const { onCancel, edit, source } = 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>
<OwnerParentSelection
selectedOwner={this.getOwner()}
selectedParent={this.getParent()}
requiredParent
requiredOwner
onChange={this.onParentChange}
disabled={edit}
/>
{
!edit &&
<div style={{marginTop: '15px', width: '100%'}}>
{
get(source, 'autoid_mapping_mnemonic') && !manualMnemonic ?
<span style={{fontWeight: '500', borderLeft: '3px solid lightgray', padding: '10px 5px'}}>
Mapping ID will be auto-assigned (<a style={ANCHOR_UNDERLINE_STYLES} onClick={this.toggleManualMnemonic}>click here</a> to override with manual entry)
</span> :
<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}
inputProps={{ pattern: "[a-zA-Z0-9-._@]+" }}
/>
}
</div>
}
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<Autocomplete
openOnFocus
isOptionEqualToValue={(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%'}}>
{
get(source, 'autoid_mapping_external_id') && !manualExternalId && !edit ?
<span style={{fontWeight: '500', borderLeft: '3px solid lightgray', padding: '10px 5px'}}>
Mapping External ID will be auto-assigned (<a style={ANCHOR_UNDERLINE_STYLES} onClick={this.toggleManualExternalId}>click here</a> to override with manual entry)
</span> :
<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}
helperText={edit ? undefined : this.getExternalIdHelperText()}
/>
}
</div>
<div className='col-md-12 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<TextField
error={Boolean(fieldErrors.sort_weight)}
id="fields.sort_weight"
label="Sort Weight"
placeholder="e.g. 1.0"
variant="outlined"
fullWidth
onChange={this.onTextFieldChange}
value={fields.sort_weight}
inputProps={{ pattern: "[0-9.]+", min: 0, step: 0.00001 }}
type='number'
/>
</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%', textAlign: 'center'}}>
<Tooltip arrow title="Swap From and To Concepts">
<IconButton color="primary" onClick={this.swapConcepts} size="large">
<SwapIcon />
</IconButton>
</Tooltip>
</div>
<div className='col-md-12 no-side-padding' style={{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 no-side-padding' style={{marginTop: '15px', width: '100%'}}>
<div className='col-md-8'>
<h3>Custom Attributes</h3>
</div>
<div className='col-md-4' style={{textAlign: 'right'}}>
<IconButton color='primary' onClick={this.onAddExtras} size="large">
<AddIcon />
</IconButton>
</div>
{
map(fields.extras, (extra, index) => (
<div className='col-md-12 no-side-padding' key={index} style={index > 0 ? {marginTop: '5px', width: '100%'} : {width: '100%'}}>
<ExtrasForm
extra={extra}
index={index}
onChange={this.onExtrasChange}
onDelete={this.onDeleteExtras}
/>
</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;