-
Notifications
You must be signed in to change notification settings - Fork 12
/
AddToCollection.jsx
328 lines (308 loc) · 12.9 KB
/
AddToCollection.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
import React from 'react';
import alertifyjs from 'alertifyjs';
import {
Button, Popper, MenuList, Grow, Paper, ClickAwayListener, Tooltip,
CircularProgress, Dialog, DialogActions, DialogContent,
TextField, InputAdornment, Chip, Divider
} from '@mui/material'
import {
ArrowDropDown as ArrowDropDownIcon,
Loyalty as LoyaltyIcon,
Search as SearchIcon,
} from '@mui/icons-material'
import { map, isEmpty, get, filter, cloneDeep, compact, uniq } from 'lodash';
import APIService from '../../services/APIService';
import { getCurrentUserCollections, getCurrentUser } from '../../common/utils';
import CommonFormDrawer from '../common/CommonFormDrawer';
import CollectionForm from '../collections/CollectionForm';
import AddReferencesResult from './AddReferencesResult';
import ReferenceCascadeDialog from './ReferenceCascadeDialog';
import MappingReferenceAddOptionsDialog from './MappingReferenceAddOptionsDialog';
import DialogTitleWithCloseButton from './DialogTitleWithCloseButton';
import CollectionListItem from './CollectionListItem';
const NEW_COLLECTION = {id: '__new__', name: 'Create New Collection'}
class AddToCollection extends React.Component {
constructor(props) {
super(props);
this.state = {
open: false,
lockedCollection: null,
selectedCollection: null,
isLoading: true,
isAdding: false,
allCollections: [],
collections: [],
cascadeMappings: false,
cascadeToConcepts: false,
cascadeMethod: false,
result: false,
collectionForm: false,
addMappings: true,
addToConcepts: false,
addFromConcepts: false,
}
this.anchorRef = React.createRef(null);
}
toggleCollectionForm = () => this.setState({collectionForm: !this.state.collectionForm})
componentDidUpdate(prevProps, prevState) {
if(this.state.open && !prevState.open && isEmpty(this.state.collections))
this.fetchCollections()
}
fetchCollections() {
getCurrentUserCollections(collections => {
this.setState({
collections: [...this.state.collections, ...collections],
allCollections: [...this.state.collections, ...collections],
isLoading: false,
})}
)
}
handleClose = event => {
if (this.anchorRef.current && this.anchorRef.current.contains(event.target)) {
return;
}
this.toggleOpen()
};
toggleOpen = () => this.setState({open: !this.state.open})
handleDialogClose = () => this.setState({selectedCollection: null})
onCheckboxChange = event => this.setState({[event.target.name]: event.target.checked})
handleMenuItemClick = (event, collection) => {
if(get(collection, 'id') === '__new__')
this.toggleCollectionForm()
else
this.setState({selectedCollection: collection}, () => this.handleClose(event))
}
getPayload = (cascadeToConcepts, cascadeMappings, cascadeMethod) => {
const { addMappings, addToConcepts, addFromConcepts } = this.state
const { references } = this.props
const isMapping = Boolean(get(references, '0.map_type'))
let expressions = map(references, 'url')
let queryParams = {}
let cascadePayload = {}
if(isMapping) {
if(addMappings)
expressions = map(references, 'url')
if(addToConcepts)
expressions = compact([...expressions, ...map(references, 'to_concept_url')])
if(addFromConcepts)
expressions = compact([...expressions, ...map(references, 'from_concept_url')])
expressions = uniq(expressions)
} else {
expressions = map(references, 'url')
if(cascadeToConcepts)
queryParams = {cascade: 'sourcetoconcepts'}
else if(cascadeMappings)
queryParams = {cascade: 'sourcemappings'}
else if(cascadeMethod) {
cascadePayload = {method: 'sourcetoconcepts', cascade_levels: '*', map_types: 'Q-AND-A,CONCEPT-SET', return_map_types: '*'}
queryParams = {transformReferences: 'resourceVersions'}
}
}
let payload = {payload: {data: {expressions: expressions}}, queryParams: queryParams}
if(!isEmpty(cascadePayload))
payload.payload.cascade = cascadePayload
return payload
}
handleAdd = () => {
const { selectedCollection, cascadeMappings, cascadeToConcepts, cascadeMethod } = this.state
const payload = this.getPayload(cascadeToConcepts, cascadeMappings, cascadeMethod)
if(isEmpty(payload?.payload?.data?.expressions)) {
alertifyjs.error('No expressions to add')
} else {
this.setState({isAdding: true}, () => {
this._collectionName = this.getCollectionName()
APIService.new().overrideURL(selectedCollection.url)
.appendToUrl('references/')
.put(payload.payload, null, null, payload.queryParams)
.then(response => {
this.setState({isAdding: false}, () => {
if(response.status === 200) {
this.setState({result: response.data})
alertifyjs.success('Successfully added reference(s)')
this.handleDialogClose()
} else {
alertifyjs.error('Something bad happened')
this.handleDialogClose()
}
})
})
})
}
}
afterNewCollectionAdd = newCollection => this.setState({
selectedCollection: newCollection,
allCollections: [...this.state.allCollections, newCollection]
})
getFilteredCollections(value) {
return filter(this.state.allCollections, collection => {
const name = collection.short_code + '/' + collection.owner;
return name.toLowerCase().match(value.trim())
})
}
onSearchValueChange = event => this.setState({
searchedValue: event.target.value,
collections: event.target.value.trim() ?
this.getFilteredCollections(event.target.value) :
this.state.allCollections
})
getButton = () => {
const { iconButton, ...rest } = this.props;
if(iconButton)
return (
<Tooltip arrow title='Add to Collection'>
<Button ref={this.anchorRef} onClick={this.toggleOpen} {...rest} color='secondary'>
<LoyaltyIcon fontSize='inherit' />
</Button>
</Tooltip>
)
return (
<Chip className='selected-control-chip' ref={this.anchorRef} onClick={this.toggleOpen} icon={<LoyaltyIcon />} deleteIcon={<ArrowDropDownIcon />} color='secondary' {...rest} onDelete={this.toggleOpen} label='Add to Collection' />
)
}
getCollectionName = () => this.state.selectedCollection ? `${this.state.selectedCollection.owner}/${this.state.selectedCollection.short_code}` : ''
render() {
const {
open, allCollections, collections, selectedCollection, isAdding, isLoading, searchedValue, collectionForm, result
} = this.state;
const { references } = this.props
const openDialog = Boolean(selectedCollection)
const collectionName = openDialog ? this.getCollectionName() : '';
const _collections = [...collections, cloneDeep(NEW_COLLECTION)]
const noOverallCollections = !isLoading && allCollections.length === 0;
const noSearchResults = !isLoading && searchedValue && collections.length === 0;
const button = this.getButton();
const isMappingReferences = Boolean(get(references, '0.map_type'))
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}>
{
noOverallCollections ?
<p style={{padding: '20px'}}>
You do not have any collections.
<a style={{cursor: 'pointer', marginLeft: '2px'}} onClick={this.toggleCollectionForm}>
Create New Collection?
</a>
</p> :
<MenuList variant='menu' id="split-button-menu" style={{maxWidth: '500px', maxHeight: '100%', overflow: 'auto'}}>
<TextField
id='collection-search-input'
placeholder='Search Collection...'
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.
<a style={{cursor: 'pointer', marginLeft: '2px'}} onClick={this.toggleCollectionForm}>
Create New Collection?
</a>
</p> :
map(_collections, (collection, index) => (
<React.Fragment key={index}>
<CollectionListItem
option={collection}
listItemProps={{onClick: event => this.handleMenuItemClick(event, collection)}}
style={{cursor: 'pointer'}}
/>
<Divider variant="inset" component="li" style={{listStyle: 'none'}} />
</React.Fragment>
))
)
}
</MenuList>
}
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
<Dialog open={openDialog} onClose={this.handleDialogClose}>
<DialogTitleWithCloseButton onClose={this.handleDialogClose}>
Add References to Collection
</DialogTitleWithCloseButton>
{
isAdding ?
<DialogContent style={{textAlign: 'center', margin: '50px'}}><CircularProgress /></DialogContent> :
(
isMappingReferences ?
<MappingReferenceAddOptionsDialog references={references} onChange={states => this.setState({addMappings: states.addMappings, addToConcepts: states.addToConcepts, addFromConcepts: states.addFromConcepts})} collectionName={collectionName} /> :
<ReferenceCascadeDialog
references={references}
onCascadeChange={states => this.setState({cascadeToConcepts: states.cascadeToConcepts, cascadeMappings: states.cascadeMappings, cascadeMethod: states.cascadeMethod})}
collectionName={collectionName}
collection={selectedCollection}
noCascadePayloadFunc={() => this.getPayload()}
cascadeMappingsFunc={() => this.getPayload(false, true, false)}
cascadeToConceptsFunc={() => this.getPayload(true, false, false)}
cascadeOpenMRSFunc={() => this.getPayload(false, false, true)}
/>
)
}
<DialogActions>
<React.Fragment>
<Button onClick={this.handleDialogClose} color="secondary" disabled={isAdding}>
Cancel
</Button>
<Button onClick={this.handleAdd} color="primary" disabled={isAdding}>
Add
</Button>
</React.Fragment>
</DialogActions>
</Dialog>
{
result &&
<AddReferencesResult
title={`Results: Add Reference(s) to ${this._collectionName}`}
open={Boolean(result)}
onClose={() => this.setState({result: false})}
result={result}
/>
}
<CommonFormDrawer
style={{zIndex: 1202}}
isOpen={collectionForm}
onClose={this.toggleCollectionForm}
size='smedium'
formComponent={
<CollectionForm
anonymous
newCollectionProps={{
name: (searchedValue || '').trim()
}}
onCancel={this.toggleCollectionForm}
owner={getCurrentUser()}
onSuccess={this.afterNewCollectionAdd}
/>
}
/>
</React.Fragment>
)
}
}
export default AddToCollection;