-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMeasureRepositoryUpload.tsx
More file actions
258 lines (243 loc) · 9.33 KB
/
Copy pathMeasureRepositoryUpload.tsx
File metadata and controls
258 lines (243 loc) · 9.33 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
import { Button, Collapse, Grid, Group, Loader, Select, TextInput } from '@mantine/core';
import React, { useState } from 'react';
import { useEffect } from 'react';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import { measureBundleState } from '../../state/atoms/measureBundle';
import { measurementPeriodEndState, measurementPeriodStartState } from '../../state/atoms/measurementPeriod';
import {
identifyMissingValueSets,
MeasureUploadProps,
populateMeasurementPeriod,
rejectUpload
} from '../../util/measureUploadUtils';
import { showNotification, cleanNotifications } from '@mantine/notifications';
import { IconAlertCircle, IconCircleCheck } from '@tabler/icons-react';
import { displayMapToSelectDataState } from '../../state/selectors/displayMapToSelectData';
enum PACKAGE_STATES {
LOADING,
SUCCESS,
FAIL,
NONE
}
const MEASURE_RETRIEVAL_BODY = {
resourceType: 'Parameters',
parameter: [
{
name: 'include-terminology',
valueBoolean: true
}
]
};
const MEASURE_RETRIEVAL_OPTIONS = {
method: 'POST',
body: JSON.stringify(MEASURE_RETRIEVAL_BODY),
headers: { 'Content-Type': 'application/json+fhir' }
};
export default function MeasureRepositoryUpload({ logError }: MeasureUploadProps) {
const [isLoadingIds, setIsLoadingIds] = useState(false);
const [isLoadingPackage, setIsLoadingPackage] = useState<PACKAGE_STATES>(PACKAGE_STATES.NONE);
const [measureSelectIsOpen, setMeasureSelectIsOpen] = useState(false);
const [{ measureRepositoryUrl, isFile, selectedMeasureId, displayMap }, setMeasureBundle] =
useRecoilState(measureBundleState);
const setPeriodStart = useSetRecoilState(measurementPeriodStartState);
const setPeriodEnd = useSetRecoilState(measurementPeriodEndState);
const idSelectData = useRecoilValue(displayMapToSelectDataState);
useEffect(() => {
if (isFile) {
setMeasureSelectIsOpen(false);
setIsLoadingPackage(PACKAGE_STATES.NONE);
}
}, [isFile]);
/**
* Handles updating of the Measure Repository Service url field and associated state variables
*/
function handleMrsUrlChange(event: React.ChangeEvent<HTMLInputElement>) {
if (measureSelectIsOpen) {
setMeasureSelectIsOpen(false);
}
setMeasureBundle(mb => ({
...mb,
measureRepositoryUrl: event.currentTarget.value,
selectedMeasureId: null,
displayMap: {},
...(!mb.isFile ? { content: null } : undefined)
}));
setIsLoadingPackage(PACKAGE_STATES.NONE);
}
/**
* Takes the entries from a Measure retrieval response and creates a map from Measure id
* to Measure display label for Measure select component
*/
function createMeasureDisplayMap(
displayMap: Record<string, string>,
{ resource: measure }: { resource: fhir4.Measure }
) {
let label: string;
const officialIdentifier = measure.identifier?.find(
identifier =>
identifier.use === 'official' && identifier.system === 'http://hl7.org/fhir/cqi/ecqm/Measure/Identifier/cms'
);
if (officialIdentifier?.value) {
label = measure.version ? `${officialIdentifier.value}|${measure.version}` : officialIdentifier.value;
} else if (measure.name) {
label = measure.version ? `${measure.name}|${measure.version}` : measure.name;
} else {
label = measure.id as string;
}
displayMap[measure.id as string] = label;
return displayMap;
}
/**
* Sends a `GET` request to the `/Measure` endpoint of the specified Measure Repository Service.
* Parses and stores the measureIds if successful
*/
async function retrieveMeasures() {
setIsLoadingIds(true);
let parsedMeasureRepositoryUrl = measureRepositoryUrl;
if (measureRepositoryUrl.slice(-1) === '/') {
parsedMeasureRepositoryUrl = measureRepositoryUrl.slice(0, -1);
setMeasureBundle(mb => ({ ...mb, measureRepositoryUrl: parsedMeasureRepositoryUrl }));
}
try {
const response = await fetch(`${parsedMeasureRepositoryUrl}/Measure`);
const responseBody = await response.json();
if (response.status === 200) {
if (responseBody.entry.length === 0) {
showNotification({
icon: <IconAlertCircle />,
title: 'No measures found',
message: `Measure repository service hosted at ${parsedMeasureRepositoryUrl} returned an empty
FHIR Bundle when queried for Measures. Ensure Measures are present on server.`,
color: 'red'
});
setIsLoadingIds(false);
return;
}
const displayMap = responseBody.entry.reduce(createMeasureDisplayMap, {});
setMeasureBundle(mb => ({ ...mb, displayMap }));
if (!measureSelectIsOpen) {
setMeasureSelectIsOpen(true);
}
} else {
showNotification({
icon: <IconAlertCircle />,
title: 'Failed reaching out to server',
message: `Server responded with code: ${response.status}:${response.statusText}`,
color: 'red'
});
}
} catch {
showNotification({
icon: <IconAlertCircle />,
title: 'Failed reaching out to server',
message: `Could not connect to server with url: ${parsedMeasureRepositoryUrl}`,
color: 'red'
});
}
setIsLoadingIds(false);
}
/**
* Sends a `POST` request to the `Measure/:id/$cqfm.package` endpoint of the specified Measure
* Repository Service. Checks the resulting bundle is valid and saves it if so.
*/
async function retrieveMeasurePackage(id: string) {
setMeasureBundle(mb => ({ ...mb, selectedMeasureId: id }));
if (id) {
const display = displayMap[id];
showNotification({
loading: true,
title: `Packaging ${display}...`,
message: `Waiting for measure repository service to finish packaging Measure: ${display}`,
color: 'blue'
});
setIsLoadingPackage(PACKAGE_STATES.LOADING);
const response = await fetch(`${measureRepositoryUrl}/Measure/${id}/$cqfm.package`, MEASURE_RETRIEVAL_OPTIONS);
const responseBody: fhir4.Bundle | fhir4.OperationOutcome = await response.json();
if (responseBody.resourceType !== 'Bundle') {
if (responseBody.resourceType === 'OperationOutcome') {
rejectUpload(
`Packaging of Measure: ${display} failed with message: "${responseBody.issue[0].details?.text}"`,
display,
logError,
true
);
} else {
rejectUpload("Uploaded file must be a JSON FHIR Resource of type 'Bundle'", display, logError, true);
}
setIsLoadingPackage(PACKAGE_STATES.FAIL);
return;
}
const measures = responseBody.entry?.filter(e => e.resource?.resourceType === 'Measure');
if (measures?.length !== 1) {
rejectUpload(
`Expected exactly 1 Measure in uploaded Bundle, received: ${measures?.length}`,
display,
logError,
true
);
setIsLoadingPackage(PACKAGE_STATES.FAIL);
return;
}
const missingValueSets = await identifyMissingValueSets(responseBody);
if (missingValueSets.length > 0) {
rejectUpload(missingValueSets, display, logError, true, true);
setIsLoadingPackage(PACKAGE_STATES.FAIL);
return;
}
const effectivePeriod = (measures[0].resource as fhir4.Measure).effectivePeriod;
// Set measurement period to default period
const { start, end } = populateMeasurementPeriod(effectivePeriod?.start, effectivePeriod?.end);
setMeasureBundle(mb => ({ ...mb, content: responseBody, isFile: false }));
setPeriodStart(start);
setPeriodEnd(end);
setIsLoadingPackage(PACKAGE_STATES.SUCCESS);
cleanNotifications();
showNotification({
icon: <IconCircleCheck />,
title: 'Upload Success',
message: `Successfully uploaded ${display}`,
color: 'green'
});
} else {
setIsLoadingPackage(PACKAGE_STATES.NONE);
}
}
return (
<div>
<Group position="apart" align="flex-end">
<TextInput
label="Measure Repository Service URL"
placeholder="http://example.com/fhir"
value={measureRepositoryUrl}
onChange={handleMrsUrlChange}
style={{ flexGrow: 1 }}
/>
<Button onClick={retrieveMeasures} loading={isLoadingIds} disabled={measureRepositoryUrl === ''}>
Get Measures
</Button>
</Group>
<Collapse in={measureSelectIsOpen || (selectedMeasureId !== null && measureRepositoryUrl !== '')}>
<Grid>
<Grid.Col span={11}>
<Select
label="Measure ID"
data={idSelectData}
disabled={idSelectData.length === 0}
value={selectedMeasureId}
onChange={retrieveMeasurePackage}
placeholder="Select ID"
searchable
clearable
nothingFound={'No options matching search'}
/>
</Grid.Col>
<Grid.Col span={1} style={{ display: 'flex', alignItems: 'end', justifyContent: 'center' }}>
{isLoadingPackage === PACKAGE_STATES.LOADING && <Loader />}
{isLoadingPackage === PACKAGE_STATES.SUCCESS && <IconCircleCheck color="green" size={40} />}
{isLoadingPackage === PACKAGE_STATES.FAIL && <IconAlertCircle color="red" size={40} />}
</Grid.Col>
</Grid>
</Collapse>
</div>
);
}