From 0b3e877e36a20f30da673c2625a858863f227949 Mon Sep 17 00:00:00 2001 From: chibongho Date: Thu, 12 Dec 2024 16:56:23 -0500 Subject: [PATCH] =?UTF-8?q?(fix)=20O3-4271=20Service=20Queues=20-=20fix=20?= =?UTF-8?q?form=20to=20start=20service=20queues=20for=20p=E2=80=A6=20(#141?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * (fix) O3-4271 Service Queues - fix form to start service queues for patient with existing visit * minor fixup --- .../patient-appointments-base.test.tsx | 2 +- ...t-upcoming-appointments-card.component.tsx | 68 +++++++++++-------- .../existing-visit-form.component.tsx | 12 ++-- .../queue-fields/queue-fields.component.tsx | 59 +++++++--------- .../queue-fields/queue-fields.scss | 4 -- .../queue-fields/queue-fields.test.tsx | 5 +- .../visit-form-queue-fields.extension.tsx | 9 ++- .../translations/en.json | 1 - 8 files changed, 84 insertions(+), 76 deletions(-) diff --git a/packages/esm-appointments-app/src/patient-appointments/patient-appointments-base.test.tsx b/packages/esm-appointments-app/src/patient-appointments/patient-appointments-base.test.tsx index 2ef78d564..7e509c33b 100644 --- a/packages/esm-appointments-app/src/patient-appointments/patient-appointments-base.test.tsx +++ b/packages/esm-appointments-app/src/patient-appointments/patient-appointments-base.test.tsx @@ -14,7 +14,7 @@ const testProps = { const mockOpenmrsFetch = jest.mocked(openmrsFetch); -describe('AppointmensOverview', () => { +describe('AppointmentsOverview', () => { it('renders an empty state if appointments data is unavailable', async () => { mockOpenmrsFetch.mockResolvedValueOnce({ data: [], diff --git a/packages/esm-appointments-app/src/patient-appointments/patient-upcoming-appointments-card.component.tsx b/packages/esm-appointments-app/src/patient-appointments/patient-upcoming-appointments-card.component.tsx index cee3684ad..ace547b74 100644 --- a/packages/esm-appointments-app/src/patient-appointments/patient-upcoming-appointments-card.component.tsx +++ b/packages/esm-appointments-app/src/patient-appointments/patient-upcoming-appointments-card.component.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useMemo, useState } from 'react'; +import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { InlineLoading, @@ -18,9 +18,13 @@ import dayjs from 'dayjs'; import { type Appointment } from '../types'; import { useMutateAppointments } from '../form/appointments-form.resource'; +interface VisitFormCallbacks { + onVisitCreatedOrUpdated: (visit: Visit) => Promise; +} + // See VisitFormExtensionState in esm-patient-chart-app export interface PatientUpcomingAppointmentsProps { - setOnVisitCreatedOrUpdated(onSubmit: (visit: Visit) => Promise); + setVisitFormCallbacks(callbacks: VisitFormCallbacks); visitFormOpenedFrom: string; patientChartConfig?: { showUpcomingAppointments: boolean; @@ -36,7 +40,7 @@ export interface PatientUpcomingAppointmentsProps { */ const PatientUpcomingAppointmentsCard: React.FC = ({ patientUuid, - setOnVisitCreatedOrUpdated, + setVisitFormCallbacks, patientChartConfig, }) => { const { t } = useTranslation(); @@ -44,37 +48,45 @@ const PatientUpcomingAppointmentsCard: React.FC(null); const { mutateAppointments } = useMutateAppointments(); + const memoMutateAppointments = useMemo(() => mutateAppointments, [mutateAppointments]); const ac = useMemo(() => new AbortController(), []); useEffect(() => () => ac.abort(), [ac]); const { data: appointmentsData, error, isLoading } = usePatientAppointments(patientUuid, startDate, ac); - useEffect(() => { - setOnVisitCreatedOrUpdated(() => { - if (selectedAppointment) { - return changeAppointmentStatus('CheckedIn', selectedAppointment.uuid) - .then(() => { - mutateAppointments(); - showSnackbar({ - isLowContrast: true, - kind: 'success', - subtitle: t('appointmentMarkedChecked', 'Appointment marked as Checked In'), - title: t('appointmentCheckedIn', 'Appointment Checked In'), - }); - }) - .catch((error) => { - showSnackbar({ - title: t('updateError', 'Error updating upcoming appointment'), - kind: 'error', - isLowContrast: false, - subtitle: error?.message, + const onVisitCreatedOrUpdated = useMemo( + () => ({ + onVisitCreatedOrUpdated: () => { + if (selectedAppointment) { + return changeAppointmentStatus('CheckedIn', selectedAppointment.uuid) + .then(() => { + memoMutateAppointments(); + showSnackbar({ + isLowContrast: true, + kind: 'success', + subtitle: t('appointmentMarkedChecked', 'Appointment marked as Checked In'), + title: t('appointmentCheckedIn', 'Appointment Checked In'), + }); + }) + .catch((error) => { + showSnackbar({ + title: t('updateError', 'Error updating upcoming appointment'), + kind: 'error', + isLowContrast: false, + subtitle: error?.message, + }); }); - }); - } else { - return Promise.resolve(); - } - }); - }, [selectedAppointment, mutateAppointments, setOnVisitCreatedOrUpdated, t]); + } else { + return Promise.resolve(); + } + }, + }), + [selectedAppointment, memoMutateAppointments, t], + ); + + useEffect(() => { + setVisitFormCallbacks(onVisitCreatedOrUpdated); + }, [onVisitCreatedOrUpdated, setVisitFormCallbacks]); const todaysAppointments = appointmentsData?.todaysAppointments?.length ? appointmentsData?.todaysAppointments : []; const futureAppointments = appointmentsData?.upcomingAppointments?.length diff --git a/packages/esm-service-queues-app/src/create-queue-entry/existing-visit-form/existing-visit-form.component.tsx b/packages/esm-service-queues-app/src/create-queue-entry/existing-visit-form/existing-visit-form.component.tsx index 2c08264b0..ec0a27e2b 100644 --- a/packages/esm-service-queues-app/src/create-queue-entry/existing-visit-form/existing-visit-form.component.tsx +++ b/packages/esm-service-queues-app/src/create-queue-entry/existing-visit-form/existing-visit-form.component.tsx @@ -22,22 +22,26 @@ const ExistingVisitForm: React.FC = ({ visit, closeWorks const [isSubmitting, setIsSubmitting] = useState(false); const { mutateQueueEntries } = useMutateQueueEntries(); - const [submitQueueEntry, setSubmitQueueEntry] = useState<(visit: Visit, patientUuid: string) => Promise>(null); + const [callback, setCallback] = useState<{ + submitQueueEntry: (visit: Visit) => Promise; + }>(null); const handleSubmit = useCallback( (event) => { event.preventDefault(); setIsSubmitting(true); - submitQueueEntry?.(visit, visit.patient.uuid) + callback + ?.submitQueueEntry?.(visit) ?.then(() => { closeWorkspace(); + mutateQueueEntries(); }) ?.finally(() => { setIsSubmitting(false); }); }, - [closeWorkspace, submitQueueEntry, visit], + [closeWorkspace, callback, visit, mutateQueueEntries], ); return visit ? ( @@ -52,7 +56,7 @@ const ExistingVisitForm: React.FC = ({ visit, closeWorks )}
- + setCallback({ submitQueueEntry: onSubmit })} />