-
Notifications
You must be signed in to change notification settings - Fork 12
/
CloneToSource.jsx
339 lines (318 loc) · 13.3 KB
/
CloneToSource.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
import React from 'react';
import alertifyjs from 'alertifyjs';
import {
Button, Popper, List, Grow, Paper, ClickAwayListener, Tooltip,
CircularProgress, Dialog, DialogActions, DialogContent, DialogContentText,
TextField, InputAdornment, Chip, Divider, Collapse
} from '@mui/material'
import {
ArrowDropDown as ArrowDropDownIcon,
ArrowDropUp as ArrowDropUpIcon,
List as SourceIcon,
Search as SearchIcon,
} from '@mui/icons-material'
import { map, isEmpty, filter, omit, get } from 'lodash';
import APIService from '../../services/APIService';
import { getCurrentUserSources } from '../../common/utils';
import { DEFAULT_CASCADE_PARAMS } from '../../common/constants';
import DialogTitleWithCloseButton from './DialogTitleWithCloseButton';
import SourceListItem from './SourceListItem';
import CascadeParametersForm from './CascadeParametersForm';
import ConceptTable from '../concepts/ConceptTable';
import APIPreview from './APIPreview'
import ConceptDisplayName from '../concepts/ConceptDisplayName';
class CloneToSource extends React.Component {
constructor(props) {
super(props);
this.state = {
open: false,
lockedSource: null,
selectedSource: null,
isLoading: true,
isAdding: false,
allSources: [],
sources: [],
result: false,
advancedSettings: false,
params: {
...omit(DEFAULT_CASCADE_PARAMS, ['method', 'cascadeHierarchy', 'cascadeMappings', 'includeRetired', 'reverse', 'view', 'omitIfExistsIn']),
equivalencyMapType: 'SAME-AS',
mapTypes: 'Q-AND-A,CONCEPT-SET'
}
}
this.anchorRef = React.createRef(null);
}
toggleSettings = () => this.setState({advancedSettings: !this.state.advancedSettings})
componentDidUpdate(prevProps, prevState) {
if(this.state.open && !prevState.open && isEmpty(this.state.sources))
this.fetchSources()
}
fetchSources() {
getCurrentUserSources(sources => {
this.setState({
sources: [...this.state.sources, ...sources],
allSources: [...this.state.sources, ...sources],
isLoading: false,
})})
}
handleClose = event => {
if (this.anchorRef.current && this.anchorRef.current.contains(event.target)) {
return;
}
this.toggleOpen()
};
toggleOpen = () => this.setState({open: !this.state.open, result: !this.state.open ? false : this.state.result})
handleDialogClose = () => this.setState({selectedSource: null, result: false})
onCheckboxChange = event => this.setState({[event.target.name]: event.target.checked})
handleMenuItemClick = (event, source) => {
this.setState({selectedSource: source}, () => this.handleClose(event))
}
getPayload = () => {
const { params } = this.state
const { references } = this.props
return {
expressions: map(references, 'url'),
parameters: params
}
}
getReferences = () => {
const { references } = this.props
const { result } = this.state
if(result && references) {
return map(references, concept => {
const response = get(result, concept.url)
let res = {...concept}
if(response) {
res.status = response.status
res.total = response?.bundle?.total || 0
res.bundle = response?.bundle
}
return res
})
}
return references
}
handleAdd = () => {
const { selectedSource } = this.state
const payload = this.getPayload()
if(isEmpty(payload?.expressions)) {
alertifyjs.error('No expressions to add')
} else {
this.setState({isAdding: true}, () => {
this._sourceName = this.getSourceName()
APIService.new().overrideURL(selectedSource.url)
.appendToUrl('concepts/$clone/')
.post(payload)
.then(response => {
this.setState({isAdding: false, result: response?.data}, () => {
if(response.status === 200) {
alertifyjs.success('Successfully cloned concept(s)')
} else {
alertifyjs.error('Something bad happened')
}
})
})
})
}
}
afterNewSourceAdd = newSource => this.setState({
selectedSource: newSource,
allSources: [...this.state.allSources, newSource]
})
getFilteredSources(value) {
return filter(this.state.allSources, source => {
const name = source.short_code + '/' + source.owner;
return name.toLowerCase().match(value.trim())
})
}
onSearchValueChange = event => this.setState({
searchedValue: event.target.value,
sources: event.target.value.trim() ?
this.getFilteredSources(event.target.value) :
this.state.allSources
})
getButton = () => {
const { iconButton, ...rest } = this.props;
if(iconButton)
return (
<Tooltip arrow title='Clone into source'>
<Button ref={this.anchorRef} onClick={this.toggleOpen} {...rest} color='secondary'>
<SourceIcon fontSize='inherit' />
</Button>
</Tooltip>
)
return (
<Chip className='selected-control-chip' ref={this.anchorRef} onClick={this.toggleOpen} icon={<SourceIcon />} deleteIcon={<ArrowDropDownIcon />} color='secondary' {...rest} onDelete={this.toggleOpen} label='Clone (beta)' />
)
}
getSourceName = () => this.state.selectedSource ? `${this.state.selectedSource.owner}/${this.state.selectedSource.short_code}` : ''
render() {
const {
open, sources, selectedSource, isAdding, isLoading, searchedValue, result
} = this.state;
const openDialog = Boolean(selectedSource)
const _sources = [...sources]
const noSearchResults = !isLoading && searchedValue && sources.length === 0;
const button = this.getButton();
const requestURL = selectedSource?.url ? `${selectedSource.url}concepts/$clone/` : null
const concepts = this.getReferences()
return (
<React.Fragment>
{button}
<Popper open={open} anchorEl={this.anchorRef.current} transition style={{zIndex: 1300}}>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
style={{
transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom',
zIndex: '1300'
}}
>
<Paper>
<ClickAwayListener onClickAway={this.handleClose}>
{
<List variant='menu' id="split-button-menu" style={{maxWidth: '500px', maxHeight: '100%', overflow: 'auto'}}>
<TextField
id='source-search-input'
placeholder='Select Source to clone into...'
variant='outlined'
size='small'
style={{padding: '10px', width: '100%'}}
autoFocus
onChange={this.onSearchValueChange}
InputProps={{
endAdornment: (
<InputAdornment position="end">
<SearchIcon />
</InputAdornment>
)
}}
/>
{
isLoading ?
<div style={{textAlign: 'center'}}>
<CircularProgress />
</div> : (
noSearchResults ?
<p style={{padding: '0 20px'}}>
No Matches.
</p> :
<List variant='menu' id="split-button-menu" style={{maxWidth: '500px', maxHeight: '300px', overflow: 'auto'}}>
{
map(_sources, (source, index) => (
<React.Fragment key={index}>
<SourceListItem
option={source}
listItemProps={{onClick: event => this.handleMenuItemClick(event, source)}}
style={{cursor: 'pointer'}}
/>
<Divider variant="inset" component="li" style={{listStyle: 'none'}} />
</React.Fragment>
))
}
</List>
)
}
</List>
}
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
<Dialog open={openDialog} onClose={this.handleDialogClose} scroll='paper' fullWidth maxWidth='md' disableEscapeKeyDown={isAdding}>
<DialogTitleWithCloseButton disaled={isAdding} onClose={this.handleDialogClose}>
{`Clone Concept(s) to Source: ${this.getSourceName()}`}
</DialogTitleWithCloseButton>
<DialogContent>
<DialogContentText>
{'This operation will clone the selected concept, plus its mappings and target concepts, recursively following specified mappings. This will skip any concepts that would be duplicated based on the concepts in your Source that already have an equivalent mapping of type "SAME-AS". This equivalence map type can be changed in the Advanced Settings.'}
</DialogContentText>
<div className='col-xs-12 no-side-padding' style={{marginTop: '10px'}}>
<Button
size='small'
variant='text'
endIcon={
this.state.advancedSettings ?
<ArrowDropUpIcon fontSize='inherit' /> :
<ArrowDropDownIcon fontSize='inherit' />
}
style={{textTransform: 'none', marginLeft: '-4px'}}
onClick={this.toggleSettings}>
Advanced Settings
</Button>
<Collapse in={this.state.advancedSettings} timeout="auto" unmountOnExit>
{
this.state.advancedSettings &&
<div className='col-xs-12 no-side-padding' style={{margin: '15px 0'}}>
<CascadeParametersForm
fieldClasses='col-xs-4'
hiddenFields={['method', 'cascadeHierarchy', 'omitIfExistsIn']}
defaultParams={{...this.state.params, omitIfExistsIn: selectedSource?.url}}
onChange={_params => this.setState({params: _params})}
/>
</div>
}
</Collapse>
</div>
<div className='col-xs-12 no-side-padding' style={{marginTop: '10px'}}>
<DialogContentText>
{`${concepts.length} selected Concept(s) will be cloned into: `} <b>{this.getSourceName()}</b>
</DialogContentText>
</div>
<div className='col-xs-12 no-side-padding' style={{marginTop: '10px'}}>
<ConceptTable concepts={concepts} showProgress={isAdding} showStatus={isAdding || result} visualFilters={this.state.params} />
</div>
<div className='col-xs-12 no-side-padding' style={{marginTop: '10px'}}>
<APIPreview url={requestURL} payload={this.getPayload()} verb='POST' />
</div>
{
result &&
<div className='col-xs-12 no-side-padding' style={{marginTop: '10px'}}>
<DialogContentText>
Results:
</DialogContentText>
{
map(concepts, concept => {
const entries = concept?.bundle?.entry || []
let conceptsAdded = filter(entries, {type: 'Concept'}).length
const mappingsAdded = filter(entries, {type: 'Mapping'}).length
return (
<React.Fragment key={concept.url}>
<DialogContentText>
<ConceptDisplayName concept={concept} />
<span style={{marginLeft: '15px'}}>
<span style={{marginLeft: '15px'}}>
Concepts: {conceptsAdded}
</span>
<span style={{marginLeft: '15px'}}>
Mappings: {mappingsAdded}
</span>
</span>
</DialogContentText>
</React.Fragment>
)
})
}
</div>
}
</DialogContent>
<DialogActions>
<React.Fragment>
<Button onClick={this.handleDialogClose} color="secondary" disabled={isAdding}>
Close
</Button>
{
!result &&
<Button onClick={this.handleAdd} color="primary" disabled={isAdding}>
Add
</Button>
}
</React.Fragment>
</DialogActions>
</Dialog>
</React.Fragment>
)
}
}
export default CloneToSource;