diff --git a/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.component.tsx b/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.component.tsx index a8e4876bf0..186e4a8ba5 100644 --- a/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.component.tsx +++ b/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.component.tsx @@ -120,7 +120,7 @@ const StartVisitForm: React.FC = ({ const { visitAttributeTypes } = useVisitAttributeTypes(); const [extraVisitInfo, setExtraVisitInfo] = useState(null); - const [visitFormCallbacks, setVisitFormCallbacks] = useVisitFormCallbacks(); + const visitFormCallbacksRef = useVisitFormCallbacks(); const displayVisitStopDateTimeFields = useMemo( () => Boolean(visitToEdit?.uuid || showVisitEndDateTimeFields), [visitToEdit?.uuid, showVisitEndDateTimeFields], @@ -243,7 +243,6 @@ const StartVisitForm: React.FC = ({ reset, } = methods; - // default values are cached so form needs to be reset when they change (e.g. when default visit location finishes loading) useEffect(() => { reset(defaultValues); }, [defaultValues, reset]); @@ -531,7 +530,7 @@ const StartVisitForm: React.FC = ({ }, ); - const onVisitCreatedOrUpdatedRequests = [...visitFormCallbacks.values()].map((callbacks) => + const onVisitCreatedOrUpdatedRequests = [...visitFormCallbacksRef.current.values()].map((callbacks) => callbacks.onVisitCreatedOrUpdated(visit), ); @@ -580,6 +579,7 @@ const StartVisitForm: React.FC = ({ return; } }, + // eslint-disable-next-line react-hooks/exhaustive-deps [ closeWorkspace, config.offlineVisitTypeUuid, @@ -591,7 +591,6 @@ const StartVisitForm: React.FC = ({ mutateCurrentVisit, mutateVisits, mutateInfiniteVisits, - visitFormCallbacks, patientUuid, t, validateVisitStartStopDatetime, @@ -660,10 +659,10 @@ const StartVisitForm: React.FC = ({
@@ -732,7 +731,6 @@ const StartVisitForm: React.FC = ({ {contentSwitcherIndex === 1 && } ) : ( - // Defaults to showing all possible visit types if recommended visits are not enabled )} @@ -774,7 +772,7 @@ const StartVisitForm: React.FC = ({ name="visit-form-bottom-slot" patientUuid={patientUuid} visitFormOpenedFrom={openedFrom} - setVisitFormCallbacks={setVisitFormCallbacks} + visitFormCallbacksRef={visitFormCallbacksRef} /> @@ -813,12 +811,12 @@ const StartVisitForm: React.FC = ({ ); }; - interface VisitFormExtensionSlotProps { name: string; patientUuid: string; visitFormOpenedFrom: string; - setVisitFormCallbacks: React.Dispatch>>; + + visitFormCallbacksRef: React.MutableRefObject>; } type VisitFormExtensionState = { @@ -838,22 +836,21 @@ type VisitFormExtensionState = { }; const VisitFormExtensionSlot: React.FC = React.memo( - ({ name, patientUuid, visitFormOpenedFrom, setVisitFormCallbacks }) => { + ({ name, patientUuid, visitFormOpenedFrom, visitFormCallbacksRef }) => { const config = useConfig(); return ( {(extension: AssignedExtension) => { - const state: VisitFormExtensionState = { + const state = { patientUuid, - setVisitFormCallbacks: (callbacks) => { - setVisitFormCallbacks((old) => { - return new Map(old).set(extension.id, callbacks); - }); + setVisitFormCallbacks: (callbacks: VisitFormCallbacks) => { + visitFormCallbacksRef.current.set(extension.id, callbacks); }, visitFormOpenedFrom, patientChartConfig: config, }; + return ; }} diff --git a/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.resource.ts b/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.resource.ts index 832c9b550f..c62ac2c948 100644 --- a/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.resource.ts +++ b/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.resource.ts @@ -1,7 +1,7 @@ import { openmrsFetch, restBaseUrl, useConnectivity, useVisitTypes, type Visit } from '@openmrs/esm-framework'; import { type amPm } from '@openmrs/esm-patient-common-lib'; import { useOfflineVisitType } from '../hooks/useOfflineVisitType'; -import { useState } from 'react'; +import { useRef } from 'react'; export type VisitFormData = { visitStartDate: Date; @@ -28,12 +28,13 @@ export function useConditionalVisitTypes() { return visitTypesHook(); } + export interface VisitFormCallbacks { onVisitCreatedOrUpdated: (visit: Visit) => Promise; } export function useVisitFormCallbacks() { - return useState>(new Map()); + return useRef>(new Map()); } export function createVisitAttribute(visitUuid: string, attributeType: string, value: string) { diff --git a/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.test.tsx b/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.test.tsx index 915fbe0add..4f3fbd9724 100644 --- a/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.test.tsx +++ b/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.test.tsx @@ -76,10 +76,11 @@ const mockUseEmrConfiguration = jest.mocked(useEmrConfiguration); // from ./visit-form.resource const mockOnVisitCreatedOrUpdatedCallback = jest.fn(); -jest.mocked(useVisitFormCallbacks).mockReturnValue([ - new Map([['test-extension-id', { onVisitCreatedOrUpdated: mockOnVisitCreatedOrUpdatedCallback }]]), // visitFormCallbacks - jest.fn(), // setVisitFormCallbacks -]); + +jest.mocked(useVisitFormCallbacks).mockReturnValue({ + current: new Map([['test-extension-id', { onVisitCreatedOrUpdated: mockOnVisitCreatedOrUpdatedCallback }]]), +}); + const mockCreateVisitAttribute = jest.mocked(createVisitAttribute).mockResolvedValue({} as unknown as FetchResponse); const mockUpdateVisitAttribute = jest.mocked(updateVisitAttribute).mockResolvedValue({} as unknown as FetchResponse); const mockDeleteVisitAttribute = jest.mocked(deleteVisitAttribute).mockResolvedValue({} as unknown as FetchResponse);