/
MappingInlineForm.jsx
119 lines (111 loc) · 4.67 KB
/
MappingInlineForm.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
import React from 'react';
import { TextField, Button, Autocomplete } from '@mui/material';
import { fetchMapTypes, sortValuesBySourceSummary } from '../../common/utils';
import SourceSearchAutocomplete from '../common/SourceSearchAutocomplete';
import ConceptSearchAutocomplete from '../common/ConceptSearchAutocomplete';
import FormTooltip from '../common/FormTooltip';
import GroupHeader from '../common/GroupHeader';
import GroupItems from '../common/GroupItems';
const MappingInlineForm = ({defaultMapType, concept, onClose, onSubmit, isDirect, suggested, sourceVersionSummary}) => {
const [mapTypes, setMapTypes] = React.useState([])
const [mapType, setMapType] = React.useState(defaultMapType ? {id: defaultMapType, name: defaultMapType} : '')
const [source, setSource] = React.useState('')
const [targetConcept, setTargetConcept] = React.useState('')
React.useEffect(() => !defaultMapType && fetchMapTypes(data => setMapTypes(sortValuesBySourceSummary(data, sourceVersionSummary, 'mappings.map_type'))), [])
React.useEffect(() => setMapTypes(sortValuesBySourceSummary(mapTypes, sourceVersionSummary, 'mappings.map_type')), [sourceVersionSummary])
const onSourceChange = (id, item) => {
setSource(item || '')
setTargetConcept('')
}
const onConceptChange = (id, item) => setTargetConcept(item || '')
const getPayload = () => {
if(isDirect)
return {from_concept_url: concept.url, map_type: mapType.id, to_concept_url: targetConcept.url}
return {to_concept_url: concept.url, map_type: mapType.id, from_concept_url: targetConcept.url}
}
const _onSubmit = () => {
const form = document.getElementById('mapping-inline-form')
const isValid = form.reportValidity()
if(isValid) {
onSubmit(getPayload(), targetConcept, isDirect)
}
}
return (
<form id='mapping-inline-form'>
<div className='col-xs-12' style={{marginBottom: '15px', padding: defaultMapType ? '0 8px 0 0' : '0 8px'}}>
<h3 style={{margin: '15px 0 0 0', display: 'flex'}}>
Add a mapping
<FormTooltip
size='small'
title={`Create a new mapping that will be saved to ${concept.source}`}
style={{marginLeft: '10px'}}
/>
</h3>
{
!defaultMapType &&
<div className='col-xs-12 no-side-padding flex-vertical-center' style={{marginTop: '15px'}}>
<Autocomplete
openOnFocus
isOptionEqualToValue={(option, value) => option.id === value?.id}
value={mapType}
id="mapType"
size='small'
options={mapTypes}
groupBy={option => option.resultType}
renderGroup={params => (
<li style={{listStyle: 'none'}} key={params.group}>
<GroupHeader>{params.group}</GroupHeader>
<GroupItems>{params.children}</GroupItems>
</li>
)}
getOptionLabel={option => option.name || ''}
fullWidth
required={!defaultMapType}
renderInput={
params => <TextField
{...params}
required
label="Relationship Type"
variant="outlined"
fullWidth
/>
}
onChange={(event, item) => setMapType(item)}
/>
<FormTooltip
title="Defines the relationship between the two concepts. For example, “Concept A is the SAME-AS Concept B”, where Concept A is the “from concept” and Concept B is the “target concept”."
style={{marginLeft: '10px'}}
/>
</div>
}
<div className='col-xs-12 no-side-padding' style={{marginTop: '15px'}}>
<SourceSearchAutocomplete
onChange={onSourceChange}
required
size='small'
suggested={suggested}
/>
</div>
<div className='col-xs-12 no-side-padding' style={{marginTop: '15px'}}>
<ConceptSearchAutocomplete
onChange={onConceptChange}
parentURI={source.url}
required
size='small'
disabled={!source}
value={targetConcept}
/>
</div>
<div className='col-xs-12 no-side-padding' style={{marginTop: '15px'}}>
<Button color='primary' size='small' variant='contained' style={{marginRight: '16px'}} onClick={_onSubmit}>
Save
</Button>
<Button variant='text' size='small' color='secondary' onClick={onClose}>
Cancel
</Button>
</div>
</div>
</form>
)
}
export default MappingInlineForm;