-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCodeEditorModal.tsx
More file actions
229 lines (219 loc) · 7.46 KB
/
Copy pathCodeEditorModal.tsx
File metadata and controls
229 lines (219 loc) · 7.46 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
import { Modal, Button, Center, Group, Text, Grid, Select, Space, Stack } from '@mantine/core';
import CodeMirror from '@uiw/react-codemirror';
import { json, jsonParseLinter } from '@codemirror/lang-json';
import { linter } from '@codemirror/lint';
import { useEffect, useState } from 'react';
import { parsedCodePaths } from 'fhir-spec-tools/build/data/codePaths';
import { IconCodePlus } from '@tabler/icons-react';
import { valueSetMapState } from '../../state/selectors/valueSetsMap';
import { useRecoilValue } from 'recoil';
import { dedupVSCodes, getDRC } from '../../util/ValueSetHelper';
import { measureBundleState } from '../../state/atoms/measureBundle';
import fhirpath from 'fhirpath';
const jsonLinter = jsonParseLinter();
export interface CodeEditorModalProps {
open: boolean;
onClose: () => void;
onSave: (value: string) => void;
title?: string;
initialValue?: string;
}
export default function CodeEditorModal({
open = true,
onClose,
title,
onSave,
initialValue = ''
}: CodeEditorModalProps) {
const valueSetMap = useRecoilValue(valueSetMapState);
const [currentValue, setCurrentValue] = useState(initialValue);
const [linterError, setLinterError] = useState<string | null>(null);
const [attributeValue, setAttributeValue] = useState<string | null>('');
const [vsValue, setVsValue] = useState<string | null>('');
const [codeValue, setCodeValue] = useState<string | null>('');
const measureBundle = useRecoilValue(measureBundleState);
// capture passed initialValue state and reset selections on open
useEffect(() => {
if (open) {
setCurrentValue(initialValue);
setAttributeValue('');
setVsValue('');
setCodeValue('');
}
}, [open, initialValue]);
// find all code-like attributes for the parse resource type
let codeAttributes: string[] = [];
if (!linterError && currentValue) {
try {
const resource: fhir4.Resource = JSON.parse(currentValue);
codeAttributes = Object.keys(parsedCodePaths[resource.resourceType].paths);
} catch {
// current json invalid or no valid resourceType
}
}
// Function for inserting code based on the selected attribute, vs, and code
// Note: choiceType ignored - current choice types only allow for an alternative non-code-like choice (i.e. "reference"),
const insertCode = () => {
if (!attributeValue || !vsValue || !codeValue) {
//should be disabled
console.error('Unexpected code insertion accessed.');
return;
}
const resource: fhir4.Resource = JSON.parse(currentValue);
const path = parsedCodePaths[resource.resourceType].paths[attributeValue];
let codedObject: fhir4.CodeableConcept | fhir4.Coding | string;
let fullPath = attributeValue;
const { code, system, display, version } = JSON.parse(codeValue) as fhir4.ValueSetExpansionContains; // pulls all fields overlapping with Coding
if (path.codeType === 'FHIR.CodeableConcept') {
codedObject = {
coding: [{ code, system, display, version }]
} as fhir4.CodeableConcept;
if (path.choiceType) fullPath += 'CodeableConcept';
} else if (path.codeType === 'FHIR.Coding') {
codedObject = {
code,
system,
display,
version
} as fhir4.Coding;
if (path.choiceType) fullPath += 'Coding';
} else {
codedObject = code as string;
// case doesn't exist in current data model:
// if(path.choiceType) fullPath += 'Code';
}
if (path.multipleCardinality) {
// add to or create array
const attributeData = fhirpath.evaluate(resource, fullPath)[0];
if (attributeData) {
// add
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(resource as any)[fullPath].push(codedObject);
} else {
//create
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(resource as any)[fullPath] = [codedObject];
}
} else {
// replace existing single attribute
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(resource as any)[fullPath] = codedObject;
}
setCurrentValue(JSON.stringify(resource, null, 2));
};
return (
<Modal
centered
size={1000}
withCloseButton={false}
opened={open}
onClose={onClose}
styles={{
body: {
height: '800px'
}
}}
title={title}
>
<Stack>
<Select
label="Attribute"
placeholder="Select coded attribute"
data={codeAttributes}
value={attributeValue}
onChange={setAttributeValue}
disabled={codeAttributes.length === 0}
/>
<Select
label="ValueSet"
placeholder="Select ValueSet"
data={[
{ value: 'DRC', label: 'Direct Reference Code' },
...Object.keys(valueSetMap).map(k => ({ value: k, label: `${valueSetMap[k]} (${k})` }))
]} //label format: name/title (url)
value={vsValue}
onChange={value => {
setVsValue(value);
setCodeValue('');
}}
searchable
/>
<Select
label="Code"
placeholder="Select Code"
data={
vsValue
? (vsValue === 'DRC' ? getDRC(measureBundle.content) : dedupVSCodes(vsValue, measureBundle.content)).map(
coding => ({
value: JSON.stringify(coding),
label: `${coding.code} - ${coding.display} (${coding.system}, version ${coding.version})`
})
)
: []
}
value={codeValue}
onChange={setCodeValue}
searchable
disabled={!vsValue}
/>
</Stack>
<Space h="md" />
<Center>
<Button
variant="filled"
rightIcon={<IconCodePlus />}
disabled={codeAttributes.length === 0 || !attributeValue || !vsValue || !codeValue}
onClick={() => insertCode()}
>
Insert Code
</Button>
</Center>
<Space h="md" />
<div style={{ overflow: 'scroll' }}>
{open && (
<CodeMirror
data-autofocus
data-testid="codemirror"
height="700px"
value={currentValue}
extensions={[json(), linter(jsonLinter)]}
theme="light"
onUpdate={v => {
const diagnosticMessages = jsonLinter(v.view).map(d => d.message);
if (diagnosticMessages.length === 0) {
setLinterError(null);
} else {
setLinterError(diagnosticMessages.join('\n'));
}
// Grabbing updates from the readonly editor state to avoid codemirror weirdness
setCurrentValue(v.state.toJSON().doc);
}}
/>
)}
</div>
<Grid>
<Grid.Col span={12}>
<Text color="red">{linterError} </Text>
</Grid.Col>
<Grid.Col span={12}>
<Center>
<Group>
<Button
data-testid="codemirror-save-button"
onClick={() => {
onSave(currentValue);
}}
disabled={linterError != null}
>
Save
</Button>
<Button data-testid="codemirror-cancel-button" variant="default" onClick={onClose}>
Cancel
</Button>
</Group>
</Center>
</Grid.Col>
</Grid>
</Modal>
);
}