/
Breadcrumbs.jsx
324 lines (313 loc) · 14.8 KB
/
Breadcrumbs.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 {
CancelOutlined as CancelIcon,
ChevronRight as SeparatorIcon,
Search as SearchIcon,
} from '@mui/icons-material';
import { IconButton } from '@mui/material';
import { isEmpty, get, startCase } from 'lodash';
import { currentUserHasAccess } from '../../common/utils';
import { WHITE, ORANGE, GREEN, BLUE } from '../../common/constants';
import APIService from '../../services/APIService';
import OwnerButton from '../common/OwnerButton';
import SourceButton from '../common/SourceButton';
import CollectionButton from '../common/CollectionButton';
import ExpansionSelectorButton from '../common/ExpansionSelectorButton';
import ConceptButton from '../common/ConceptButton';
import MappingButton from '../common/MappingButton';
import ResourceVersionButton from '../common/VersionButton';
import CommonFormDrawer from '../common/CommonFormDrawer';
import SourceForm from './SourceForm';
import CollectionForm from '../collections/CollectionForm';
import ConceptContainerDelete from '../common/ConceptContainerDelete';
import VersionSelectorButton from '../common/VersionSelectorButton';
import ManageSourceChildButton from '../common/ManageSourceChildButton';
import ConceptForm from '../concepts/ConceptForm';
import MappingForm from '../mappings/MappingForm';
import { OperationsContext } from '../app/LayoutContext';
const Breadcrumbs = ({
params, selectedResource, container, isVersionedObject, versions, onSplitViewClose,
isLoadingExpansions, expansions, expansion
}) => {
const { openOperations, menuOpen } = React.useContext(OperationsContext);
const [conceptForm, setConceptForm] = React.useState(false);
const [mappingForm, setMappingForm] = React.useState(false);
const [deleteDialog, setDeleteDialog] = React.useState(false);
const [sourceForm, setSourceForm] = React.useState(false);
const [collectionForm, setCollectionForm] = React.useState(false);
const containerLabel = params.source ? 'Source' : 'Collection'
const owner = params.org ? params.org : params.user
const ownerType = params.org ? 'org' : 'user'
const parent = params.source || params.collection
const parentType = params.source ? 'source' : 'collection'
const parentVersion = params.version || 'HEAD'
const resource = params.concept || params.mapping
const resourceType = params.concept ? 'concept' : (params.mapping ? 'mapping' : null)
const resourceVersion = params.conceptVersion || params.mappingVersion
const ownerURL = (params.org || params.user) ? `#/${ownerType}s/${owner}/` : null;
const parentURL = `${ownerURL}${parentType}s/${parent}/`;
const parentVersionURL = parentVersion === 'HEAD' ? parentURL : `${parentURL}${parentVersion}/`;
const resourceURL = `${parentVersionURL}${resourceType}s/${resource}/`
const resourceVersionURL = `${resourceURL}${resourceVersion}/`
const unselectedParentProps = {variant: 'outlined', style: {borderColor: GREEN, color: GREEN, boxShadow: 'none', textTransform: 'none', border: '1px solid', background: params.search ? '#f1f1f1' : WHITE}}
const parentProps = (parentVersion === 'HEAD' && !resource) ? {} : unselectedParentProps;
const parentVersionProps = (expansion || (resource && selectedResource)) ? {...unselectedParentProps, className: ''} : {}
const expansionProps = (resource && selectedResource) ? {...unselectedParentProps, className: ''} : {}
const resourceProps = resourceVersion ? {variant: 'outlined', style: {borderColor: BLUE, color: BLUE, background: '#f1f1f1', border: '1px solid'}} : {}
const downloadFileName = container ? (isVersionedObject ? `${container.type}-${container.short_code}` : `${container.type}-${container.short_code}-${container.id}`) : '';
const hasAccess = currentUserHasAccess();
const deleteContainer = () => {
APIService.new().overrideURL(container.url).delete().then(response => {
if(get(response, 'status') === 204)
alertifyjs.success(`${containerLabel} Deleted`, 1, () => window.location.hash = container.owner_url)
else if(get(response, 'status') === 202)
alertifyjs.success(`${containerLabel} Delete Accepted. This may take few minutes.`)
else if(get(response, 'status') === 400)
alertifyjs.error(get(response, 'data.detail', `${containerLabel} Delete Failed`))
else
alertifyjs.error('Something bad happened!')
})
}
const onRetire = () => {
const prompt = alertifyjs.prompt()
prompt.setContent('<form id="retireForm"> <p>Retire Reason</p> <textarea required id="comment" style="width: 100%;"></textarea> </form>')
prompt.set('onok', () => {
document.getElementById('retireForm').reportValidity();
const comment = document.getElementById('comment').value
if(!comment)
return false
retire(comment)
})
prompt.set('title', 'Retire Concept')
prompt.show()
}
const onUnretire = () => {
const prompt = alertifyjs
.prompt()
prompt.setContent('<form id="retireForm"> <p>Unretire Reason</p> <textarea required id="comment" style="width: 100%;"></textarea> </form>')
.set('onok', () => {
document.getElementById('retireForm').reportValidity();
const comment = document.getElementById('comment').value
if(!comment)
return false
unretire(comment)
})
.set('title', 'Unretire Concept')
.show()
}
const retire = comment => {
APIService.new().overrideURL(resourceURL.replace('#', '')).delete({comment: comment}).then(response => {
if(get(response, 'status') === 204)
alertifyjs.success(`${startCase(resource)} Retired`, 1, () => window.location.reload())
else
alertifyjs.error('Something bad happened!')
})
}
const unretire = comment => {
APIService.new().overrideURL(resourceURL.replace('#', '')).appendToUrl('reactivate/').put({comment: comment}).then(response => {
if(get(response, 'status') === 204)
alertifyjs.success(`${startCase(resource)} Unretired`, 1, () => window.location.reload())
else
alertifyjs.error('Something bad happened!')
})
}
const getWidth = () => {
if(!menuOpen && !openOperations)
return '100%'
let width = 0;
if(menuOpen)
width += 190
if(openOperations)
width += 350
return `calc(100% - ${width}px)`
}
const getOwner = () => {
let owner = {url: container.owner_url, type: container.owner_type}
if(owner.type === 'User')
owner.username = container.owner
else
owner.id = container.owner
return owner
}
return (
<header className='col-xs-12 no-side-padding'>
<div className='col-xs-12 no-side-padding container'>
<div className='col-xs-12 no-side-padding'>
<div className='col-xs-12 no-side-padding flex-vertical-center'>
<span className='flex-vertical-center' style={{width: getWidth()}}>
{
params.search &&
<span className='search-breadcurmbs flex-vertical-center' style={{background: WHITE, minHeight: '60px', paddingRight: '15px', minWidth: '125px'}}>
<span className='flex-vertical-center' style={{fontWeight: 'bold', paddingLeft: '8px'}}>
<SearchIcon style={{marginRight: '5px'}} fontSize='small' />
{params.search}
</span>
</span>
}
<span className='container-breadcurmbs flex-vertical-center' style={{paddingLeft: params.search ? '15px' : '10px', background: params.search ? '#f1f1f1' : WHITE, width: ownerURL ? 'auto': '100%', minHeight: '60px', paddingRight: params.search ? '0' : '15px'}}>
{
ownerURL &&
<OwnerButton uri={ownerURL} owner={owner} variant='outlined' style={{borderColor: ORANGE, color: ORANGE, boxShadow: 'none', textTransform: 'none', border: '1px solid'}} />
}
{
parentURL &&
<React.Fragment>
{
parent &&
<span className='separator'><SeparatorIcon /></span>
}
{
params.source &&
<SourceButton
noActions={!container}
source={container}
label={parent}
href={parentURL}
onEditClick={() => params.source ? setSourceForm(true) : setCollectionForm(true)}
onDeleteClick={() => setDeleteDialog(true) }
downloadFileName={downloadFileName}
{...parentProps}
/>
}
{
params.collection &&
<CollectionButton
noActions={!container}
collection={container}
label={parent}
href={parentURL}
onEditClick={() => setCollectionForm(true)}
onDeleteClick={() => setDeleteDialog(true) }
downloadFileName={downloadFileName}
{...parentProps}
/>
}
{
container &&
<React.Fragment>
<span className='separator'><SeparatorIcon /></span>
<VersionSelectorButton
selected={container}
versions={versions}
resource={parentType}
{...parentVersionProps}
/>
</React.Fragment>
}
{
params.collection && !isEmpty(expansions) && !isLoadingExpansions &&
<React.Fragment>
<span className='separator'><SeparatorIcon /></span>
<ExpansionSelectorButton
selected={expansion}
expansions={expansions}
version={container}
{...expansionProps}
/>
</React.Fragment>
}
</React.Fragment>
}
</span>
{
resource && selectedResource &&
<span className='resource-breadcurmbs flex-vertical-center' style={{background: '#f1f1f1', padding: '10px', paddingLeft: params.search ? 0 : '10px', border: '3px solid #f1f1f1', flex: 1, justifyContent: 'space-between'}}>
<span className='flex-vertical-center'>
{
params.search &&
<span className='separator'><SeparatorIcon /></span>
}
{
params.concept ?
<ConceptButton label={resource} href={params.source ? resourceURL : undefined} {...resourceProps} /> :
<MappingButton label={resource} href={params.source ? resourceURL : undefined} {...resourceProps} />
}
{
resourceVersion &&
<React.Fragment>
<span className='separator'><SeparatorIcon /></span>
<ResourceVersionButton label={resourceVersion} href={params.source ? resourceVersionURL : undefined} />
</React.Fragment>
}
<span className='flex-vertical-center' style={{marginLeft: '10px'}}>
<ManageSourceChildButton
resource={resourceType}
instance={selectedResource}
isVersionedObject={Boolean(!resourceVersion)}
currentURL={resourceVersion ? resourceVersionURL : resourceURL}
onEditClick={() => params.source ? (resourceType === 'concept' ? setConceptForm(true) : setMappingForm(true)) : null}
onRetire={() => params.source ? onRetire() : null}
onUnretire={() => params.source ? onUnretire() : null}
/>
</span>
</span>
<span className='flex-vertical-center' style={{background: '#f1f1f1', border: '1px solid #f1f1f1', marginRight: '60px'}}>
<IconButton size='small' color='secondary' onClick={onSplitViewClose}>
<CancelIcon fontSize='inherit' />
</IconButton>
</span>
</span>
}
{
!params.search && ownerURL && !selectedResource &&
<span className='resource-breadcurmbs flex-vertical-center' style={{background: WHITE, padding: '10px', border: `3px solid ${WHITE}`, flex: 1, minHeight: '60px'}} />
}
</span>
</div>
</div>
</div>
{
container && sourceForm &&
<CommonFormDrawer
style={{zIndex: '1202'}}
isOpen={sourceForm}
onClose={() => setSourceForm(false)}
size='smedium'
formComponent={
<SourceForm edit reloadOnSuccess onCancel={() => setSourceForm(false)} source={{...container, id: container.short_code}} owner={getOwner()} />
}
/>
}
{
container && collectionForm &&
<CommonFormDrawer
style={{zIndex: '1202'}}
isOpen={collectionForm}
onClose={() => setCollectionForm(false)}
size='smedium'
formComponent={
<CollectionForm edit reloadOnSuccess onCancel={() => setCollectionForm(false)} collection={{...container, id: container.short_code}} owner={getOwner()} />
}
/>
}
{
conceptForm && selectedResource &&
<CommonFormDrawer
style={{zIndex: 1202}}
isOpen={conceptForm}
onClose={() => setConceptForm(false)}
formComponent={
<ConceptForm edit reloadOnSuccess onCancel={() => setConceptForm(false)} concept={selectedResource} parentURL={resourceURL.replace('#', '')} />
}
/>
}
{
mappingForm && selectedResource &&
<CommonFormDrawer
style={{zIndex: 1202}}
isOpen={mappingForm}
onClose={() => setMappingForm(false)}
formComponent={
<MappingForm edit reloadOnSuccess onCancel={() => setMappingForm(false)} mapping={selectedResource} parentURL={resourceURL.replace('#', '')} />
}
/>
}
{
hasAccess && !isEmpty(container) &&
<ConceptContainerDelete open={deleteDialog} resource={{...container, id: container.short_code}} onClose={() => setDeleteDialog(false)} onDelete={deleteContainer} />
}
</header>
)
}
export default Breadcrumbs;