useCheckout.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { useMutation } from "@apollo/client/react";
  2. import { useRouter } from "next/navigation";
  3. import { useCustomToast } from "./useToast";
  4. import { useDispatch } from "react-redux";
  5. import {
  6. clearCart,
  7. } from "@/store/slices/cart-slice";
  8. import { getCartToken } from "@utils/getCartToken";
  9. import { getCookie, setCookie } from "@/utils/cookie-tools";
  10. import { useGuestCartToken } from "./useGuestCartToken";
  11. import { ORDER_ID, IS_GUEST } from "@utils/constants";
  12. import { useCartDetail } from "./useCartDetail";
  13. import {
  14. CREATE_CHECKOUT_ADDRESS,
  15. CREATE_CHECKOUT_ORDER,
  16. CREATE_CHECKOUT_PAYMENT_METHODS,
  17. CREATE_CHECKOUT_SHIPPING_METHODS,
  18. GET_CHECKOUT_ADDRESSES,
  19. } from "@/graphql";
  20. import { CreateCheckoutShippingMethodData, CreateCheckoutPaymentMethodResponse, CreateCheckoutOrderData } from "@/types/checkout/type";
  21. export const useCheckout = () => {
  22. const router = useRouter();
  23. const { resetGuestToken } = useGuestCartToken();
  24. const { showToast } = useCustomToast();
  25. const dispatch = useDispatch();
  26. const { getCartDetail } = useCartDetail();
  27. const handleMutationError = (error: any) => {
  28. showToast(error?.message || "An error occurred", "danger");
  29. };
  30. const [saveAddressToCheckout, { loading: isLoadingToSave }] = useMutation(
  31. CREATE_CHECKOUT_ADDRESS,
  32. {
  33. refetchQueries: [{ query: GET_CHECKOUT_ADDRESSES }],
  34. awaitRefetchQueries: true,
  35. onCompleted: () => {
  36. showToast("Address saved successfully", "success");
  37. router.replace("/checkout?step=shipping");
  38. },
  39. onError: handleMutationError,
  40. },
  41. );
  42. const saveCheckoutAddress = async (input: any) => {
  43. const token = getCartToken();
  44. await saveAddressToCheckout({
  45. variables: {
  46. token: token || "",
  47. ...input,
  48. },
  49. });
  50. };
  51. const [saveShipping, { loading: isSaving }] = useMutation<CreateCheckoutShippingMethodData>(
  52. CREATE_CHECKOUT_SHIPPING_METHODS,
  53. {
  54. onCompleted: (response) => {
  55. const responseData =
  56. response?.createCheckoutShippingMethod?.checkoutShippingMethod;
  57. if (responseData?.success) {
  58. getCartDetail();
  59. showToast("Shipping method saved successfully", "success");
  60. router.replace("/checkout?step=payment");
  61. } else {
  62. showToast(
  63. responseData?.message || "Failed to save shipping method",
  64. "warning",
  65. );
  66. }
  67. },
  68. onError: handleMutationError,
  69. },
  70. );
  71. const saveCheckoutShipping = async (shippingMethod: string) => {
  72. const token = getCartToken();
  73. await saveShipping({
  74. variables: {
  75. token: token || "",
  76. shippingMethod,
  77. },
  78. });
  79. };
  80. const [savePayment, { loading: isPaymentLoading }] = useMutation<CreateCheckoutPaymentMethodResponse>(
  81. CREATE_CHECKOUT_PAYMENT_METHODS,
  82. {
  83. onCompleted: (response) => {
  84. const responseData =
  85. response?.createCheckoutPaymentMethod?.checkoutPaymentMethod;
  86. if (responseData?.success) {
  87. showToast("Payment method saved successfully", "success");
  88. router.replace("/checkout?step=review");
  89. } else {
  90. showToast(
  91. responseData?.message || "Failed to save payment method",
  92. "warning",
  93. );
  94. }
  95. },
  96. onError: handleMutationError,
  97. },
  98. );
  99. const saveCheckoutPayment = async (paymentMethod: string) => {
  100. const token = getCartToken();
  101. await savePayment({
  102. variables: {
  103. token: token || "",
  104. paymentMethod,
  105. },
  106. });
  107. };
  108. const [placeOrder, { loading: isPlaceOrder }] = useMutation<CreateCheckoutOrderData>(
  109. CREATE_CHECKOUT_ORDER,
  110. {
  111. onCompleted: (response) => {
  112. const responseData = response?.createCheckoutOrder?.checkoutOrder;
  113. if (responseData) {
  114. showToast("Order placed successfully!", "success");
  115. setCookie(ORDER_ID, responseData?.orderId);
  116. dispatch(clearCart());
  117. router.replace("/success");
  118. } else {
  119. showToast(
  120. "Failed to place order",
  121. "warning",
  122. );
  123. }
  124. },
  125. onError: handleMutationError,
  126. },
  127. );
  128. const savePlaceOrder = async () => {
  129. const token = getCartToken();
  130. await placeOrder({
  131. variables: {
  132. token: token || "",
  133. },
  134. });
  135. const isGuest = getCookie(IS_GUEST);
  136. if (isGuest === "true") {
  137. await resetGuestToken();
  138. }
  139. };
  140. return {
  141. isLoadingToSave,
  142. saveCheckoutAddress,
  143. isSaving,
  144. saveCheckoutShipping,
  145. isPaymentLoading,
  146. saveCheckoutPayment,
  147. isPlaceOrder,
  148. savePlaceOrder,
  149. };
  150. };