@openmrs/esm-form-builder-app 1.0.1-pre.126
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +2 -0
- package/.eslintrc +33 -0
- package/.husky/pre-commit +4 -0
- package/.husky/pre-push +6 -0
- package/.prettierignore +14 -0
- package/.yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs +541 -0
- package/.yarn/plugins/@yarnpkg/plugin-version.cjs +550 -0
- package/.yarn/versions/7d315ef1.yml +0 -0
- package/LICENSE +401 -0
- package/README.md +14 -0
- package/__mocks__/react-i18next.js +56 -0
- package/dist/openmrs-esm-form-builder-app.js +1 -0
- package/i18next-parser.config.js +89 -0
- package/jest.config.json +19 -0
- package/package.json +102 -0
- package/src/components/dashboard/dashboard.component.tsx +310 -0
- package/src/components/dashboard/dashboard.scss +112 -0
- package/src/components/empty-state/empty-data-illustration.component.tsx +51 -0
- package/src/components/empty-state/empty-state.component.tsx +41 -0
- package/src/components/empty-state/empty-state.scss +55 -0
- package/src/components/error-state/error-state.component.tsx +37 -0
- package/src/components/error-state/error-state.scss +49 -0
- package/src/components/form-editor/form-editor.component.tsx +297 -0
- package/src/components/form-editor/form-editor.scss +50 -0
- package/src/components/form-renderer/form-renderer.component.tsx +82 -0
- package/src/components/form-renderer/form-renderer.scss +31 -0
- package/src/components/interactive-builder/add-question-modal.component.tsx +494 -0
- package/src/components/interactive-builder/edit-question-modal.component.tsx +447 -0
- package/src/components/interactive-builder/editable-value.component.tsx +60 -0
- package/src/components/interactive-builder/editable-value.scss +23 -0
- package/src/components/interactive-builder/interactive-builder.component.tsx +403 -0
- package/src/components/interactive-builder/interactive-builder.scss +83 -0
- package/src/components/interactive-builder/new-form-modal.component.tsx +86 -0
- package/src/components/interactive-builder/page-modal.component.tsx +91 -0
- package/src/components/interactive-builder/question-modal.scss +35 -0
- package/src/components/interactive-builder/section-modal.component.tsx +94 -0
- package/src/components/interactive-builder/value-editor.component.tsx +55 -0
- package/src/components/interactive-builder/value-editor.scss +10 -0
- package/src/components/modals/save-form.component.tsx +310 -0
- package/src/components/modals/save-form.scss +5 -0
- package/src/components/schema-editor/schema-editor.component.tsx +190 -0
- package/src/components/schema-editor/schema-editor.scss +30 -0
- package/src/config-schema.ts +47 -0
- package/src/constants.ts +3 -0
- package/src/declarations.d.tsx +2 -0
- package/src/form-builder-app-menu-link.component.tsx +13 -0
- package/src/forms.resource.ts +178 -0
- package/src/hooks/useClobdata.ts +20 -0
- package/src/hooks/useConceptLookup.ts +18 -0
- package/src/hooks/useEncounterTypes.ts +18 -0
- package/src/hooks/useForm.ts +18 -0
- package/src/hooks/useForms.ts +20 -0
- package/src/index.ts +70 -0
- package/src/root.component.tsx +19 -0
- package/src/setup-tests.ts +1 -0
- package/src/types.ts +132 -0
- package/translations/en.json +110 -0
- package/tsconfig.json +23 -0
- package/turbo.json +26 -0
- package/webpack.config.js +19 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { useTranslation } from "react-i18next";
|
|
3
|
+
import {
|
|
4
|
+
Button,
|
|
5
|
+
ComposedModal,
|
|
6
|
+
Form,
|
|
7
|
+
FormGroup,
|
|
8
|
+
ModalBody,
|
|
9
|
+
ModalFooter,
|
|
10
|
+
ModalHeader,
|
|
11
|
+
TextInput,
|
|
12
|
+
} from "@carbon/react";
|
|
13
|
+
import { showToast, showNotification } from "@openmrs/esm-framework";
|
|
14
|
+
import { Schema } from "../../types";
|
|
15
|
+
|
|
16
|
+
type SectionModalProps = {
|
|
17
|
+
schema: Schema;
|
|
18
|
+
onSchemaChange: (schema: Schema) => void;
|
|
19
|
+
pageIndex: number;
|
|
20
|
+
resetIndices: () => void;
|
|
21
|
+
showModal: boolean;
|
|
22
|
+
onModalChange: (showModal: boolean) => void;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const SectionModal: React.FC<SectionModalProps> = ({
|
|
26
|
+
schema,
|
|
27
|
+
onSchemaChange,
|
|
28
|
+
pageIndex,
|
|
29
|
+
resetIndices,
|
|
30
|
+
showModal,
|
|
31
|
+
onModalChange,
|
|
32
|
+
}) => {
|
|
33
|
+
const { t } = useTranslation();
|
|
34
|
+
const [sectionTitle, setSectionTitle] = useState("");
|
|
35
|
+
|
|
36
|
+
const handleUpdatePageSections = () => {
|
|
37
|
+
updateSections();
|
|
38
|
+
onModalChange(false);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const updateSections = () => {
|
|
42
|
+
try {
|
|
43
|
+
schema.pages[pageIndex]?.sections?.push({
|
|
44
|
+
label: sectionTitle,
|
|
45
|
+
isExpanded: "true",
|
|
46
|
+
questions: [],
|
|
47
|
+
});
|
|
48
|
+
onSchemaChange({ ...schema });
|
|
49
|
+
setSectionTitle("");
|
|
50
|
+
resetIndices();
|
|
51
|
+
|
|
52
|
+
showToast({
|
|
53
|
+
title: t("success", "Success!"),
|
|
54
|
+
kind: "success",
|
|
55
|
+
critical: true,
|
|
56
|
+
description: t("sectionCreated", "New section created"),
|
|
57
|
+
});
|
|
58
|
+
} catch (error) {
|
|
59
|
+
showNotification({
|
|
60
|
+
title: t("errorCreatingSection", "Error creating section"),
|
|
61
|
+
kind: "error",
|
|
62
|
+
critical: true,
|
|
63
|
+
description: error?.message,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
<ComposedModal open={showModal} onClose={() => onModalChange(false)}>
|
|
70
|
+
<ModalHeader title={t("createNewSection", "Create a new section")} />
|
|
71
|
+
<Form onSubmit={(event) => event.preventDefault()}>
|
|
72
|
+
<ModalBody>
|
|
73
|
+
<FormGroup legendText={""}>
|
|
74
|
+
<TextInput
|
|
75
|
+
id="sectionTitle"
|
|
76
|
+
labelText={t("enterSectionTitle", "Enter a section title")}
|
|
77
|
+
value={sectionTitle}
|
|
78
|
+
onChange={(event) => setSectionTitle(event.target.value)}
|
|
79
|
+
/>
|
|
80
|
+
</FormGroup>
|
|
81
|
+
</ModalBody>
|
|
82
|
+
</Form>
|
|
83
|
+
<ModalFooter>
|
|
84
|
+
<Button onClick={() => onModalChange(false)} kind="secondary">
|
|
85
|
+
{t("cancel", "Cancel")}
|
|
86
|
+
</Button>
|
|
87
|
+
<Button disabled={!sectionTitle} onClick={handleUpdatePageSections}>
|
|
88
|
+
<span>{t("save", "Save")}</span>
|
|
89
|
+
</Button>
|
|
90
|
+
</ModalFooter>
|
|
91
|
+
</ComposedModal>
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
export default SectionModal;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { useTranslation } from "react-i18next";
|
|
3
|
+
import { Button, TextInput } from "@carbon/react";
|
|
4
|
+
import { Close, Save } from "@carbon/react/icons";
|
|
5
|
+
import styles from "./value-editor.scss";
|
|
6
|
+
|
|
7
|
+
type ValueEditorProps = {
|
|
8
|
+
id: string;
|
|
9
|
+
handleCancel: () => void;
|
|
10
|
+
handleSave: (value: string) => void;
|
|
11
|
+
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
|
|
12
|
+
value: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const ValueEditor: React.FC<ValueEditorProps> = ({
|
|
16
|
+
id,
|
|
17
|
+
handleCancel,
|
|
18
|
+
handleSave,
|
|
19
|
+
onChange,
|
|
20
|
+
value,
|
|
21
|
+
}) => {
|
|
22
|
+
const { t } = useTranslation();
|
|
23
|
+
const [tmpValue, setTmpValue] = useState(value);
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<>
|
|
27
|
+
<TextInput
|
|
28
|
+
id={id}
|
|
29
|
+
labelText=""
|
|
30
|
+
value={tmpValue}
|
|
31
|
+
onChange={(event) => setTmpValue(event.target.value)}
|
|
32
|
+
/>
|
|
33
|
+
<div className={styles.actionButtons}>
|
|
34
|
+
<Button
|
|
35
|
+
size="md"
|
|
36
|
+
renderIcon={(props) => <Save {...props} size={16} />}
|
|
37
|
+
kind="primary"
|
|
38
|
+
onClick={() => handleSave(tmpValue)}
|
|
39
|
+
>
|
|
40
|
+
{t("saveButtonText", "Save")}
|
|
41
|
+
</Button>
|
|
42
|
+
<Button
|
|
43
|
+
size="md"
|
|
44
|
+
renderIcon={(props) => <Close {...props} size={16} />}
|
|
45
|
+
kind="secondary"
|
|
46
|
+
onClick={handleCancel}
|
|
47
|
+
>
|
|
48
|
+
{t("cancelButtonText", "Cancel")}
|
|
49
|
+
</Button>
|
|
50
|
+
</div>
|
|
51
|
+
</>
|
|
52
|
+
);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export default ValueEditor;
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import React, { useCallback, useState } from "react";
|
|
2
|
+
import { useTranslation } from "react-i18next";
|
|
3
|
+
import { useParams } from "react-router-dom";
|
|
4
|
+
import {
|
|
5
|
+
Button,
|
|
6
|
+
ComposedModal,
|
|
7
|
+
Form,
|
|
8
|
+
FormGroup,
|
|
9
|
+
InlineLoading,
|
|
10
|
+
ModalBody,
|
|
11
|
+
ModalFooter,
|
|
12
|
+
ModalHeader,
|
|
13
|
+
Select,
|
|
14
|
+
SelectItem,
|
|
15
|
+
Stack,
|
|
16
|
+
TextArea,
|
|
17
|
+
TextInput,
|
|
18
|
+
} from "@carbon/react";
|
|
19
|
+
import { showNotification, showToast } from "@openmrs/esm-framework";
|
|
20
|
+
import {
|
|
21
|
+
uploadSchema,
|
|
22
|
+
saveNewForm,
|
|
23
|
+
updateName,
|
|
24
|
+
updateVersion,
|
|
25
|
+
updateDescription,
|
|
26
|
+
getResourceUUID,
|
|
27
|
+
deleteClobData,
|
|
28
|
+
deleteResource,
|
|
29
|
+
updateEncounterType,
|
|
30
|
+
} from "../../forms.resource";
|
|
31
|
+
import { EncounterType, Resource, RouteParams, Schema } from "../../types";
|
|
32
|
+
import { useEncounterTypes } from "../../hooks/useEncounterTypes";
|
|
33
|
+
import styles from "./save-form.scss";
|
|
34
|
+
|
|
35
|
+
type FormGroupData = {
|
|
36
|
+
name: string;
|
|
37
|
+
uuid: string;
|
|
38
|
+
version: string;
|
|
39
|
+
encounterType: EncounterType;
|
|
40
|
+
description: string;
|
|
41
|
+
resources: Array<Resource>;
|
|
42
|
+
};
|
|
43
|
+
type SaveFormModalProps = { form: FormGroupData; schema: Schema };
|
|
44
|
+
|
|
45
|
+
const SaveForm: React.FC<SaveFormModalProps> = ({ form, schema }) => {
|
|
46
|
+
const { t } = useTranslation();
|
|
47
|
+
const { formUuid } = useParams<RouteParams>();
|
|
48
|
+
const isSavingNewForm = !formUuid;
|
|
49
|
+
const { encounterTypes } = useEncounterTypes();
|
|
50
|
+
const [openSaveFormModal, setOpenSaveFormModal] = useState(false);
|
|
51
|
+
const [openConfirmSaveModal, setOpenConfirmSaveModal] = useState(false);
|
|
52
|
+
const [saveState, setSaveState] = useState("");
|
|
53
|
+
const [isSavingForm, setIsSavingForm] = useState(false);
|
|
54
|
+
|
|
55
|
+
const openModal = useCallback((option) => {
|
|
56
|
+
if (option === "newVersion") {
|
|
57
|
+
setSaveState("newVersion");
|
|
58
|
+
setOpenConfirmSaveModal(false);
|
|
59
|
+
setOpenSaveFormModal(true);
|
|
60
|
+
} else if (option === "new") {
|
|
61
|
+
setSaveState("newVersion");
|
|
62
|
+
setOpenSaveFormModal(true);
|
|
63
|
+
} else if (option === "update") {
|
|
64
|
+
setSaveState("update");
|
|
65
|
+
setOpenConfirmSaveModal(false);
|
|
66
|
+
setOpenSaveFormModal(true);
|
|
67
|
+
}
|
|
68
|
+
}, []);
|
|
69
|
+
|
|
70
|
+
const handleSubmit = async (event) => {
|
|
71
|
+
event.preventDefault();
|
|
72
|
+
setIsSavingForm(true);
|
|
73
|
+
let name = event.target.name.value,
|
|
74
|
+
version = event.target.version.value,
|
|
75
|
+
encounterType = event.target.encounterType.value,
|
|
76
|
+
description = event.target.description.value,
|
|
77
|
+
encounterTypeUUID;
|
|
78
|
+
|
|
79
|
+
if (encounterType == "undefined") {
|
|
80
|
+
encounterTypeUUID = undefined;
|
|
81
|
+
} else {
|
|
82
|
+
encounterTypes.forEach((encType) => {
|
|
83
|
+
if (encounterType == encType.name) {
|
|
84
|
+
encounterTypeUUID = encType.uuid;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (saveState === "new" || saveState === "newVersion") {
|
|
90
|
+
try {
|
|
91
|
+
const newForm = await saveNewForm(
|
|
92
|
+
name,
|
|
93
|
+
version,
|
|
94
|
+
false,
|
|
95
|
+
description,
|
|
96
|
+
encounterTypeUUID
|
|
97
|
+
);
|
|
98
|
+
const newValueReference = await uploadSchema(schema);
|
|
99
|
+
await getResourceUUID(newForm.uuid, newValueReference.toString());
|
|
100
|
+
showToast({
|
|
101
|
+
title: t("formCreated", "New form created"),
|
|
102
|
+
kind: "success",
|
|
103
|
+
critical: true,
|
|
104
|
+
description:
|
|
105
|
+
name +
|
|
106
|
+
" " +
|
|
107
|
+
t(
|
|
108
|
+
"saveSuccessMessage",
|
|
109
|
+
"was created successfully. It is now visible on the Forms dashboard."
|
|
110
|
+
),
|
|
111
|
+
});
|
|
112
|
+
setOpenSaveFormModal(false);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
showNotification({
|
|
115
|
+
title: t("errorCreatingForm", "Error creating form"),
|
|
116
|
+
kind: "error",
|
|
117
|
+
critical: true,
|
|
118
|
+
description: error?.message,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
try {
|
|
123
|
+
if (form?.resources.length != 0) {
|
|
124
|
+
deleteClobData(form?.resources[0].valueReference);
|
|
125
|
+
deleteResource(form?.uuid, form?.resources[0].uuid);
|
|
126
|
+
}
|
|
127
|
+
const newValueReference = await uploadSchema(schema);
|
|
128
|
+
await getResourceUUID(form?.uuid, newValueReference.toString());
|
|
129
|
+
|
|
130
|
+
if (name !== form?.name) {
|
|
131
|
+
await updateName(name, form?.uuid);
|
|
132
|
+
}
|
|
133
|
+
if (version !== form?.version) {
|
|
134
|
+
await updateVersion(version, form?.uuid);
|
|
135
|
+
}
|
|
136
|
+
if (encounterTypeUUID !== form?.encounterType?.uuid) {
|
|
137
|
+
await updateEncounterType(encounterTypeUUID, form?.uuid);
|
|
138
|
+
}
|
|
139
|
+
if (description !== form?.description) {
|
|
140
|
+
await updateDescription(description, form?.uuid);
|
|
141
|
+
}
|
|
142
|
+
showToast({
|
|
143
|
+
title: t("success", "Success!"),
|
|
144
|
+
kind: "success",
|
|
145
|
+
critical: true,
|
|
146
|
+
description:
|
|
147
|
+
name + " " + t("saveSuccess", "was updated successfully"),
|
|
148
|
+
});
|
|
149
|
+
setOpenSaveFormModal(false);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
showNotification({
|
|
152
|
+
title: t("errorUpdatingForm", "Error updating form"),
|
|
153
|
+
kind: "error",
|
|
154
|
+
critical: true,
|
|
155
|
+
description: error?.message,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<div>
|
|
163
|
+
{!isSavingNewForm ? (
|
|
164
|
+
<ComposedModal
|
|
165
|
+
open={openConfirmSaveModal}
|
|
166
|
+
onClose={() => setOpenConfirmSaveModal(false)}
|
|
167
|
+
>
|
|
168
|
+
<ModalHeader title={t("saveConfirmation", "Save or Update form")} />
|
|
169
|
+
<ModalBody>
|
|
170
|
+
<p>
|
|
171
|
+
{t(
|
|
172
|
+
"saveAsModal",
|
|
173
|
+
"A version of the form you're working on already exists on the server. Do you want to update the form or to save it as a new version?"
|
|
174
|
+
)}
|
|
175
|
+
</p>
|
|
176
|
+
</ModalBody>
|
|
177
|
+
<ModalFooter>
|
|
178
|
+
<Button kind={"tertiary"} onClick={() => openModal("update")}>
|
|
179
|
+
{t("updateExistingForm", "Update existing version")}
|
|
180
|
+
</Button>
|
|
181
|
+
<Button kind={"primary"} onClick={() => openModal("newVersion")}>
|
|
182
|
+
{t("saveAsNewForm", "Save as a new")}
|
|
183
|
+
</Button>
|
|
184
|
+
<Button
|
|
185
|
+
kind={"secondary"}
|
|
186
|
+
onClick={() => setOpenConfirmSaveModal(false)}
|
|
187
|
+
>
|
|
188
|
+
{t("close", "Close")}
|
|
189
|
+
</Button>
|
|
190
|
+
</ModalFooter>
|
|
191
|
+
</ComposedModal>
|
|
192
|
+
) : null}
|
|
193
|
+
|
|
194
|
+
<ComposedModal
|
|
195
|
+
open={openSaveFormModal}
|
|
196
|
+
onClose={() => setOpenSaveFormModal(false)}
|
|
197
|
+
>
|
|
198
|
+
<ModalHeader
|
|
199
|
+
title={t("saveFormToServer", "Save form to server")}
|
|
200
|
+
></ModalHeader>
|
|
201
|
+
<Form onSubmit={handleSubmit}>
|
|
202
|
+
<ModalBody>
|
|
203
|
+
<p>
|
|
204
|
+
{t(
|
|
205
|
+
"saveExplainerText",
|
|
206
|
+
"Clicking the Save button saves your form schema to the database. To see your form in your frontend, you first need to publish it. Click the Publish button to publish your form."
|
|
207
|
+
)}
|
|
208
|
+
</p>
|
|
209
|
+
<FormGroup legendText={""}>
|
|
210
|
+
<Stack gap={5}>
|
|
211
|
+
<TextInput
|
|
212
|
+
id="name"
|
|
213
|
+
labelText={t("formName", "Form name")}
|
|
214
|
+
defaultValue={saveState === "update" ? form?.name : ""}
|
|
215
|
+
placeholder="e.g. OHRI Express Care Patient Encounter Form"
|
|
216
|
+
required
|
|
217
|
+
/>
|
|
218
|
+
{saveState === "update" ? (
|
|
219
|
+
<TextInput
|
|
220
|
+
id="uuid"
|
|
221
|
+
labelText="UUID (auto-generated)"
|
|
222
|
+
disabled
|
|
223
|
+
defaultValue={saveState === "update" ? form?.uuid : ""}
|
|
224
|
+
/>
|
|
225
|
+
) : null}
|
|
226
|
+
<TextInput
|
|
227
|
+
id="version"
|
|
228
|
+
labelText="Version"
|
|
229
|
+
defaultValue={saveState === "update" ? form?.version : ""}
|
|
230
|
+
placeholder="e.g. 1.0"
|
|
231
|
+
required
|
|
232
|
+
/>
|
|
233
|
+
<Select
|
|
234
|
+
id="encounterType"
|
|
235
|
+
defaultValue={
|
|
236
|
+
form?.encounterType
|
|
237
|
+
? form?.encounterType?.name
|
|
238
|
+
: "undefined"
|
|
239
|
+
}
|
|
240
|
+
labelText={t("encounterType", "Encounter Type")}
|
|
241
|
+
required
|
|
242
|
+
>
|
|
243
|
+
{!form?.encounterType ? (
|
|
244
|
+
<SelectItem
|
|
245
|
+
text={t(
|
|
246
|
+
"chooseEncounterType",
|
|
247
|
+
"Choose an encounter type to link your form to"
|
|
248
|
+
)}
|
|
249
|
+
/>
|
|
250
|
+
) : null}
|
|
251
|
+
{encounterTypes?.map((encounterType, key) => (
|
|
252
|
+
<SelectItem
|
|
253
|
+
key={key}
|
|
254
|
+
value={encounterType.name}
|
|
255
|
+
text={encounterType.name}
|
|
256
|
+
/>
|
|
257
|
+
))}
|
|
258
|
+
</Select>
|
|
259
|
+
<TextArea
|
|
260
|
+
labelText={t("description", "Description")}
|
|
261
|
+
defaultValue={saveState === "update" ? form?.description : ""}
|
|
262
|
+
cols={6}
|
|
263
|
+
rows={3}
|
|
264
|
+
id="description"
|
|
265
|
+
placeholder={t(
|
|
266
|
+
"descriptionPlaceholderText",
|
|
267
|
+
"e.g. A form used to collect encounter data for clients in the Express Care program."
|
|
268
|
+
)}
|
|
269
|
+
required
|
|
270
|
+
/>
|
|
271
|
+
</Stack>
|
|
272
|
+
</FormGroup>
|
|
273
|
+
</ModalBody>
|
|
274
|
+
<ModalFooter>
|
|
275
|
+
<Button
|
|
276
|
+
kind={"secondary"}
|
|
277
|
+
onClick={() => setOpenSaveFormModal(false)}
|
|
278
|
+
>
|
|
279
|
+
{t("close", "Close")}
|
|
280
|
+
</Button>
|
|
281
|
+
<Button
|
|
282
|
+
disabled={isSavingForm}
|
|
283
|
+
className={styles.spinner}
|
|
284
|
+
type={"submit"}
|
|
285
|
+
kind={"primary"}
|
|
286
|
+
>
|
|
287
|
+
{isSavingForm ? (
|
|
288
|
+
<InlineLoading description={t("saving", "Saving") + "..."} />
|
|
289
|
+
) : (
|
|
290
|
+
<span>{t("save", "Save")}</span>
|
|
291
|
+
)}
|
|
292
|
+
</Button>
|
|
293
|
+
</ModalFooter>
|
|
294
|
+
</Form>
|
|
295
|
+
</ComposedModal>
|
|
296
|
+
|
|
297
|
+
<Button
|
|
298
|
+
disabled={!schema}
|
|
299
|
+
kind="primary"
|
|
300
|
+
onClick={() =>
|
|
301
|
+
isSavingNewForm ? openModal("new") : setOpenConfirmSaveModal(true)
|
|
302
|
+
}
|
|
303
|
+
>
|
|
304
|
+
{t("saveForm", "Save form")}
|
|
305
|
+
</Button>
|
|
306
|
+
</div>
|
|
307
|
+
);
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
export default SaveForm;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { useParams } from "react-router-dom";
|
|
3
|
+
import AceEditor from "react-ace";
|
|
4
|
+
import "ace-builds/webpack-resolver";
|
|
5
|
+
import { Button, InlineLoading } from "@carbon/react";
|
|
6
|
+
import { useTranslation } from "react-i18next";
|
|
7
|
+
import { OHRIFormSchema } from "@ohri/openmrs-ohri-form-engine-lib";
|
|
8
|
+
import { RouteParams, Schema } from "../../types";
|
|
9
|
+
import styles from "./schema-editor.scss";
|
|
10
|
+
|
|
11
|
+
type SchemaEditorProps = {
|
|
12
|
+
isLoading: boolean;
|
|
13
|
+
onSchemaChange: (schema: Schema) => void;
|
|
14
|
+
schema: Schema;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const SchemaEditor: React.FC<SchemaEditorProps> = ({
|
|
18
|
+
isLoading,
|
|
19
|
+
onSchemaChange,
|
|
20
|
+
schema,
|
|
21
|
+
}) => {
|
|
22
|
+
const { t } = useTranslation();
|
|
23
|
+
const { formUuid } = useParams<RouteParams>();
|
|
24
|
+
const isNewSchema = !formUuid;
|
|
25
|
+
|
|
26
|
+
const [stringifiedSchema, setStringifiedSchema] = useState(
|
|
27
|
+
schema ? JSON.stringify(schema, null, 2) : ""
|
|
28
|
+
);
|
|
29
|
+
const [invalidJsonErrorMessage, setInvalidJsonErrorMessage] = useState("");
|
|
30
|
+
const [isRendering, setIsRendering] = useState(false);
|
|
31
|
+
|
|
32
|
+
const resetErrorMessage = useCallback(() => {
|
|
33
|
+
setInvalidJsonErrorMessage("");
|
|
34
|
+
}, []);
|
|
35
|
+
|
|
36
|
+
const handleSchemaChange = (updatedSchema: string) => {
|
|
37
|
+
setStringifiedSchema(updatedSchema);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const inputDummySchema = useCallback(() => {
|
|
41
|
+
const dummySchema: OHRIFormSchema = {
|
|
42
|
+
encounterType: "",
|
|
43
|
+
name: "Sample Form",
|
|
44
|
+
pages: [
|
|
45
|
+
{
|
|
46
|
+
label: "First Page",
|
|
47
|
+
sections: [
|
|
48
|
+
{
|
|
49
|
+
label: "A Section",
|
|
50
|
+
isExpanded: "true",
|
|
51
|
+
questions: [
|
|
52
|
+
{
|
|
53
|
+
label: "A Question of type obs that renders a text input",
|
|
54
|
+
type: "obs",
|
|
55
|
+
questionOptions: {
|
|
56
|
+
rendering: "text",
|
|
57
|
+
concept: "a-system-defined-concept-uuid",
|
|
58
|
+
},
|
|
59
|
+
id: "sampleQuestion",
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
label: "Another Section",
|
|
65
|
+
isExpanded: "true",
|
|
66
|
+
questions: [
|
|
67
|
+
{
|
|
68
|
+
label:
|
|
69
|
+
"Another Question of type obs whose answers get rendered as radio inputs",
|
|
70
|
+
type: "obs",
|
|
71
|
+
questionOptions: {
|
|
72
|
+
rendering: "radio",
|
|
73
|
+
concept: "system-defined-concept-uuid",
|
|
74
|
+
answers: [
|
|
75
|
+
{
|
|
76
|
+
concept: "another-system-defined-concept-uuid",
|
|
77
|
+
label: "Choice 1",
|
|
78
|
+
conceptMappings: [],
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
concept: "yet-another-system-defined-concept-uuid",
|
|
82
|
+
label: "Choice 2",
|
|
83
|
+
conceptMappings: [],
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
concept: "yet-one-more-system-defined-concept-uuid",
|
|
87
|
+
label: "Choice 3",
|
|
88
|
+
conceptMappings: [],
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
id: "anotherSampleQuestion",
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
processor: "EncounterFormProcessor",
|
|
100
|
+
referencedForms: [],
|
|
101
|
+
uuid: "xxx",
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
setStringifiedSchema(JSON.stringify(dummySchema, null, 2));
|
|
105
|
+
onSchemaChange({ ...dummySchema });
|
|
106
|
+
}, [onSchemaChange]);
|
|
107
|
+
|
|
108
|
+
const renderSchemaChanges = useCallback(() => {
|
|
109
|
+
setIsRendering(true);
|
|
110
|
+
resetErrorMessage();
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
const parsedJson: Schema = JSON.parse(stringifiedSchema);
|
|
114
|
+
onSchemaChange(parsedJson);
|
|
115
|
+
setStringifiedSchema(JSON.stringify(parsedJson, null, 2));
|
|
116
|
+
} catch (error) {
|
|
117
|
+
setInvalidJsonErrorMessage(error.message);
|
|
118
|
+
}
|
|
119
|
+
setIsRendering(false);
|
|
120
|
+
}, [stringifiedSchema, onSchemaChange, resetErrorMessage]);
|
|
121
|
+
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
setStringifiedSchema(JSON.stringify(schema, null, 2));
|
|
124
|
+
}, [schema]);
|
|
125
|
+
|
|
126
|
+
return (
|
|
127
|
+
<>
|
|
128
|
+
<div className={styles.actionButtons}>
|
|
129
|
+
{isLoading ? (
|
|
130
|
+
<InlineLoading
|
|
131
|
+
description={t("loadingSchema", "Loading schema") + "..."}
|
|
132
|
+
/>
|
|
133
|
+
) : null}
|
|
134
|
+
|
|
135
|
+
{isNewSchema ? (
|
|
136
|
+
<Button kind="secondary" onClick={inputDummySchema}>
|
|
137
|
+
{t("inputDummySchema", "Input dummy schema")}
|
|
138
|
+
</Button>
|
|
139
|
+
) : null}
|
|
140
|
+
|
|
141
|
+
<Button
|
|
142
|
+
disabled={isRendering}
|
|
143
|
+
kind="primary"
|
|
144
|
+
onClick={renderSchemaChanges}
|
|
145
|
+
>
|
|
146
|
+
{isRendering ? (
|
|
147
|
+
<InlineLoading
|
|
148
|
+
className={styles.spinner}
|
|
149
|
+
description={t("rendering", "Rendering") + "..."}
|
|
150
|
+
/>
|
|
151
|
+
) : (
|
|
152
|
+
<span>{t("renderChanges", "Render changes")}</span>
|
|
153
|
+
)}
|
|
154
|
+
</Button>
|
|
155
|
+
</div>
|
|
156
|
+
|
|
157
|
+
{invalidJsonErrorMessage ? (
|
|
158
|
+
<div className={styles.errorMessage}>
|
|
159
|
+
<p className={styles.heading}>
|
|
160
|
+
{t("schemaError", "There's an error in your schema.")}
|
|
161
|
+
</p>
|
|
162
|
+
<p>{invalidJsonErrorMessage}</p>
|
|
163
|
+
</div>
|
|
164
|
+
) : null}
|
|
165
|
+
|
|
166
|
+
<AceEditor
|
|
167
|
+
style={{ height: "100vh", width: "100%" }}
|
|
168
|
+
mode="json"
|
|
169
|
+
theme="textmate"
|
|
170
|
+
name="schemaEditor"
|
|
171
|
+
onChange={handleSchemaChange}
|
|
172
|
+
fontSize={15}
|
|
173
|
+
showPrintMargin={false}
|
|
174
|
+
showGutter={true}
|
|
175
|
+
highlightActiveLine={true}
|
|
176
|
+
value={stringifiedSchema}
|
|
177
|
+
setOptions={{
|
|
178
|
+
enableBasicAutocompletion: false,
|
|
179
|
+
enableLiveAutocompletion: false,
|
|
180
|
+
displayIndentGuides: false,
|
|
181
|
+
enableSnippets: false,
|
|
182
|
+
showLineNumbers: true,
|
|
183
|
+
tabSize: 2,
|
|
184
|
+
}}
|
|
185
|
+
/>
|
|
186
|
+
</>
|
|
187
|
+
);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export default SchemaEditor;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
@use '@carbon/styles/scss/colors';
|
|
2
|
+
@use '@carbon/styles/scss/type';
|
|
3
|
+
|
|
4
|
+
.actionButtons {
|
|
5
|
+
display: flex;
|
|
6
|
+
align-items: center;
|
|
7
|
+
justify-content: flex-end;
|
|
8
|
+
margin: 1rem 0;
|
|
9
|
+
|
|
10
|
+
button {
|
|
11
|
+
margin-left: 1rem
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.errorContainer {
|
|
16
|
+
@include type.type-style("body-compact-02");
|
|
17
|
+
background-color: colors.$red-20;
|
|
18
|
+
color: colors.$red-70;
|
|
19
|
+
padding: 1.5rem;
|
|
20
|
+
margin: 1rem 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.errorMessage {
|
|
24
|
+
@include type.type-style('body-compact-01');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.heading {
|
|
28
|
+
@include type.type-style('heading-compact-02');
|
|
29
|
+
margin-bottom: 1rem;
|
|
30
|
+
}
|