@@ -23,10 +23,10 @@ import { useMutation } from '@tanstack/react-query';
2323import { Appointment , DocumentReference , Encounter , Organization , Patient } from 'fhir/r4b' ;
2424import { DateTime } from 'luxon' ;
2525import { enqueueSnackbar } from 'notistack' ;
26- import { FC , Fragment , ReactElement , useEffect , useState } from 'react' ;
26+ import { FC , Fragment , ReactElement , useState } from 'react' ;
2727import { useOystehrAPIClient } from 'src/features/visits/shared/hooks/useOystehrAPIClient' ;
2828import { useApiClients } from 'src/hooks/useAppClients' ;
29- import { useGetEncounter } from 'src/hooks/useEncounter' ;
29+ import { useEncounterReceipt , useGetEncounter } from 'src/hooks/useEncounter' ;
3030import { useGetPatientAccount } from 'src/hooks/useGetPatient' ;
3131import { useGetPatientPaymentsList } from 'src/hooks/useGetPatientPaymentsList' ;
3232import {
@@ -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"
0 commit comments