@kenyaemr/esm-care-panel-app 5.4.2-pre.2774 → 5.4.2-pre.2782

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.
@@ -0,0 +1,108 @@
1
+ import { InlineLoading, OverflowMenuItem } from '@carbon/react';
2
+ import { launchWorkspace, showSnackbar, useConfig, Visit } from '@openmrs/esm-framework';
3
+ import { launchStartVisitPrompt } from '@openmrs/esm-patient-common-lib';
4
+ import React, { FC, useEffect, useMemo } from 'react';
5
+ import { useTranslation } from 'react-i18next';
6
+ import { CarePanelConfig } from '../config-schema';
7
+ import { useFormsFilled, usePatientFormEncounter } from './care-program.resource';
8
+ import KvpLinkPatientToPeerEducator from './link-patient-to-peer-action.component';
9
+
10
+ type ProgramFormOverflowMenuItemProps = {
11
+ patientUuid: string;
12
+ form: CarePanelConfig['careProgramForms'][0]['forms'][0];
13
+ mutate?: () => void;
14
+ visit?: Visit;
15
+ };
16
+
17
+ const ProgramFormOverflowMenuItem: FC<ProgramFormOverflowMenuItemProps> = ({
18
+ form,
19
+ patientUuid,
20
+ mutate,
21
+ visit: currentVisit,
22
+ }) => {
23
+ const {
24
+ formEncounters,
25
+ error,
26
+ isLoading,
27
+ mutate: mutateFormEncounters,
28
+ } = usePatientFormEncounter(patientUuid, form.formUuId);
29
+ const { hideFilledProgramForm, peerCalendarOutreactForm } = useConfig<CarePanelConfig>();
30
+ const { t } = useTranslation();
31
+ const {
32
+ error: formFilledError,
33
+ formsFilled: areAllDependancyFormsFilled,
34
+ isLoading: isLoadingDependancyStatus,
35
+ mutate: mutateDependancyStatus,
36
+ } = useFormsFilled(patientUuid, form.dependancies);
37
+
38
+ const latestFormEncounter = useMemo(() => formEncounters.at(0)?.encounter?.uuid, [formEncounters]);
39
+ // Show form if
40
+ // 1. Form is not yet filled AND (Form Has no dependancies OR Form has dependancies that are all filled)
41
+ // 2. Form is filled already AND hideFilledProgramForm is configured to false (else launch in edit mode)
42
+ const showForm = useMemo(() => {
43
+ // !latestFormEncounter -> current form is not yet filled
44
+ if (!latestFormEncounter && (!form?.dependancies?.length || areAllDependancyFormsFilled)) {
45
+ return true;
46
+ }
47
+ if (latestFormEncounter && !hideFilledProgramForm) {
48
+ return true;
49
+ }
50
+ return false;
51
+ }, [areAllDependancyFormsFilled, form?.dependancies?.length, hideFilledProgramForm, latestFormEncounter]);
52
+
53
+ useEffect(() => {
54
+ if (error || formFilledError) {
55
+ showSnackbar({ kind: 'error', title: t('error', 'Error'), subtitle: (error ?? formFilledError)?.message });
56
+ }
57
+ }, [error, formFilledError, t]);
58
+
59
+ if (isLoading || isLoadingDependancyStatus) {
60
+ return <InlineLoading />;
61
+ }
62
+
63
+ if (!showForm) {
64
+ return null;
65
+ }
66
+
67
+ if (form.formUuId === peerCalendarOutreactForm) {
68
+ return (
69
+ <KvpLinkPatientToPeerEducator
70
+ form={form}
71
+ patientUuid={patientUuid}
72
+ visit={currentVisit}
73
+ mutate={() => {
74
+ mutate?.();
75
+ mutateDependancyStatus();
76
+ mutateFormEncounters();
77
+ }}
78
+ />
79
+ );
80
+ }
81
+
82
+ return (
83
+ <OverflowMenuItem
84
+ key={form.formUuId}
85
+ itemText={form.formName}
86
+ onClick={() => {
87
+ if (currentVisit) {
88
+ return launchWorkspace('patient-form-entry-workspace', {
89
+ workspaceTitle: form.formName,
90
+ mutateForm: () => {
91
+ mutate?.();
92
+ mutateDependancyStatus();
93
+ mutateFormEncounters();
94
+ },
95
+ formInfo: {
96
+ encounterUuid: latestFormEncounter ?? '',
97
+ formUuid: form.formUuId,
98
+ // additionalProps: { enrollmenrDetails: careProgram.enrollmentDetails ?? {} },
99
+ },
100
+ });
101
+ }
102
+ launchStartVisitPrompt();
103
+ }}
104
+ />
105
+ );
106
+ };
107
+
108
+ export default ProgramFormOverflowMenuItem;
@@ -14,7 +14,15 @@ const useCareProgramForms = () => {
14
14
  },
15
15
  [careProgramForms],
16
16
  );
17
- return { careProgramForms, getProgramForms };
17
+
18
+ const getProgramEnrollmentForm = useCallback(
19
+ (programUuid: string) => {
20
+ const forms = getProgramForms(programUuid);
21
+ return forms.find((form) => form.isEnrollment);
22
+ },
23
+ [getProgramForms],
24
+ );
25
+ return { careProgramForms, getProgramForms, getProgramEnrollmentForm };
18
26
  };
19
27
 
20
28
  export default useCareProgramForms;