@openmrs/esm-fast-data-entry-app 1.0.1-pre.17 → 1.0.1-pre.21

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.
Files changed (32) hide show
  1. package/dist/132.js +1 -1
  2. package/dist/229.js +1 -0
  3. package/dist/255.js +1 -1
  4. package/dist/32.js +1 -1
  5. package/dist/574.js +1 -1
  6. package/dist/617.js +1 -1
  7. package/dist/640.js +1 -0
  8. package/dist/804.js +1 -1
  9. package/dist/main.js +1 -1
  10. package/dist/openmrs-esm-fast-data-entry-app.js +1 -1
  11. package/dist/openmrs-esm-fast-data-entry-app.js.buildmanifest.json +65 -42
  12. package/dist/openmrs-esm-fast-data-entry-app.old +1 -1
  13. package/package.json +3 -3
  14. package/src/CancelModal.tsx +48 -0
  15. package/src/CompleteModal.tsx +46 -0
  16. package/src/FormBootstrap.tsx +3 -0
  17. package/src/context/FormWorkflowReducer.ts +4 -0
  18. package/src/context/GroupFormWorkflowContext.tsx +31 -2
  19. package/src/context/GroupFormWorkflowReducer.ts +127 -1
  20. package/src/form-entry-workflow/FormEntryWorkflow.tsx +15 -82
  21. package/src/group-form-entry-workflow/GroupFormEntryWorkflow.tsx +11 -398
  22. package/src/group-form-entry-workflow/GroupSessionWorkspace.tsx +227 -0
  23. package/src/group-form-entry-workflow/SessionDetailsForm.tsx +122 -0
  24. package/src/group-form-entry-workflow/SessionMetaWorkspace.tsx +107 -0
  25. package/src/group-form-entry-workflow/attendance-table/AttendanceTable.tsx +105 -0
  26. package/src/group-form-entry-workflow/attendance-table/index.ts +1 -0
  27. package/src/group-form-entry-workflow/group-display-header/GroupDisplayHeader.tsx +1 -9
  28. package/src/group-form-entry-workflow/group-search-header/GroupSearchHeader.tsx +0 -3
  29. package/src/hooks/useGetSystemSetting.ts +38 -0
  30. package/src/hooks/useStartVisit.ts +83 -0
  31. package/translations/en.json +7 -1
  32. package/dist/906.js +0 -1
@@ -1,413 +1,26 @@
1
- import {
2
- ExtensionSlot,
3
- getGlobalStore,
4
- useStore,
5
- } from "@openmrs/esm-framework";
6
- import {
7
- Button,
8
- ComposedModal,
9
- ModalBody,
10
- ModalFooter,
11
- ModalHeader,
12
- Layer,
13
- Tile,
14
- TextInput,
15
- TextArea,
16
- DatePicker,
17
- DatePickerInput,
18
- } from "@carbon/react";
19
- import React, { useContext, useEffect, useState } from "react";
20
- import { useNavigate } from "react-router-dom";
21
- import PatientCard from "../patient-card/PatientCard";
1
+ import { ExtensionSlot } from "@openmrs/esm-framework";
2
+ import React from "react";
22
3
  import GroupDisplayHeader from "./group-display-header";
23
4
  import styles from "./styles.scss";
24
- import { useTranslation } from "react-i18next";
25
- import GroupFormWorkflowContext, {
26
- GroupFormWorkflowProvider,
27
- } from "../context/GroupFormWorkflowContext";
5
+ import { GroupFormWorkflowProvider } from "../context/GroupFormWorkflowContext";
28
6
  import GroupSearchHeader from "./group-search-header";
