-
Notifications
You must be signed in to change notification settings - Fork 12
/
OperationsDrawer.jsx
402 lines (391 loc) · 15.4 KB
/
OperationsDrawer.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import React from 'react';
import ReactJson from 'react-json-view'
import alertifyjs from 'alertifyjs';
import {
Toolbar, Button, Drawer, IconButton, TextField, FormControl, InputLabel, Select, MenuItem,
ButtonGroup
} from '@mui/material';
import makeStyles from '@mui/styles/makeStyles';
import {
CancelOutlined as CloseIcon,
OpenInNew as NewTabIcon,
FileCopy as CopyIcon,
} from '@mui/icons-material';
import { get, map, includes, uniq, filter, find, startCase, isString, isObject, merge, forEach } from 'lodash';
import { OperationsContext } from '../app/LayoutContext';
import {
getFHIRServerConfigFromCurrentContext, getAppliedServerConfig, getServerConfigsForCurrentUser, copyURL, urlSearchParamsToObject
} from '../../common/utils';
import { FHIR_OPERATIONS, GREEN, ERROR_RED, BLACK, DEFAULT_CASCADE_PARAMS } from '../../common/constants';
import APIService from '../../services/APIService';
import CascadeParametersForm from './CascadeParametersForm';
const drawerWidth = 350;
const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
},
appBar: {
zIndex: theme.zIndex.drawer + 1
},
appBarShift: {
width: `calc(100% - ${drawerWidth}px)`,
marginLeft: drawerWidth,
transition: theme.transitions.create(['margin', 'width'], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.enteringScreen,
}),
},
menuButton: {
marginRight: 0,
},
hide: {
display: 'none',
},
drawerContainer: {
overflow: 'auto',
padding: '0 10px'
},
drawer: {
width: drawerWidth,
flexShrink: 0,
},
drawerPaper: {
width: drawerWidth,
zIndex: 1202
},
drawerHeader: {
display: 'flex',
alignItems: 'center',
padding: theme.spacing(0, 1),
height: '65px',
// necessary for content to be below app bar
...theme.mixins.toolbar,
},
content: {
flexGrow: 1,
padding: theme.spacing(3),
transition: theme.transitions.create('margin', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
marginLeft: -drawerWidth,
},
contentShift: {
transition: theme.transitions.create('margin', {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.enteringScreen,
}),
marginLeft: 0,
},
}));
const OperationsDrawer = () => {
const classes = useStyles();
const { setOpenOperations, operationItem, parentResource, parentItem } = React.useContext(OperationsContext);
const currentServer = getAppliedServerConfig()
const fhirServer = getFHIRServerConfigFromCurrentContext()
let containerResource = parentResource || 'source'
const isSource = parentResource === 'source'
const fhirResource = isSource ? 'codeSystem' : 'valueSet'
const fhirResourceDisplay = startCase(fhirResource).replace(' ', '')
const operations = uniq([...get(fhirServer, `operations.${fhirResource}`, []), ...get(currentServer, `operations.${containerResource}`, [])])
const [byURL, setByURL] = React.useState(false)
React.useEffect(
() => {
setItem(operationItem)
setVersion(get(operationItem, 'parentVersion') || 'HEAD')
setParentId(getParentId(operationItem))
setCode(getItemCode(operationItem))
if(!parentItem)
getParent(operationItem)
},
[operationItem]
)
React.useEffect(
() => {
if(parentItem)
setCanonicalURL(parentItem.canonical_url || '')
},
[parentItem]
)
React.useEffect(
() => setValuesFromURL(),
[]
)
const shouldGetParent = _item => {
_item ||= item
return _item && currentServer.type === 'ocl' && _item.concept_class && fhirServer
}
const getParent = _item => {
_item ||= item
if(shouldGetParent(_item)) {
APIService.new().overrideURL(`${_item.owner_url}sources/${_item.source}/`).get().then(res => {
setCanonicalURL(get(res.data, 'canonical_url') || '')
})
}
}
const getParentId = _item => {
if(parentItem)
return parentItem.id
return get(_item || item, 'source') || ''
}
const getItemCode = _item => get(_item || item, 'id') || ''
const [item, setItem] = React.useState(operationItem)
const [parentId, setParentId] = React.useState(getParentId)
const [canonicalURL, setCanonicalURL] = React.useState(get(operationItem, 'canonical_url') || '')
const [systemURL, setSystemURL] = React.useState('')
const [systemVersion, setSystemVersion] = React.useState('')
const [code, setCode] = React.useState(getItemCode)
const [version, setVersion] = React.useState('HEAD')
const [operation, setOperation] = React.useState('');
const [response, setResponse] = React.useState(null);
const [url, setURL] = React.useState(null);
const [isFetching, setIsFetching] = React.useState(false);
const [selectedFHIRServerId, setSelectedFHIRServerId] = React.useState(get(fhirServer, 'id', ''))
const [cascadeParams, setCascadeParams] = React.useState({...DEFAULT_CASCADE_PARAMS})
const onOperationChange = event => setOperation(event.target.value)
const setValuesFromURL = () => {
const [url, query] = window.location.hash.split('?')
if(url && url.includes('/$cascade'))
setOperation('$cascade')
if(query) {
const queryParams = new URLSearchParams(query)
let newParams = merge(cascadeParams, urlSearchParamsToObject(queryParams))
forEach(newParams, (value, key) => {
if(['true', 'false'].includes(value))
newParams[key] = value === 'true'
})
setCascadeParams(newParams)
}
}
const onExecute = event => {
setIsFetching(true)
setResponse(null)
event.preventDefault()
event.stopPropagation()
const isFHIROperation = includes(FHIR_OPERATIONS, operation)
if(isFHIROperation) {
const selectedFHIRServer = getSelectedFHIRServer()
if(canonicalURL && selectedFHIRServer) {
const service = APIService.new()
const canonicalURLAttr = operation === '$lookup' ? 'system' : 'url'
service.URL = `${selectedFHIRServer.url}${selectedFHIRServer.info.baseURI}${fhirResourceDisplay}/${operation}/?code=${code}&${canonicalURLAttr}=${canonicalURL}`
if(version && version.toLowerCase() !== 'head' && !byURL)
service.URL += `&version=${version}`
if(operation === '$validate-code') {
if(systemURL)
service.URL += `&system=${systemURL}`
if(systemVersion)
service.URL += `&systemVersion=${systemVersion}`
}
service.get(null, false, null, true).then(_response => {
if(get(_response, 'response.status') >= 400) {
setResponse(get(_response, 'response'))
} else {
setResponse(_response)
}
setURL(_response?.request?.responseURL || _response?.config?.url)
setIsFetching(false)
})
} else {
if(!selectedFHIRServer)
alertifyjs.error('No FHIR Server found associated to this OCL server.')
else
alertifyjs.error('No canonical_url found.')
setIsFetching(false)
setResponse(null)
setURL(null)
}
} else {
let queryParams = operation === '$cascade' ? cascadeParams : {}
APIService.new().overrideURL(parentItem.version_url || parentItem.url).appendToUrl(`concepts/${code}/${operation}/`).get(null, null, queryParams).then(
_response => {
setURL(_response?.request?.responseURL || _response?.config?.url)
setResponse(_response)
setIsFetching(false)
}
)
}
}
const onOpenInNewTab = () => window.open(url)
const onCopyURLClick = () => copyURL(url)
const responseLabel = isFetching ? 'Response: (fetching...)' : `Response: (status: ${get(response, 'status', 'null')})`;
const isError = get(response, 'status') !== 200 && !isFetching
const fhirServers = filter(getServerConfigsForCurrentUser(), {type: 'fhir'})
const onFHIRServerChange = event => setSelectedFHIRServerId(event.target.value)
const getSelectedFHIRServer = () => find(fhirServers, {id: selectedFHIRServerId})
const toggleByURL = _byURL => {
if(_byURL && operation === '$cascade')
setOperation('')
setByURL(_byURL)
}
const getResponse = () => {
let data = get(response, 'data') || get(response, 'error')
if(isString(data))
return {response: data}
if(isObject(data))
return data
return response
}
return (
<React.Fragment>
<Drawer
className={classes.drawer}
variant="persistent"
anchor="right"
open
classes={{paper: classes.drawerPaper,}}
onClose={() => setOpenOperations(false)}
>
<Toolbar />
<div className={classes.drawerContainer}>
<div style={{justifyContent: 'space-between', display: 'flex', alignItems: 'center'}}>
<h3>Operations (Beta)</h3>
<IconButton size='small' color='secondary' onClick={() => setOpenOperations(false)} style={{float: 'right'}}>
<CloseIcon fontSize='inherit' />
</IconButton>
</div>
<div className='col-xs-12 no-side-padding'>
<div className='col-xs-12 no-side-padding' style={{textAlign: 'center', marginBottom: '20px'}}>
<ButtonGroup size='small'>
<Button variant={byURL ? 'outlined' : 'contained'} onClick={() => toggleByURL(false)}>
Find Resource
</Button>
<Button variant={byURL ? 'contained' : 'outlined'} onClick={() => toggleByURL(true)}>
Enter URL
</Button>
</ButtonGroup>
</div>
<div className='col-xs-12 no-side-padding'>
<FormControl fullWidth>
<InputLabel>Server Base URL</InputLabel>
<Select
value={selectedFHIRServerId}
label="Server Base URL"
onChange={onFHIRServerChange}
>
{
map(fhirServers, server => {
return <MenuItem value={server.id} key={server.id}>{`${server.name} (${server.url})`}</MenuItem>
})
}
</Select>
</FormControl>
</div>
{
byURL ?
<div className='col-xs-12 no-side-padding'>
<div className='col-xs-12 no-side-padding'>
<h4 style={{marginBottom: '15px'}}>
Canonical URL
</h4>
<div className='col-xs-12 no-left-padding'>
<TextField fullWidth value={canonicalURL} label='CanonicalURL' onChange={event => setCanonicalURL(event.target.value || '')} size='small' />
</div>
</div>
{
operation === '$validate-code' &&
<React.Fragment>
<div className='col-xs-12 no-side-padding'>
<h4 style={{marginBottom: '15px'}}>
System URL
</h4>
<div className='col-xs-12 no-left-padding'>
<TextField fullWidth value={systemURL} label='SystemURL' onChange={event => setSystemURL(event.target.value || '')} size='small' />
</div>
</div>
<div className='col-xs-12 no-side-padding'>
<h4 style={{marginBottom: '15px'}}>
System Version
</h4>
<div className='col-xs-12 no-left-padding'>
<TextField fullWidth value={systemVersion} label='SystemVersion' onChange={event => setSystemVersion(event.target.value || '')} size='small' />
</div>
</div>
</React.Fragment>
}
</div> :
<div className='col-xs-12 no-side-padding'>
<h4 style={{marginBottom: '15px'}}>
{fhirResourceDisplay}
</h4>
<div className='col-xs-8 no-left-padding'>
<TextField fullWidth value={parentId} label={fhirResourceDisplay} size='small' />
</div>
<div className='col-xs-4 no-side-padding'>
<TextField fullWidth value={version} label='Version' onChange={event => setVersion(event.target.value)} size='small' />
</div>
</div>
}
<div className='col-xs-12 no-side-padding'>
<h4 style={{marginTop: '30px', marginBottom: '15px'}}>
Resource & Operation
</h4>
<div className='col-xs-6 no-left-padding'>
<TextField value={code} label='Code' fullWidth onChange={event => setCode(event.target.value)} size='small' />
</div>
<div className='col-xs-6 no-side-padding'>
<FormControl fullWidth>
<InputLabel>Operation</InputLabel>
<Select
value={operation}
label="Operation"
onChange={onOperationChange}
size='small'
>
{
map(operations, _operation => {
return <MenuItem size='small' value={_operation} key={_operation} disabled={byURL && _operation === '$cascade'}>{_operation}</MenuItem>
})
}
</Select>
</FormControl>
</div>
</div>
{
operation === '$cascade' &&
<div className='col-xs-12 no-side-padding'>
<h4 style={{marginTop: '30px', marginBottom: '15px'}}>
Cascade Parameters
</h4>
<CascadeParametersForm onChange={setCascadeParams} defaultParams={cascadeParams} />
</div>
}
<div className='col-xs-12 no-side-padding' style={{textAlign: 'right', margin: '15px 0'}}>
<Button onClick={onExecute} variant='contained' disabled={!operation}>Execute</Button>
</div>
{
(response || isFetching) &&
<div className='col-xs-12 no-side-padding'>
<h4 style={{margin: 0, color: isError ? ERROR_RED : (isFetching ? BLACK : GREEN)}}>
<i>{responseLabel}</i>
{
!isFetching &&
<React.Fragment>
<IconButton color='primary' onClick={onOpenInNewTab} size='small'>
<NewTabIcon fontSize='small' />
</IconButton>
<IconButton onClick={onCopyURLClick} size='small'>
<CopyIcon fontSize='inherit' />
</IconButton>
</React.Fragment>
}
</h4>
<div className='col-xs-12 no-side-padding'>
<ReactJson
theme='hopscotch'
name={false}
displayDataTypes={false}
displayObjectSize={false}
src={getResponse() || {}}
style={{overflow: 'auto'}}
/>
</div>
</div>
}
</div>
</div>
</Drawer>
</React.Fragment>
)
}
export default OperationsDrawer;