| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- import { useMutation } from "@apollo/client/react";
- import { useRouter } from "next/navigation";
- import { useCustomToast } from "./useToast";
- import { useDispatch } from "react-redux";
- import {
- clearCart,
- } from "@/store/slices/cart-slice";
- import { getCartToken } from "@utils/getCartToken";
- import { getCookie, setCookie } from "@/utils/cookie-tools";
- import { useGuestCartToken } from "./useGuestCartToken";
- import { ORDER_ID, IS_GUEST } from "@utils/constants";
- import { useCartDetail } from "./useCartDetail";
- import {
- CREATE_CHECKOUT_ADDRESS,
- CREATE_CHECKOUT_ORDER,
- CREATE_CHECKOUT_PAYMENT_METHODS,
- CREATE_CHECKOUT_SHIPPING_METHODS,
- GET_CHECKOUT_ADDRESSES,
- } from "@/graphql";
- import { CreateCheckoutShippingMethodData, CreateCheckoutPaymentMethodResponse, CreateCheckoutOrderData } from "@/types/checkout/type";
- export const useCheckout = () => {
- const router = useRouter();
- const { resetGuestToken } = useGuestCartToken();
- const { showToast } = useCustomToast();
- const dispatch = useDispatch();
- const { getCartDetail } = useCartDetail();
- const handleMutationError = (error: any) => {
- showToast(error?.message || "An error occurred", "danger");
- };
- const [saveAddressToCheckout, { loading: isLoadingToSave }] = useMutation(
- CREATE_CHECKOUT_ADDRESS,
- {
- refetchQueries: [{ query: GET_CHECKOUT_ADDRESSES }],
- awaitRefetchQueries: true,
- onCompleted: () => {
- showToast("Address saved successfully", "success");
- router.replace("/checkout?step=shipping");
- },
- onError: handleMutationError,
- },
- );
- const saveCheckoutAddress = async (input: any) => {
- const token = getCartToken();
- await saveAddressToCheckout({
- variables: {
- token: token || "",
- ...input,
- },
- });
- };
- const [saveShipping, { loading: isSaving }] = useMutation<CreateCheckoutShippingMethodData>(
- CREATE_CHECKOUT_SHIPPING_METHODS,
- {
- onCompleted: (response) => {
- const responseData =
- response?.createCheckoutShippingMethod?.checkoutShippingMethod;
- if (responseData?.success) {
- getCartDetail();
- showToast("Shipping method saved successfully", "success");
- router.replace("/checkout?step=payment");
- } else {
- showToast(
- responseData?.message || "Failed to save shipping method",
- "warning",
- );
- }
- },
- onError: handleMutationError,
- },
- );
- const saveCheckoutShipping = async (shippingMethod: string) => {
- const token = getCartToken();
- await saveShipping({
- variables: {
- token: token || "",
- shippingMethod,
- },
- });
- };
- const [savePayment, { loading: isPaymentLoading }] = useMutation<CreateCheckoutPaymentMethodResponse>(
- CREATE_CHECKOUT_PAYMENT_METHODS,
- {
- onCompleted: (response) => {
- const responseData =
- response?.createCheckoutPaymentMethod?.checkoutPaymentMethod;
- if (responseData?.success) {
- showToast("Payment method saved successfully", "success");
- router.replace("/checkout?step=review");
- } else {
- showToast(
- responseData?.message || "Failed to save payment method",
- "warning",
- );
- }
- },
- onError: handleMutationError,
- },
- );
- const saveCheckoutPayment = async (paymentMethod: string) => {
- const token = getCartToken();
- await savePayment({
- variables: {
- token: token || "",
- paymentMethod,
- },
- });
- };
- const [placeOrder, { loading: isPlaceOrder }] = useMutation<CreateCheckoutOrderData>(
- CREATE_CHECKOUT_ORDER,
- {
- onCompleted: (response) => {
- const responseData = response?.createCheckoutOrder?.checkoutOrder;
- if (responseData) {
- showToast("Order placed successfully!", "success");
- setCookie(ORDER_ID, responseData?.orderId);
- dispatch(clearCart());
- router.replace("/success");
- } else {
- showToast(
- "Failed to place order",
- "warning",
- );
- }
- },
- onError: handleMutationError,
- },
- );
- const savePlaceOrder = async () => {
- const token = getCartToken();
- await placeOrder({
- variables: {
- token: token || "",
- },
- });
- const isGuest = getCookie(IS_GUEST);
- if (isGuest === "true") {
- await resetGuestToken();
- }
- };
- return {
- isLoadingToSave,
- saveCheckoutAddress,
- isSaving,
- saveCheckoutShipping,
- isPaymentLoading,
- saveCheckoutPayment,
- isPlaceOrder,
- savePlaceOrder,
- };
- };
|