29
- import {
30
- Controller,
31
- FormProvider,
32
- useForm,
33
- useFormContext,
34
- } from "react-hook-form";
35
- import FormBootstrap from "../FormBootstrap";
36
-
37
- const formStore = getGlobalStore("ampath-form-state");
38
-
39
- const CancelModal = ({ open, setOpen }) => {
40
- const { destroySession, closeSession } = useContext(GroupFormWorkflowContext);
41
- const { t } = useTranslation();
42
- const navigate = useNavigate();
43
-
44
- const discard = async () => {
45
- await destroySession();
46
- setOpen(false);
47
- navigate("../");
48
- };
49
-
50
- const saveAndClose = async () => {
51
- await closeSession();
52
- setOpen(false);
53
- navigate("../");
54
- };
55
-
56
- return (
57
- <ComposedModal open={open}>
58
- <ModalHeader>{t("areYouSure", "Are you sure?")}</ModalHeader>
59
- <ModalBody>
60
- {t(
61
- "cancelExplanation",
62
- "You will lose any unsaved changes on the current form. Do you want to discard the current session?"
63
- )}
64
- </ModalBody>
65
- <ModalFooter>
66
- <Button kind="secondary" onClick={() => setOpen(false)}>
67
- {t("cancel", "Cancel")}
68
- </Button>
69
- <Button kind="danger" onClick={discard}>
70
- {t("discard", "Discard")}
71
- </Button>
72
- <Button kind="primary" onClick={saveAndClose}>
73
- {t("saveSession", "Save Session")}
74
- </Button>
75
- </ModalFooter>
76
- </ComposedModal>
77
- );
78
- };
79
-
80
- const CompleteModal = ({ open, setOpen }) => {
81
- const { submitForComplete } = useContext(GroupFormWorkflowContext);
82
- const { t } = useTranslation();
83
-
84
- const completeSession = () => {
85
- submitForComplete();
86
- setOpen(false);
87
- };
88
-
89
- return (
90
- <ComposedModal open={open}>
91
- <ModalHeader>{t("areYouSure", "Are you sure?")}</ModalHeader>
92
- <ModalBody>
93
- {t(
94
- "saveExplanation",
95
- "Do you want to save the current form and exit the workflow?"
96
- )}
97
- </ModalBody>
98
- <ModalFooter>
99
- <Button kind="secondary" onClick={() => setOpen(false)}>
100
- {t("cancel", "Cancel")}
101
- </Button>
102
- <Button kind="primary" onClick={completeSession}>
103
- {t("complete", "Complete")}
104
- </Button>
105
- </ModalFooter>
106
- </ComposedModal>
107
- );
108
- };
109
-
110
- const NewGroupWorkflowButtons = () => {
111
- const { t } = useTranslation();
112
- const { workflowState } = useContext(GroupFormWorkflowContext);
113
- const [cancelModalOpen, setCancelModalOpen] = useState(false);
114
- if (workflowState !== "NEW_GROUP_SESSION") return null;
115
-
116
- return (
117
- <>
118
- <div className={styles.rightPanelActionButtons}>
119
- <Button kind="secondary" type="submit">
120
- {t("createNewSession", "Create New Session")}
121
- </Button>
122
- <Button
123
- kind="tertiary"
124
- onClick={() => {
125
- setCancelModalOpen(true);
126
- }}
127
- >
128
- {t("cancel", "Cancel")}
129
- </Button>
130
- </div>
131
- <CancelModal open={cancelModalOpen} setOpen={setCancelModalOpen} />
132
- </>
133
- );
134
- };
135
-
136
- const WorkflowNavigationButtons = () => {
137
- const { activeFormUuid, submitForNext, patientUuids, activePatientUuid } =
138
- useContext(GroupFormWorkflowContext);
139
- const store = useStore(formStore);
140
- const formState = store[activeFormUuid];
141
- const navigationDisabled = formState !== "ready";
142
- const [cancelModalOpen, setCancelModalOpen] = useState(false);
143
- const [completeModalOpen, setCompleteModalOpen] = useState(false);
144
- const { t } = useTranslation();
145
-
146
- const isLastPatient =
147
- activePatientUuid === patientUuids[patientUuids.length - 1];
148
-
149
- return (
150
- <>
151
- <div className={styles.rightPanelActionButtons}>
152
- <Button
153
- kind="primary"
154
- onClick={() => submitForNext()}
155
- disabled={navigationDisabled}
156
- >
157
- {isLastPatient
158
- ? t("saveForm", "Save Form")
159
- : t("nextPatient", "Next Patient")}
160
- </Button>
161
- <Button kind="secondary" onClick={() => setCompleteModalOpen(true)}>
162
- {t("saveAndComplete", "Save & Complete")}
163
- </Button>
164
- <Button kind="tertiary" onClick={() => setCancelModalOpen(true)}>
165
- {t("cancel", "Cancel")}
166
- </Button>
167
- </div>
168
- <CancelModal open={cancelModalOpen} setOpen={setCancelModalOpen} />
169
- <CompleteModal open={completeModalOpen} setOpen={setCompleteModalOpen} />
170
- </>
171
- );
172
- };
173
-
174
- const SessionDetails = () => {
175
- const { t } = useTranslation();
176
- const {
177
- register,
178
- formState: { errors },
179
- control,
180
- } = useFormContext();
181
-
182
- return (
183
- <div className={styles.formSection}>
184
- <h4>{t("sessionDetails", "Session details")}</h4>
185
- <div>
186
- <p>
187
- {t(
188
- "allFieldsRequired",
189
- "All fields are required unless marked optional"
190
- )}
191
- </p>
192
- </div>
193
- <Layer>
194
- <Tile className={styles.formSectionTile}>
195
- <Layer>
196
- <div
197
- style={{
198
- display: "flex",
199
- flexDirection: "column",
200
- rowGap: "1.5rem",
201
- }}
202
- >
203
- <TextInput
204
- id="text"
205
- type="text"
206
- labelText={t("sessionName", "Session Name")}
207
- {...register("sessionName", { required: true })}
208
- invalid={errors.sessionName}
209
- invalidText={"This field is required"}
210
- />
211
- <TextInput
212
- id="text"
213
- type="text"
214
- labelText={t("practitionerName", "Practitioner Name")}
215
- {...register("practitionerName", { required: true })}
216
- invalid={errors.practitionerName}
217
- invalidText={"This field is required"}
218
- />
219
- <Controller
220
- name="sessionDate"
221
- control={control}
222
- rules={{ required: true }}
223
- render={({ field }) => (
224
- <DatePicker
225
- datePickerType="single"
226
- size="md"
227
- maxDate={new Date()}
228
- {...field}
229
- >
230
- <DatePickerInput
231
- id="session-date"
232
- labelText={t("sessionDate", "Session Date")}
233
- placeholder="mm/dd/yyyy"
234
- size="md"
235
- invalid={errors.sessionDate}
236
- invalidText={"This field is required"}
237
- />
238
- </DatePicker>
239
- )}
240
- />
241
- <TextArea
242
- id="text"
243
- type="text"
244
- labelText={t("sessionNotes", "Session Notes")}
245
- {...register("sessionNotes", { required: true })}
246
- invalid={errors.sessionNotes}
247
- invalidText={"This field is required"}
248
- />
249
- </div>
250
- </Layer>
251
- </Tile>
252
- </Layer>
253
- </div>
254
- );
255
- };
256
-
257
- const GroupIdField = () => {
258
- const { t } = useTranslation();
259
- const {
260
- register,
261
- formState: { errors },
262
- setValue,
263
- } = useFormContext();
264
- const { activeGroupUuid } = useContext(GroupFormWorkflowContext);
265
-
266
- useEffect(() => {
267
- if (activeGroupUuid) setValue("groupUuid", activeGroupUuid);
268
- }, [activeGroupUuid, setValue]);
269
-
270
- return (
271
- <>
272
- <input
273
- hidden
274
- {...register("groupUuid", {
275
- value: activeGroupUuid,
276
- required: t("chooseGroupError", "Please choose a group."),
277
- })}
278
- />
279
- {errors.groupUuid && !activeGroupUuid && (
280
- <div className={styles.formError}>
281
- {errors.groupUuid.message as string}
282
- </div>
283
- )}
284
- </>
285
- );
286
- };
287
-
288
- const SessionMetaWorkspace = () => {
289
- const { t } = useTranslation();
290
- const { setSessionMeta } = useContext(GroupFormWorkflowContext);
291
- const methods = useForm();
292
-
293
- const onSubmit = (data) => {
294
- const { sessionDate, ...rest } = data;
295
- setSessionMeta({ ...rest, sessionDate: sessionDate[0] });
296
- };
297
-
298
- return (
299
- <FormProvider {...methods}>
300
- <form onSubmit={methods.handleSubmit(onSubmit)}>
301
- <div className={styles.workspace}>
302
- <div className={styles.formMainContent}>
303
- <div className={styles.formContainer}>
304
- <SessionDetails />
305
- </div>
306
- <div className={styles.rightPanel}>
307
- <h4>{t("newGroupSession", "New Group Session")}</h4>
308
- <GroupIdField />
309
- <hr style={{ width: "100%" }} />
310
- <NewGroupWorkflowButtons />
311
- </div>
312
- </div>
313
- </div>
314
- </form>
315
- </FormProvider>
316
- );
317
- };
318
-
319
- const GroupSessionWorkspace = () => {
320
- const { t } = useTranslation();
321
- const {
322
- patientUuids,
323
- activePatientUuid,
324
- editEncounter,
325
- encounters,
326
- activeEncounterUuid,
327
- activeFormUuid,
328
- saveEncounter,
329
- // activeSessionMeta,
330
- } = useContext(GroupFormWorkflowContext);
331
-
332
- // const handleEncounterCreate = (payload: Record<string, unknown>) => {
333
- // console.log("payload", payload);
334
- // Object.entries(activeSessionMeta).forEach((key, value) => {
335
- // payload[key as unknown as string] = value;
336
- // });
337
- // };
338
-
339
- const handlePostResponse = (encounter) => {
340
- if (encounter && encounter.uuid) {
341
- saveEncounter(encounter.uuid);
342
- }
343
- };
344
-
345
- return (
346
- <div className={styles.workspace}>
347
- <div className={styles.formMainContent}>
348
- <div className={styles.formContainer}>
349
- <FormBootstrap
350
- patientUuid={activePatientUuid}
351
- encounterUuid={activeEncounterUuid}
352
- {...{
353
- formUuid: activeFormUuid,
354
- handlePostResponse,
355
- // handleEncounterCreate,
356
- }}
357
- />
358
- </div>
359
- <div className={styles.rightPanel}>
360
- <h4>{t("formsFilled", "Forms filled")}</h4>
361
- <div className={styles.patientCardsSection}>
362
- {patientUuids?.map((patientUuid) => (
363
- <PatientCard
364
- key={patientUuid}
365
- {...{
366
- patientUuid,
367
- activePatientUuid,
368
- editEncounter,
369
- encounters,
370
- }}
371
- />
372
- ))}
373
- </div>
374
- <WorkflowNavigationButtons />
375
- </div>
376
- </div>
377
- </div>
378
- );
379
- };
7
+ import SessionMetaWorkspace from "./SessionMetaWorkspace";
8
+ import GroupSessionWorkspace from "./GroupSessionWorkspace";
380
9
 
