11'use client' ;
22
33import ButtonWithLoading from '@/components/common/ButtonWithLoading' ;
4- import { UPDATE_SHIPPING_METHOD } from '@/graphql/queries/cart' ;
4+ import {
5+ EMPTY_CART_MUTATION ,
6+ UPDATE_SHIPPING_METHOD ,
7+ } from '@/graphql/queries/cart' ;
58import {
69 CHECKOUT_MUTATION ,
710 GET_PAYMENT_GATEWAYS ,
811} from '@/graphql/queries/checkout' ;
912import {
13+ CheckoutMutation ,
1014 GetPaymentGatewaysQuery ,
15+ RemoveItemsFromCartMutation ,
1116 ShippingRate ,
1217 UpdateShippingMethodMutation ,
1318} from '@/graphql/types/graphql' ;
1419import useCartQuery from '@/hooks/useCartQuery' ;
15- import useEmptyCart from '@/hooks/useEmptyCart' ;
1620import { redirect } from '@/navigation' ;
1721import { cartAtom } from '@/store/atoms' ;
1822import { useMutation , useQuery } from '@apollo/client' ;
@@ -61,11 +65,13 @@ const Page = () => {
6165 useMutation < UpdateShippingMethodMutation > ( UPDATE_SHIPPING_METHOD ) ;
6266
6367 const [ checkout , { loading : checkoutLoading } ] =
64- useMutation ( CHECKOUT_MUTATION ) ;
68+ useMutation < CheckoutMutation > ( CHECKOUT_MUTATION ) ;
6569
66- const { emptyCartLoading, emptyCartMutate } = useEmptyCart ( ) ;
70+ const [ emptyCartMutate , { loading : emptyCartLoading } ] =
71+ useMutation < RemoveItemsFromCartMutation > ( EMPTY_CART_MUTATION ) ;
6772
6873 if ( ! content ?. contents ?. itemCount ) return redirect ( '/cart' ) ;
74+
6975 const rates = content ?. availableShippingMethods ?. flatMap ( ( item ) => {
7076 return item ?. rates ;
7177 } ) as ShippingRate [ ] ;
@@ -83,15 +89,20 @@ const Page = () => {
8389 refetch ( ) ;
8490 } ;
8591
86- const onSubmit = async ( data : any ) => {
87- await checkout ( {
92+ const onSubmit = async ( payload : any ) => {
93+ const { data } = await checkout ( {
8894 variables : {
89- customerNote : data . customerNote ,
90- paymentMethod : data . paymentMethod ,
95+ customerNote : payload . customerNote ,
96+ paymentMethod : payload . paymentMethod ,
9197 } ,
9298 } ) ;
9399
94- await emptyCartMutate ( ) ;
100+ const url = data ?. checkout ?. redirect ;
101+
102+ if ( url ) {
103+ window . location . replace ( url ) ;
104+ await emptyCartMutate ( ) ;
105+ }
95106 } ;
96107
97108 const isCartLoading = loading || shippingMethodLoading ;
0 commit comments