@@ -48,54 +48,61 @@ const OnPrescriptionFilledModal: React.FC<OnPrescriptionFilledModalProps> = ({ p
4848 const providers = useProviders ( dispenserProviderRoles ) ;
4949 const { medicationRequestBundles } = usePrescriptionDetails ( encounterUuid ) ;
5050 const { t } = useTranslation ( ) ;
51+ const [ isSubmitting , setIsSubmitting ] = React . useState ( false ) ;
5152
5253 const onConfirm = async ( ) => {
54+ setIsSubmitting ( true ) ;
5355 markEncounterAsStale ( encounterUuid ) ;
54- for ( const medicationRequestBundle of medicationRequestBundles ) {
55- const medicationDispensePayload = initiateMedicationDispenseBody (
56- medicationRequestBundle . request ,
57- session ,
58- providers ,
59- true ,
60- ) ;
61- const medicationDisplay = getMedicationDisplay (
62- getMedicationReferenceOrCodeableConcept ( medicationRequestBundle . request ) ,
63- ) ;
56+ try {
57+ for ( const medicationRequestBundle of medicationRequestBundles ) {
58+ const medicationDispensePayload = initiateMedicationDispenseBody (
59+ medicationRequestBundle . request ,
60+ session ,
61+ providers ,
62+ true ,
63+ ) ;
64+ const medicationDisplay = getMedicationDisplay (
65+ getMedicationReferenceOrCodeableConcept ( medicationRequestBundle . request ) ,
66+ ) ;
6467
65- await saveMedicationDispense ( medicationDispensePayload , MedicationDispenseStatus . completed )
66- . then ( ( response ) => {
67- const hasNoRefills = medicationRequestBundle . request . dispenseRequest . numberOfRepeatsAllowed == 0 ;
68- if ( response . ok && hasNoRefills ) {
69- return updateMedicationRequestFulfillerStatus (
70- getUuidFromReference (
71- medicationDispensePayload . authorizingPrescription [ 0 ] . reference , // assumes authorizing prescription exist
72- ) ,
73- MedicationRequestFulfillerStatus . completed ,
74- ) . then ( ( ) => response ) ;
75- } else {
76- return response ;
77- }
78- } )
79- . then ( ( ) => {
80- showSnackbar ( {
81- title : t ( 'stockDispensed' , 'Stock dispensed' ) ,
82- subtitle : medicationDisplay ,
83- isLowContrast : false ,
68+ await saveMedicationDispense ( medicationDispensePayload , MedicationDispenseStatus . completed )
69+ . then ( ( response ) => {
70+ const hasNoRefills = medicationRequestBundle . request . dispenseRequest . numberOfRepeatsAllowed == 0 ;
71+ if ( response . ok && hasNoRefills ) {
72+ return updateMedicationRequestFulfillerStatus (
73+ getUuidFromReference (
74+ medicationDispensePayload . authorizingPrescription [ 0 ] . reference , // assumes authorizing prescription exist
75+ ) ,
76+ MedicationRequestFulfillerStatus . completed ,
77+ ) . then ( ( ) => response ) ;
78+ } else {
79+ return response ;
80+ }
81+ } )
82+ . then ( ( ) => {
83+ showSnackbar ( {
84+ title : t ( 'stockDispensed' , 'Stock dispensed' ) ,
85+ subtitle : medicationDisplay ,
86+ isLowContrast : false ,
87+ } ) ;
88+ } )
89+ . catch ( ( error ) => {
90+ showSnackbar ( {
91+ title : t ( 'errorDispensingMedication' , 'Error dispensing medication' ) ,
92+ kind : 'error' ,
93+ subtitle : t ( 'errorDispensingMedicationMessage' , '{{medication}}: {{error}}' , {
94+ medication : medicationDisplay ,
95+ error : error ?. message ,
96+ } ) ,
97+ } ) ;
8498 } ) ;
85- } )
86- . catch ( ( error ) => {
87- showSnackbar ( {
88- title : t ( 'errorDispensingMedication' , 'Error dispensing medication' ) ,
89- kind : 'error' ,
90- subtitle : t ( 'errorDispensingMedicationMessage' , '{{medication}}: {{error}}' , {
91- medication : medicationDisplay ,
92- error : error ?. message ,
93- } ) ,
94- } ) ;
95- } ) ;
99+ }
100+
101+ close ( ) ;
102+ } finally {
103+ revalidate ( encounterUuid ) ;
104+ setIsSubmitting ( false ) ;
96105 }
97- close ( ) ;
98- revalidate ( encounterUuid ) ;
99106 } ;
100107
101108 const patientName = getPatientName ( patient ) ;
@@ -119,6 +126,7 @@ const OnPrescriptionFilledModal: React.FC<OnPrescriptionFilledModalProps> = ({ p
119126 { t ( 'createOrderWithoutDispensing' , 'Create order without dispensing' ) }
120127 </ Button >
121128 < Button
129+ disabled = { isSubmitting }
122130 onClick = { ( ) => {
123131 onConfirm ( ) ;
124132 } } >
0 commit comments