381
10
  const GroupFormEntryWorkflow = () => {
382
- const { workflowState } = useContext(GroupFormWorkflowContext);
383
-
384
11
  return (
385
- <>
12
+ <GroupFormWorkflowProvider>
386
13
  <div className={styles.breadcrumbsContainer}>
387
14
  <ExtensionSlot extensionSlotName="breadcrumbs-slot" />
388
15
  </div>
389
16
  <GroupSearchHeader />
390
17
  <GroupDisplayHeader />
391
- {workflowState === "NEW_GROUP_SESSION" && (
392
- <div className={styles.workspaceWrapper}>
393
- <SessionMetaWorkspace />
394
- </div>
395
- )}
396
- {["EDIT_FORM"].includes(workflowState) && (
397
- <div className={styles.workspaceWrapper}>
398
- <GroupSessionWorkspace />
399
- </div>
400
- )}
401
- </>
402
- );
403
- };
404
-
405
- const GroupFormEntryWorkflowWrapper = () => {
406
- return (
407
- <GroupFormWorkflowProvider>
408
- <GroupFormEntryWorkflow />
18
+ <div className={styles.workspaceWrapper}>
19
+ <SessionMetaWorkspace />
20
+ <GroupSessionWorkspace />
21
+ </div>
409
22
  </GroupFormWorkflowProvider>
410
23
  );
411
24
  };
