-
Notifications
You must be signed in to change notification settings - Fork 12
/
CollectionHomeChildrenList.jsx
324 lines (307 loc) · 15.5 KB
/
CollectionHomeChildrenList.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
import React from 'react';
import alertifyjs from 'alertifyjs';
import {
includes, compact, isEmpty, get, merge, forEach, flatten, uniq, map, max, keys, filter
} from 'lodash';
import {
Dialog, DialogContent, DialogTitle, Divider, CircularProgress,
List, ListItem, FormControl, RadioGroup, FormControlLabel, Radio, Tooltip, Button
} from '@mui/material';
import { LocalOffer as LocalOfferIcon, Info as InfoIcon, AutoAwesome as BetaIcon } from '@mui/icons-material'
import { toFullAPIURL } from '../../common/utils';
import { DARKGRAY } from '../../common/constants';
import ResourceLabel from '../common/ResourceLabel';
import Search from '../search/Search';
import APIService from '../../services/APIService';
import BetaLabel from '../common/BetaLabel';
class CollectionHomeChildrenList extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedVersion: this.props.currentVersion || 'HEAD',
describeDelete: false,
selectedResources: [],
selectedResourceReferences: {},
actions: {},
allLoaded: false,
}
}
getURL() {
const { selectedVersion } = this.state;
const { versionedObjectURL, resource, expansions } = this.props;
let url = this.props.collectionURLs.version || this.props.collectionURLs.collection || versionedObjectURL
if(resource === 'references') {
if(this.props.collectionURLs?.collection)
url = this.props.collectionURLs.version || this.props.collectionURLs.collection
else if(selectedVersion)
url += selectedVersion + '/'
url += resource + '/'
return url
}
let expansionURL = this.props.collectionURLs?.expansion
if(expansionURL)
return `${expansionURL}${resource}/`
if(isEmpty(expansions)) {
if(selectedVersion && !includes(['HEAD', 'concepts', 'mappings', 'about', 'versions', 'references'], selectedVersion) && !url.endsWith(`${selectedVersion}/`))
url += `${selectedVersion}/`
url += `${resource}/`
return url
}
return `${versionedObjectURL}/${resource}/`
}
onExecute = () => {
const { actions, selectedResources, selectedResourceReferences } = this.state;
const deleteReferenceResourceIds = compact(map(actions, (action, uuid) => action === 'delete' ? uuid : null))
const deleteReferenceURIs = uniq(flatten(map(filter(selectedResources, resource => includes(deleteReferenceResourceIds, resource.uuid)), 'references')))
const referenceIds = map(deleteReferenceURIs, uri => selectedResourceReferences[uri].id)
const excludeReferenceResourceIds = compact(map(actions, (action, uuid) => action === 'exclude' ? uuid : null))
const excludeResources = filter(selectedResources, resource => includes(excludeReferenceResourceIds, resource.uuid))
const getURLs = entity => compact(map(excludeResources, resource => resource.type === entity ? resource.url : null))
this.deleteReferences(referenceIds, false)
this.excludeReferences({concepts: getURLs('Concept'), mappings: getURLs('Mapping'), exclude: true})
}
excludeReferences = data => {
APIService
.new()
.overrideURL(this.props.versionedObjectURL)
.appendToUrl('references/').put({data: data}).then(() => {
this.setState(
{describeDelete: false},
() => alertifyjs.success(`Successfully executed`, 1, () => window.location.reload())
)
})
}
deleteReferences = (referenceIds, alert = true) => {
if(!isEmpty(referenceIds)) {
APIService.new().overrideURL(this.props.versionedObjectURL).appendToUrl('references/').delete({ids: referenceIds}).then(response => {
if(alert) {
if(get(response, 'status') === 204)
alertifyjs.success(`Successfully delete references`, 1, () => window.location.reload())
else if(alert)
alertifyjs.error('Failed!')
}
})
}
}
onReferencesDelete = items => {
if(!isEmpty(items)) {
if(items[0].type === 'CollectionReference')
this.deleteReferences(compact(map(items, 'id')), true)
else {
this.setState({describeDelete: true, selectedResources: items}, () => {
const uniqRefs = uniq(compact(flatten(map(items, 'references'))))
let count = 0;
forEach(uniqRefs, referenceURI => {
APIService.new().overrideURL(referenceURI).get().then(response => {
const newState = {...this.state}
newState.selectedResourceReferences[referenceURI] = response.data
count += 1
newState.allLoaded = uniqRefs.length == count
this.setState(newState, () => {
if(this.state.allLoaded)
this.updateActions()
})
})
})
})
}
}
}
updateActions = () => {
const newState = {...this.state}
forEach(newState.selectedResources, resource => {
if(!get(newState.actions, resource.uuid))
newState.actions[resource.uuid] = this.shouldMakeExclusion(resource) ? 'exclude' : 'delete'
})
this.setState(newState)
}
onClose = () => this.setState({describeDelete: false, actions: {}})
shouldMakeExclusion = resource => {
if((get(resource.references, 'length') || 0) > 1)
return true
let total = 0;
forEach(resource.references, referenceURI => {
const reference = this.state.selectedResourceReferences[referenceURI]
if(reference) {
total += reference.concepts
total += reference.mappings
}
})
return total === 0 || total > 1; // total===0 is needed so that when the references are loading exclusion stays as recommended/selected option
}
onActionChange = (event, resource, value) => this.setState({actions: {...this.state.actions, [resource.uuid]: value}})
render() {
const { selectedVersion, describeDelete, selectedResources, selectedResourceReferences, allLoaded, actions } = this.state;
const { collection, resource, fixedFilters } = this.props;
const isVersionedObject = !selectedVersion || selectedVersion === 'HEAD'
const haveAllActions = keys(actions).length === selectedResources.length
return (
<React.Fragment>
<Search
{...this.props}
nested
baseURL={this.getURL()}
fixedFilters={merge({isTable: true, limit: 25}, (fixedFilters || {}))}
searchInputPlaceholder={`Search ${collection.name} ${resource}...`}
onReferencesDelete={isVersionedObject && this.onReferencesDelete}
isVersionedObject={isVersionedObject}
/>
{
describeDelete &&
<Dialog open onClose={this.onClose} maxWidth='md' fullWidth>
<DialogTitle>
<span style={{display: 'flex', alignItems: 'center', justifyContent: 'space-between'}}>
<span className='flex-vertical-center'>
<Tooltip arrow title='beta' placement='top'>
<BetaIcon style={{marginRight: '5px'}}/>
</Tooltip>
<BetaLabel label="Remove Resources by Reference" />
</span>
<span>
<Button onClick={this.onExecute} variant='contained' color='error' disabled={!haveAllActions}>
Execute
</Button>
</span>
</span>
</DialogTitle>
<DialogContent>
{
map(selectedResources, (resource, index) => {
const shouldExclude = this.shouldMakeExclusion(resource)
const recommendedOption = shouldExclude ? 'exclude' : 'delete'
const excludeLabel = 'Exclude concept(s)/mapping(s) from collection'
const deleteLabel = `Remove reference(s)`
const recommended = '(recommended)'
return (
<React.Fragment key={index}>
<div className='col-xs-12' style={{marginBottom: '5px', borderRadius: '5px', border: '1px solid rgba(0, 0, 0, 0.1)', padding: '5px 10px'}}>
<div className='col-xs-12 no-side-padding'>
<ResourceLabel
resource='concept'
owner={resource.owner}
parent={resource.source}
id={resource.id}
parentURL={resource.owner_url + 'sources/' + resource.source + '/'}
name={resource.display_name}
icon={
<LocalOfferIcon
fontSize='small'
style={{width: '10pt', color: DARKGRAY}}
/>
}
/>
</div>
<div className="col-xs-12" style={{paddingTop: '5px'}}>
{`This is a result of ${get(resource.references, 'length') || 0} reference(s):`}
</div>
<List dense style={{padding: '0 20px', display: 'inline-block'}}>
{
map(resource.references, uri => (
<React.Fragment key={`${index}-${uri}`}>
<ListItem>
<span style={{marginRight: '5px'}}>Reference:</span>
{
get(selectedResourceReferences, `${uri}.expression`) ?
<a href={toFullAPIURL(uri)} target='_blank' rel='noopener noreferrer'>
{selectedResourceReferences[uri].expression}
</a> :
<CircularProgress style={{marginLeft: '10px', width: '14px', height: '14px'}} />
}
</ListItem>
<ListItem style={{marginTop: '-8px'}}>
<span>
<span style={{marginRight: '5px'}}>Concepts:</span>
{
get(selectedResourceReferences, uri) ?
max([0, selectedResourceReferences[uri].concepts]) :
'-'
}
</span>
<Divider orientation='vertical' style={{height: '1rem', margin: '0 10px'}} />
<span>
<span style={{marginRight: '5px'}}>Mappings:</span>
{
get(selectedResourceReferences, uri) ?
max([0, selectedResourceReferences[uri].mappings]) :
'-'
}
</span>
</ListItem>
</React.Fragment>
))
}
</List>
{
allLoaded &&
<div className='col-xs-12'>
<FormControl style={{width: '100%'}}>
<RadioGroup
aria-labelledby="demo-radio-buttons-group-label"
name="reference-action"
onChange={(event, value) => this.onActionChange(event, resource, value)}
defaultValue={recommendedOption}
>
<FormControlLabel
style={{marginLeft: '0px', padding: '5px', background: shouldExclude ? 'none' : 'rgba(51, 115, 170, 0.15)', borderRadius: '5px'}}
value="delete"
control={
<Radio
style={{padding: '2px 5px 2px 2px'}}
sx={{
'& .MuiSvgIcon-root': {
fontSize:'0.9rem',
},
}}
/>
}
label={
<span className='flex-vertical-center' style={{fontSize: '0.9rem'}}>
<span>{deleteLabel}</span>
{ !shouldExclude && <span style={{marginLeft: '5px'}}><i>{recommended}</i></span> }
<Tooltip arrow placement='right' title="References will be removed if they are causing the selected resource(s) to appear in the collection. This cannot be undone, but the references can be added again in the References tab.">
<InfoIcon color='default' fontSize='small' style={{marginLeft: '5px'}} />
</Tooltip>
</span>
}
/>
<FormControlLabel
style={{marginLeft: '0px', padding: '5px', background: shouldExclude ? 'rgba(51, 115, 170, 0.15)' : 'none', borderRadius: '5px'}}
value="exclude"
control={
<Radio
style={{padding: '2px 5px 2px 2px'}}
sx={{
'& .MuiSvgIcon-root': {
fontSize:'0.9rem',
},
}}
/>
}
label={
<span className='flex-vertical-center' style={{fontSize: '0.9rem'}}>
<span>{excludeLabel}</span>
{ shouldExclude && <span style={{marginLeft: '5px'}}><i>{recommended}</i></span> }
<Tooltip arrow placement='right' title="Excluding a concept/mapping will ensure that it will not appear in the collection. This can be undone by deleting the exclusion reference(s) in the References tab.">
<InfoIcon color='default' fontSize='small' style={{marginLeft: '5px'}} />
</Tooltip>
</span>
}
/>
<div style={{marginLeft: '40px', fontSize: '12px'}}>Exclusion Expression: <b>{resource.url}</b></div>
</RadioGroup>
</FormControl>
</div>
}
</div>
</React.Fragment>
)
})
}
</DialogContent>
</Dialog>
}
</React.Fragment>
)
}
}
export default CollectionHomeChildrenList;