forked from HL7-DaVinci/crd-request-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRequestBuilder.jsx
More file actions
401 lines (371 loc) · 13.2 KB
/
RequestBuilder.jsx
File metadata and controls
401 lines (371 loc) · 13.2 KB
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
import { useContext, useState, useEffect } from 'react';
import { Button, Box, Grid, IconButton } from '@mui/material';
import PersonIcon from '@mui/icons-material/Person';
import RefreshIcon from '@mui/icons-material/Refresh';
import DisplayBox from '../components/DisplayBox/DisplayBox.jsx';
import '../index.css';
import RequestBox from '../components/RequestBox/RequestBox.jsx';
import buildRequest from '../util/buildRequest.js';
import { types, PATIENT_VIEW } from '../util/data.js';
import { createJwt } from '../util/auth.js';
import { getMedicationSpecificCdsHooksUrl, prepPrefetch } from '../util/util.js';
import Accordion from '@mui/material/Accordion';
import AccordionSummary from '@mui/material/AccordionSummary';
import AccordionDetails from '@mui/material/AccordionDetails';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import PatientSearchBar from '../components/RequestBox/PatientSearchBar/PatientSearchBar.jsx';
import { MedicationStatus } from '../components/MedicationStatus/MedicationStatus.jsx';
import { actionTypes } from './ContextProvider/reducer.js';
import axios from 'axios';
import { EtasuStatus } from '../components/EtasuStatus/EtasuStatus';
import { SettingsContext } from './ContextProvider/SettingsProvider.jsx';
const RequestBuilder = props => {
const { client } = props;
const [globalState, dispatch] = useContext(SettingsContext);
const [state, setState] = useState({
loading: false,
patient: {},
user: null,
expanded: true,
patientList: [],
response: {},
code: null,
codeSystem: null,
display: null,
prefetchedResources: new Map(),
request: {},
showSettings: false,
token: props.token,
client: client,
medicationDispense: null,
lastCheckedMedicationTime: null,
prefetchCompleted: false,
medicationRequests: {}
});
const displayRequestBox = !!globalState.patient?.id;
useEffect(() => {
console.log('Prefetched Resources updated:');
console.log(state.prefetchedResources);
}, [state.prefetchedResources]);
const isOrderNotSelected = () => {
return Object.keys(state.request).length === 0;
};
const disableGetMedicationStatus = isOrderNotSelected() || state.loading || globalState.disableMedicationStatus;
const disableGetEtasu = isOrderNotSelected() || state.loading;
const getMedicationStatus = () => {
setState(prevState => ({
...prevState,
lastCheckedMedicationTime: Date.now()
}));
axios.get(`${globalState.ehrUrl}/MedicationDispense?prescription=${state.request.id}`).then(
response => {
const bundle = response.data;
setState(prevState => ({
...prevState,
medicationDispense: bundle.entry?.[0].resource
}));
},
error => {
console.log('Was not able to get medication status', error);
}
);
};
useEffect(() => {
if (state.client) {
// Call patients on load of page
getPatients();
dispatch({
type: actionTypes.updateSetting,
settingId: 'baseUrl',
value: state.client.state.serverUrl
});
dispatch({
type: actionTypes.updateSetting,
settingId: 'ehrUrl',
value: state.client.state.serverUrl
});
}
// if use default user is set, use default user otherwise use logged in user if set
let currentUser = globalState.useDefaultUser ? globalState.defaultUser : (state.userId ? state.userId : globalState.defaultUser);
setState(prevState => ({...prevState, user: currentUser}));
}, []);
const updateStateElement = (elementName, text) => {
if (elementName === 'patient') {
setState(prevState => ({ ...prevState, patient: text }));
dispatch({
type: actionTypes.updatePatient,
value: text
});
} else {
setState(prevState => ({
...prevState,
[elementName]: text
}));
}
};
const getMedicationRequests = patientId => {
client
.request(`MedicationRequest?subject=Patient/${patientId}`, {
resolveReferences: ['subject', 'performer', 'medicationReference'],
graph: false,
flat: true
})
.then(result => {
setState(prevState => ({ ...prevState, medicationRequests: result }));
});
};
useEffect(() => {
const hook = globalState.hookToSend;
let remsAdminUrls = [];
// get all the remsAdminUrl for each MedicationRequest
state.medicationRequests?.data?.forEach(request => {
const remsAdminUrl = getMedicationSpecificCdsHooksUrl(request, globalState, hook);
if (remsAdminUrl) {
remsAdminUrls.push(remsAdminUrl);
}
//sendHook(prefetch, request, patient, hook, remsAdminUrl);
});
const uniqueUrls = [...new Set(remsAdminUrls.map(item => item))];
uniqueUrls?.forEach(url => {
sendHook(prepPrefetch(state.prefetchedResources), null, globalState.patient, hook, url);
});
}, [state.medicationRequests]);
const submitInfo = (prefetch, request, patient, hook) => {
console.log('Initiating form submission ', types.info);
let remsAdminUrl = null;
if (request) {
remsAdminUrl = getMedicationSpecificCdsHooksUrl(request, globalState, hook);
sendHook(prefetch, request, patient, hook, remsAdminUrl);
} else {
// get all MedicationRequests for the patient, then continue
getMedicationRequests(patient.id);
}
};
const sendHook = (prefetch, request, patient, hook, remsAdminUrl) => {
setState(prevState => ({
...prevState,
loading: !!remsAdminUrl,
patient
}));
if (!remsAdminUrl) {
return;
}
const hookConfig = {
includeConfig: globalState.includeConfig,
alternativeTherapy: globalState.alternativeTherapy
};
let user = state.user;
let json_request = buildRequest(
request,
user,
patient,
globalState.ehrUrlSentToRemsAdminForPreFetch,
state.client.state.tokenResponse,
prefetch,
globalState.sendPrefetch,
hook,
hookConfig,
'pharm0111'
);
let baseUrl = globalState.baseUrl;
const headers = {
'Content-Type': 'application/json'
};
if (globalState.generateJsonToken) {
const jwt = 'Bearer ' + createJwt(baseUrl, remsAdminUrl);
headers.authorization = jwt;
}
try {
fetch(remsAdminUrl, {
method: 'POST',
headers: new Headers(headers),
body: JSON.stringify(json_request)
})
.then(response => {
response.json().then(fhirResponse => {
console.log(fhirResponse);
if (fhirResponse?.status) {
console.log(
'Server returned status ' + fhirResponse.status + ': ' + fhirResponse.error
);
console.log(fhirResponse.message);
} else {
if (response?.url?.includes(PATIENT_VIEW)) {
// copy the cards from the old response into the new
setState(prevState => ({
...prevState,
response: { cards: [...(prevState.response.cards || []), ...fhirResponse.cards] }
}));
} else {
setState(prevState => ({ ...prevState, response: fhirResponse }));
}
}
setState(prevState => ({ ...prevState, loading: false }));
});
})
.catch(() => {
console.log('No response received from the server', types.error);
setState(prevState => ({ ...prevState, response: {}, loading: false }));
});
} catch (error) {
setState(prevState => ({ ...prevState, loading: false }));
console.log('Unexpected error occurred', types.error);
if (error instanceof TypeError) {
console.log(error.name + ': ' + error.message);
}
}
};
const getPatients = () => {
setState(prevState => ({ ...prevState, expanded: false }));
if (globalState.patientFhirQuery) {
client
.request(globalState.patientFhirQuery, { flat: true })
.then(result => {
setState(prevState => ({ ...prevState, patientList: result, expanded: true }));
})
.catch(e => {
setState(prevState => ({ ...prevState, patientList: e }));
console.log(e);
});
}
};
const updateStateMap = (elementName, key, text) => {
setState(prevState => ({
...prevState,
[elementName]: { ...prevState[elementName], [key]: text }
}));
};
const clearState = () => {
setState(prevState => ({
...prevState,
prefetchedResources: new Map(),
practitioner: {},
coverage: {},
response: {}
}));
};
const handleChange = () => (event, isExpanded) => {
setState(prevState => ({
...prevState,
expanded: isExpanded ? true : false
}));
};
const renderError = () => {
return (
<span className="patient-error">
Encountered Error: Try Refreshing The Client <br /> {state.patientList.message}{' '}
</span>
);
};
return (
<>
<Grid container spacing={2} padding={2}>
<Grid item xs={11}>
<Accordion expanded={state.expanded} onChange={handleChange()}>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-controls="panel1a-content"
id="panel1a-header"
>
<Button variant="contained" startIcon={<PersonIcon />} style={{padding:'10px',paddingLeft:'20px', paddingRight:'20px'}}>
Select a patient
</Button>
<span style={{ width: '30px'}}></span>
{state.patient?.name ? (
// Display the first name
<span><h4>{state.patient?.name?.[0]?.given?.[0] + ' ' + state.patient?.name?.[0]?.family}</h4></span>
) : (
<span><h4>All Patients</h4></span>
)}
</AccordionSummary>
<AccordionDetails>
{state.patientList.length > 0 && state.expanded && (
<Box>
{state.patientList instanceof Error ? (
renderError()
) : (
<PatientSearchBar
getPatients={getPatients}
searchablePatients={state.patientList}
client={client}
request={state.request}
launchUrl={globalState.launchUrl}
callback={updateStateElement}
callbackMap={updateStateMap}
clearCallback={clearState}
responseExpirationDays={globalState.responseExpirationDays}
user={state.user}
showButtons={true}
/>
)}
</Box>
)}
</AccordionDetails>
</Accordion>
</Grid>
<Grid item xs={1} alignContent="center" justifyContent="center">
<IconButton color="primary" onClick={() => getPatients()} size="large">
<RefreshIcon fontSize="large" />
</IconButton>
</Grid>
<Grid item container className="form-group" xs={12} md={6} spacing={2}>
{displayRequestBox && (
<Grid item>
<RequestBox
ehrUrl={globalState.ehrUrl}
submitInfo={submitInfo}
access_token={state.token}
client={state.client}
fhirServerUrl={globalState.baseUrl}
fhirVersion={'r4'}
patientId={globalState.patient.id}
patient={globalState.patient}
request={state.request}
response={state.response}
code={state.code}
codeSystem={state.codeSystem}
display={state.display}
prefetchedResources={state.prefetchedResources}
launchUrl={globalState.launchUrl}
responseExpirationDays={globalState.responseExpirationDays}
pimsUrl={globalState.pimsUrl}
smartAppUrl={globalState.smartAppUrl}
user={state.user}
loading={state.loading}
patientFhirQuery={globalState.patientFhirQuery}
prefetchCompleted={state.prefetchCompleted}
/>
</Grid>
)}
<Grid item container justifyContent="center" textAlign="center" spacing={2}>
{!disableGetEtasu && (
<Grid item>
<EtasuStatus code={state.code} request={state.request} />
</Grid>
)}
{!disableGetMedicationStatus && (
<Grid item>
<MedicationStatus
ehrUrl={globalState.ehrUrl}
request={state.request}
medicationDispense={state.medicationDispense}
getMedicationStatus={getMedicationStatus}
lastCheckedMedicationTime={state.lastCheckedMedicationTime}
/>
</Grid>
)}
</Grid>
</Grid>
<Grid item container xs={12} md={6}>
<DisplayBox
response={state.response}
client={state.client}
patientId={globalState.patient?.id}
ehrLaunch={true}
/>
</Grid>
</Grid>
</>
);
};
export default RequestBuilder;