412
25
 
413
- export default GroupFormEntryWorkflowWrapper;
26
+ export default GroupFormEntryWorkflow;
@@ -0,0 +1,227 @@
1
+ import { getGlobalStore, useStore } from "@openmrs/esm-framework";
2
+ import { Button } from "@carbon/react";
3
+ import React, { useCallback, useContext, useEffect, useState } from "react";
4
+ import PatientCard from "../patient-card/PatientCard";
5
+ import styles from "./styles.scss";
6
+ import { useTranslation } from "react-i18next";
7
+ import GroupFormWorkflowContext from "../context/GroupFormWorkflowContext";
8
+ import FormBootstrap from "../FormBootstrap";
9
+ import useStartVisit from "../hooks/useStartVisit";
10
+ import CompleteModal from "../CompleteModal";
11
+ import CancelModal from "../CancelModal";
12
+
13
+ const formStore = getGlobalStore("ampath-form-state");
14
+
15
+ const WorkflowNavigationButtons = () => {
16
+ const context = useContext(GroupFormWorkflowContext);
17
+ const {
18
+ activeFormUuid,
19
+ validateForNext,
20
+ patientUuids,
21
+ activePatientUuid,
22
+ workflowState,
23
+ } = context;
24
+ const store = useStore(formStore);
25
+ const formState = store[activeFormUuid];
26
+ const navigationDisabled =
27
+ formState !== "ready" || workflowState !== "EDIT_FORM";
28
+ const [cancelModalOpen, setCancelModalOpen] = useState(false);
29
+ const [completeModalOpen, setCompleteModalOpen] = useState(false);
30
+ const { t } = useTranslation();
31
+
32
+ const isLastPatient =
33
+ activePatientUuid === patientUuids[patientUuids.length - 1];
34
+
35
+ const handleClickNext = () => {
36
+ if (workflowState === "EDIT_FORM") {
37
+ validateForNext();
38
+ }
39
+ };
40
+
41
+ return (
42
+ <>
43
+ <div className={styles.rightPanelActionButtons}>
44
+ <Button
45
+ kind="primary"
46
+ onClick={handleClickNext}
47
+ disabled={navigationDisabled}
48
+ >
49
+ {isLastPatient
50
+ ? t("saveForm", "Save Form")
51
+ : t("nextPatient", "Next Patient")}
52
+ </Button>
53
+ <Button kind="secondary" onClick={() => setCompleteModalOpen(true)}>
54
+ {t("saveAndComplete", "Save & Complete")}
55
+ </Button>
56
+ <Button kind="tertiary" onClick={() => setCancelModalOpen(true)}>
57
+ {t("cancel", "Cancel")}
58
+ </Button>
59
+ </div>
60
+ <CancelModal
61
+ open={cancelModalOpen}
62
+ setOpen={setCancelModalOpen}
63
+ context={context}
64
+ />
65
+ <CompleteModal
66
+ open={completeModalOpen}
67
+ setOpen={setCompleteModalOpen}
68
+ context={context}
69
+ validateFirst={true}
70
+ />
71
+ </>
72
+ );
73
+ };
74
+
75
+ const GroupSessionWorkspace = () => {
76
+ const { t } = useTranslation();
77
+ const {
78
+ patientUuids,
79
+ activePatientUuid,
80
+ editEncounter,
81
+ encounters,
82
+ activeEncounterUuid,
83
+ activeVisitUuid,
84
+ activeFormUuid,
85
+ saveEncounter,
86
+ activeSessionMeta,
87
+ groupVisitTypeUuid,
88
+ updateVisitUuid,
89
+ submitForNext,
90
+ workflowState,
91
+ submitForComplete,
92
+ } = useContext(GroupFormWorkflowContext);
93
+
94
+ const { saveVisit, success: visitSaveSuccess } = useStartVisit({
95
+ showSuccessNotification: false,
96
+ showErrorNotification: true,
97
+ });
98
+
99
+ // 0. user clicks "next patient" in WorkflowNavigationButtons
100
+ // which triggers validateForNext() if workflowState === "EDIT_FORM"
101
+
102
+ // 1. validate the form
103
+ // if the form is valid, save a visit for the user
104
+ // handleOnValidate is a callback passed to the form engine.
105
+ const handleOnValidate = useCallback(
106
+ (valid) => {
107
+ if (valid && !activeVisitUuid) {
108
+ // make a visit
109
+ // we will not persist this state or update the reducer
110
+ const date = new Date(activeSessionMeta.sessionDate);
111
+ date.setHours(date.getHours() + 1);
112
+ saveVisit({
113
+ patientUuid: activePatientUuid,
114
+ startDatetime: activeSessionMeta.sessionDate,
115
+ stopDatetime: date.toISOString(),
116
+ visitType: groupVisitTypeUuid,
117
+ });
118
+ } else if (valid && activeVisitUuid) {
119
+ // there is already a visit for this form
120
+ submitForNext();
121
+ }
122
+ },
123
+ [
124
+ activePatientUuid,
125
+ activeSessionMeta,
126
+ groupVisitTypeUuid,
127
+ saveVisit,
128
+ activeVisitUuid,
129
+ submitForNext,
130
+ ]
131
+ );
132
+
133
+ // 2. save the new visit uuid and start form submission
134
+ useEffect(() => {
135
+ if (visitSaveSuccess) {
136
+ const visitUuid = visitSaveSuccess?.data?.uuid;
137
+ if (!activeVisitUuid) {
138
+ updateVisitUuid(visitUuid);
139
+ }
140
+ if (workflowState === "VALIDATE_FOR_NEXT") {
141
+ submitForNext();
142
+ }
143
+ if (workflowState === "VALIDATE_FOR_COMPLETE") {
144
+ submitForComplete();
145
+ }
146
+ }
147
+ }, [
148
+ visitSaveSuccess,
149
+ updateVisitUuid,
150
+ submitForNext,
151
+ workflowState,
152
+ activeVisitUuid,
153
+ submitForComplete,
154
+ ]);
155
+
156
+ // 3. on form payload creation inject the activeVisitUuid
157
+ const handleEncounterCreate = useCallback(
158
+ (payload) => {
159
+ const obsTime = new Date(activeSessionMeta.sessionDate);
160
+ obsTime.setMinutes(obsTime.getMinutes() + 1);
161
+
162
+ payload.obs.forEach((item, index) => {
163
+ payload.obs[index] = {
164
+ ...item,
165
+ groupMembers: item.groupMembers?.map((mem) => ({
166
+ ...mem,
167
+ obsDatetime: obsTime.toISOString(),
168
+ })),
169
+ obsDatetime: obsTime.toISOString(),
170
+ };
171
+ });
172
+ payload.visit = activeVisitUuid;
173
+ payload.encounterDatetime = obsTime.toISOString();
174
+ },
175
+ [activeVisitUuid, activeSessionMeta]
176
+ );
177
+
178
+ // 4. Once form has been posted, save the new encounter uuid so we can edit it later
179
+ const handlePostResponse = useCallback(
180
+ (encounter) => {
181
+ if (encounter && encounter.uuid) {
182
+ saveEncounter(encounter.uuid);
183
+ }
184
+ },
185
+ [saveEncounter]
186
+ );
187
+
188
+ if (workflowState === "NEW_GROUP_SESSION") return null;
189
+
190
+ return (
191
+ <div className={styles.workspace}>
192
+ <div className={styles.formMainContent}>
193
+ <div className={styles.formContainer}>
194
+ <FormBootstrap
195
+ patientUuid={activePatientUuid}
196
+ encounterUuid={activeEncounterUuid}
197
+ {...{
198
+ formUuid: activeFormUuid,
199
+ handlePostResponse,
200
+ handleOnValidate,
201
+ handleEncounterCreate,
202
+ }}
203
+ />
204
+ </div>
205
+ <div className={styles.rightPanel}>
206
+ <h4>{t("formsFilled", "Forms filled")}</h4>
207
+ <div className={styles.patientCardsSection}>
208
+ {patientUuids?.map((patientUuid) => (
209
+ <PatientCard
210
+ key={patientUuid}
211
+ {...{
212
+ patientUuid,
213
+ activePatientUuid,
214
+ editEncounter,
215
+ encounters,
216
+ }}
217
+ />
218
+ ))}
219
+ </div>
220
+ <WorkflowNavigationButtons />
221
+ </div>
222
+ </div>
223
+ </div>
224
+ );
225
+ };
226
+
227
+ export default GroupSessionWorkspace;