-
Notifications
You must be signed in to change notification settings - Fork 12
/
MappingInlineForm.jsx
157 lines (147 loc) · 6.06 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
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
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('')
const [targetConceptCode, setTargetConceptCode] = React.useState('')
const [targetConceptName, setTargetConceptName] = 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 onConceptCodeChange = (id, val) => setTargetConceptCode(val || '')
const shouldShowTargetConceptName = () => Boolean(targetConcept?.url ? targetConcept.id !== targetConceptCode : targetConceptCode)
const getPayload = () => {
let payload = {}
const unknownConcept = shouldShowTargetConceptName()
if(isDirect) {
payload = {from_concept_url: concept.url, map_type: mapType.id}
if(unknownConcept) {
payload['to_source_url'] = source.url
payload['to_concept_code'] = targetConceptCode
payload['to_concept_name'] = targetConceptName
} else {
payload['to_concept_url'] = targetConcept?.url
}
} else {
payload = {to_concept_url: concept.url, map_type: mapType.id}
if(unknownConcept) {
payload['from_source_url'] = source.url
payload['from_concept_code'] = targetConceptCode
payload['from_concept_name'] = targetConceptName
} else {
payload['from_concept_url'] = targetConcept?.url
}
}
return payload
}
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' style={{display: 'inline-block'}}>
<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 || 'none'}>
<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}
onInputChange={onConceptCodeChange}
parentURI={source.url}
required
size='small'
disabled={!source}
value={targetConcept}
freeSolo
/>
</div>
{
shouldShowTargetConceptName() &&
<div className='col-xs-12 no-side-padding' style={{marginTop: '15px'}}>
<TextField
onChange={event => setTargetConceptName(event.target.value || '')}
size='small'
fullWidth
label='Name'
/>
</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;