Skip to content

Commit c3260ed

Browse files
authored
Merge pull request #6090 from masslight/rgolchuk/otr-1729-ehr-visit-details-need-to-manually-reload-the-screen-to
Issue 5846. EHR. Visit Details. Need to manually reload the screen to enable "Email receipt" button.
2 parents 94e1726 + 219b37a commit c3260ed

File tree

2 files changed

+68
-40
lines changed

2 files changed

+68
-40
lines changed

apps/ehr/src/components/PatientPaymentsList.tsx

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import { useMutation } from '@tanstack/react-query';
2323
import { Appointment, DocumentReference, Encounter, Organization, Patient } from 'fhir/r4b';
2424
import { DateTime } from 'luxon';
2525
import { enqueueSnackbar } from 'notistack';
26-
import { FC, Fragment, ReactElement, useEffect, useState } from 'react';
26+
import { FC, Fragment, ReactElement, useState } from 'react';
2727
import { useOystehrAPIClient } from 'src/features/visits/shared/hooks/useOystehrAPIClient';
2828
import { useApiClients } from 'src/hooks/useAppClients';
29-
import { useGetEncounter } from 'src/hooks/useEncounter';
29+
import { useEncounterReceipt, useGetEncounter } from 'src/hooks/useEncounter';
3030
import { useGetPatientAccount } from 'src/hooks/useGetPatient';
3131
import { useGetPatientPaymentsList } from 'src/hooks/useGetPatientPaymentsList';
3232
import {
@@ -42,7 +42,6 @@ import {
4242
PatientPaymentDTO,
4343
PaymentVariant,
4444
PostPatientPaymentInput,
45-
RECEIPT_CODE,
4645
SendReceiptByEmailZambdaInput,
4746
SERVICE_CATEGORY_SYSTEM,
4847
updateEncounterPaymentVariantExtension,
@@ -88,13 +87,19 @@ export default function PatientPaymentList({
8887
const theme = useTheme();
8988
const [paymentDialogOpen, setPaymentDialogOpen] = useState(false);
9089
const [sendReceiptByEmailDialogOpen, setSendReceiptByEmailDialogOpen] = useState(false);
91-
const [receiptDocRefId, setReceiptDocRefId] = useState<string | undefined>();
90+
9291
const {
9392
data: encounter,
9493
refetch: refetchEncounter,
9594
isRefetching: isEncounterRefetching,
9695
} = useGetEncounter({ encounterId });
9796

97+
const {
98+
data: receiptDocRef,
99+
refetch: refetchReceipt,
100+
isFetching: isReceiptFetching,
101+
} = useEncounterReceipt({ encounterId });
102+
98103
const {
99104
data: paymentData,
100105
refetch: refetchPaymentList,
@@ -145,30 +150,7 @@ export default function PatientPaymentList({
145150
? (paymentListError as APIError).code === APIErrorCode.STRIPE_CUSTOMER_ID_DOES_NOT_EXIST
146151
: false;
147152

148-
useEffect(() => {
149-
if (oystehr && encounterId) {
150-
void oystehr.fhir
151-
.search<DocumentReference>({
152-
resourceType: 'DocumentReference',
153-
params: [
154-
{
155-
name: 'type',
156-
value: RECEIPT_CODE,
157-
},
158-
{
159-
name: 'encounter',
160-
value: 'Encounter/' + encounterId,
161-
},
162-
],
163-
})
164-
.then((response) => {
165-
const docRef = response.unbundle()[0];
166-
if (docRef) {
167-
setReceiptDocRefId(docRef.id);
168-
}
169-
});
170-
}
171-
}, [encounterId, oystehr, paymentData]);
153+
const receiptDocRefId = receiptDocRef?.id;
172154

173155
const sendReceipt = async (formData: SendReceiptFormData): Promise<void> => {
174156
if (!oystehr) return;
@@ -220,17 +202,34 @@ export default function PatientPaymentList({
220202

221203
const createNewPayment = useMutation({
222204
mutationFn: async (input: PostPatientPaymentInput) => {
223-
if (oystehrZambda && input) {
224-
return oystehrZambda.zambda
225-
.execute({
226-
id: 'patient-payments-post',
227-
...input,
228-
})
229-
.then(async () => {
230-
await refetchPaymentList();
231-
setPaymentDialogOpen(false);
232-
});
233-
}
205+
if (!oystehrZambda) return;
206+
207+
await oystehrZambda.zambda.execute({
208+
id: 'patient-payments-post',
209+
...input,
210+
});
211+
},
212+
onSuccess: async () => {
213+
await refetchPaymentList();
214+
const waitForReceipt = async (): Promise<void> => {
215+
let receipt: DocumentReference | null = null;
216+
const maxTries = 15;
217+
let tries = 0;
218+
219+
while (!receipt && tries < maxTries) {
220+
const result = await refetchReceipt();
221+
222+
receipt = result.data ?? null;
223+
if (!receipt) {
224+
await new Promise((res) => setTimeout(res, 2000));
225+
}
226+
tries += 1;
227+
}
228+
};
229+
230+
await waitForReceipt();
231+
232+
setPaymentDialogOpen(false);
234233
},
235234
retry: 0,
236235
});
@@ -500,7 +499,7 @@ export default function PatientPaymentList({
500499
<span>
501500
<Button
502501
sx={{ mt: 2, ml: 2 }}
503-
disabled={!receiptDocRefId}
502+
disabled={!receiptDocRefId || isReceiptFetching}
504503
onClick={() => setSendReceiptByEmailDialogOpen(true)}
505504
variant="contained"
506505
color="primary"

apps/ehr/src/hooks/useEncounter.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { useQuery, UseQueryResult } from '@tanstack/react-query';
22
import { Encounter } from 'fhir/r4b';
3+
import { DocumentReference } from 'fhir/r4b';
4+
import { RECEIPT_CODE } from 'utils';
35
import { useApiClients } from './useAppClients';
46

57
export interface useGetEncounterInput {
@@ -24,3 +26,30 @@ export const useGetEncounter = (input: useGetEncounterInput): UseQueryResult<Enc
2426
enabled: Boolean(encounterId) && Boolean(oystehr),
2527
});
2628
};
29+
30+
export const useEncounterReceipt = (input: useGetEncounterInput): UseQueryResult<DocumentReference | null, Error> => {
31+
const { oystehr } = useApiClients();
32+
const { encounterId } = input;
33+
34+
return useQuery({
35+
queryKey: ['encounter-receipt', encounterId],
36+
queryFn: async () => {
37+
if (oystehr) {
38+
const response = await oystehr!.fhir.search<DocumentReference>({
39+
resourceType: 'DocumentReference',
40+
params: [
41+
{ name: 'type', value: RECEIPT_CODE },
42+
{ name: 'encounter', value: `Encounter/${encounterId}` },
43+
],
44+
});
45+
46+
const receipt = response.unbundle()[0] ?? null;
47+
48+
return receipt;
49+
}
50+
51+
throw new Error('api client not defined or patient id is not provided');
52+
},
53+
enabled: Boolean(encounterId) && Boolean(oystehr),
54+
});
55+
};

0 commit comments

Comments
 (0)