1
- import { MouseEventHandler , useCallback } from "react" ;
1
+ import { MouseEventHandler , useCallback , useState } from "react" ;
2
2
import { toast } from "sonner" ;
3
3
4
4
import { Card , CardContent } from "~/components/ui/card" ;
@@ -39,16 +39,16 @@ export const ConfirmationTab = ({
39
39
) => string | null ;
40
40
currencyId : string ;
41
41
} ) => {
42
- const [ purchaseOrderMutation , purchaseOrderMutationResults ] =
43
- useCreatePurchaseOrderMutation ( ) ;
42
+ const [ isDisabled , setIsDisabled ] = useState ( false ) ;
43
+ const [ purchaseOrderMutation ] = useCreatePurchaseOrderMutation ( ) ;
44
44
const createPurchaseOrder = useCallback ( async ( ) => {
45
- // calls the mutation
46
- const purchaseOrder = Object . entries ( selectedTickets ) . map (
47
- ( [ ticketId , quantity ] ) => ( {
45
+ setIsDisabled ( true ) ;
46
+ const purchaseOrder = Object . entries ( selectedTickets )
47
+ . filter ( ( [ , quantity ] ) => quantity > 0 )
48
+ . map ( ( [ ticketId , quantity ] ) => ( {
48
49
ticketId,
49
50
quantity,
50
- } ) ,
51
- ) ;
51
+ } ) ) ;
52
52
53
53
await purchaseOrderMutation ( {
54
54
variables : {
@@ -73,18 +73,20 @@ export const ConfirmationTab = ({
73
73
} , 2000 ) ;
74
74
}
75
75
} else {
76
+ setIsDisabled ( false ) ;
76
77
toast . error (
77
78
"Ocurrió un error al intentar comprar tus tickets. Por favor intenta de nuevo." ,
78
79
) ;
79
80
}
80
81
} ,
81
82
onError ( ) {
83
+ setIsDisabled ( false ) ;
82
84
toast . error (
83
85
"Ocurrió un error al intentar comprar tus tickets. Por favor intenta de nuevo." ,
84
86
) ;
85
87
} ,
86
88
} ) ;
87
- } , [ currencyId , purchaseOrderMutation , selectedTickets ] ) ;
89
+ } , [ currencyId , purchaseOrderMutation , selectedTickets , setIsDisabled ] ) ;
88
90
89
91
return (
90
92
< div className = "flex flex-col gap-4" >
@@ -155,9 +157,7 @@ export const ConfirmationTab = ({
155
157
console . error ( error ) ;
156
158
} ) ;
157
159
} }
158
- isDisabled = {
159
- numberOfTickets === 0 || purchaseOrderMutationResults . loading
160
- }
160
+ isDisabled = { numberOfTickets === 0 || isDisabled }
161
161
total = { formattedTotal }
162
162
/>
163
163
</ div >
0 commit comments