Skip to content

Commit 763cad0

Browse files
committed
feat: redirect user to payment url
1 parent 629003b commit 763cad0

File tree

1 file changed

+20
-9
lines changed
  • src/app/[locale]/(main)/(container)/checkout

1 file changed

+20
-9
lines changed

src/app/[locale]/(main)/(container)/checkout/page.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
'use client';
22

33
import 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';
58
import {
69
CHECKOUT_MUTATION,
710
GET_PAYMENT_GATEWAYS,
811
} from '@/graphql/queries/checkout';
912
import {
13+
CheckoutMutation,
1014
GetPaymentGatewaysQuery,
15+
RemoveItemsFromCartMutation,
1116
ShippingRate,
1217
UpdateShippingMethodMutation,
1318
} from '@/graphql/types/graphql';
1419
import useCartQuery from '@/hooks/useCartQuery';
15-
import useEmptyCart from '@/hooks/useEmptyCart';
1620
import { redirect } from '@/navigation';
1721
import { cartAtom } from '@/store/atoms';
1822
import { 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

Comments
 (0)