| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- "use client";
- import { useCustomToast } from "./useToast";
- import { useAppDispatch } from "@/store/hooks";
- import { addItem, clearCart } from "@/store/slices/cart-slice";
- import { isObject } from "@utils/type-guards";
- import { getCartToken } from "@utils/getCartToken";
- import { getCookie } from "@utils/cookie-tools";
- import { useGuestCartToken } from "./useGuestCartToken";
- import { IS_GUEST } from "@/utils/constants";
- import { useMutation } from "@apollo/client/react";
- import {
- CREATE_ADD_PRODUCT_IN_CART,
- REMOVE_CART_ITEM,
- UPDATE_CART_ITEM,
- } from "@/graphql";
- import { AddToCartData, RemoveCartItemData } from "@/types/cart/type";
- export const useAddProduct = () => {
- const dispatch = useAppDispatch();
- const { createGuestToken, resetGuestToken } = useGuestCartToken();
- const { showToast } = useCustomToast();
- const [mutateAsync, { loading: isCartLoading }] = useMutation<AddToCartData>(
- CREATE_ADD_PRODUCT_IN_CART,
- {
- onCompleted: (res) => {
- const responseData = res?.createAddProductInCart?.addProductInCart;
- if (!responseData?.success) {
- showToast(responseData?.message || "Error adding to cart", "danger");
- return;
- }
- if (responseData) {
- if (responseData.success) {
- dispatch(addItem(responseData as any));
- showToast("Product added to cart successfully", "success");
- }
- }
- },
- onError: (err) => {
- showToast(err?.message ?? "Error", "danger");
- },
- },
- );
- const onAddToCart = async ({
- productId,
- quantity,
- variantId
- }: {
- productId: string;
- quantity: number;
- variantId?: number;
- token?: string;
- cartId?: number | string;
- }) => {
- // Ensure token exists - create if needed
- let token = getCartToken(); // 从cookie获取token
- if (!token) {
- token = await createGuestToken();
- if (!token) {
- showToast("Failed to create cart session", "danger");
- return;
- }
- }
- let param : { productId: number; quantity:number; variantId?: number; } = {
- productId: parseInt(productId),
- quantity,
- };
- if(variantId) {
- param.variantId = variantId;
- }
- let result = await mutateAsync({
- variables: param,
- });
- return {
- data: result.data,
- error: result.error,
- };
- };
- //--------Remove Cart Product Quantity--------//
- const [removeFromCart, { loading: isRemoveLoading }] = useMutation<RemoveCartItemData>(
- REMOVE_CART_ITEM,
- {
- onCompleted: async (response) => {
- const responseData = response?.createRemoveCartItem?.removeCartItem;
- if (isObject(responseData)) {
- const message = "Cart item removed successfully";
- dispatch(addItem(responseData as any));
- showToast(message as string, "warning");
- if (!responseData?.itemsQty) {
- dispatch(clearCart());
- const isGuest = getCookie(IS_GUEST);
- if (isGuest === "true") {
- resetGuestToken();
- }
- }
- } else {
- showToast("Something went wrong", "warning");
- }
- },
- onError: (error) => {
- showToast(error?.message as string, "danger");
- },
- },
- );
- const onAddToRemove = async (productId: string) => {
- await removeFromCart({
- variables: {
- cartItemId: parseInt(productId),
- },
- });
- };
- //---------Update Cart Product Quantity--------//
- const [updateCartItem, { loading: isUpdateLoading }] = useMutation(
- UPDATE_CART_ITEM,
- {
- onCompleted: (response: any) => {
- const responseData = response?.createUpdateCartItem?.updateCartItem;
- if (isObject(responseData)) {
- dispatch(addItem(responseData as any));
- } else {
- showToast("Something went wrong!", "warning");
- }
- },
- onError: (error) => {
- showToast(error?.message as string, "danger");
- },
- },
- );
- const onUpdateCart = async ({
- cartItemId,
- quantity,
- }: {
- cartItemId: number;
- quantity: number;
- }) => {
- if (quantity < 1) {
- showToast("Quantity must be at least 1", "warning");
- return;
- }
- await updateCartItem({
- variables: {
- cartItemId: cartItemId,
- quantity,
- },
- });
- };
- return {
- isCartLoading,
- onAddToCart,
- isRemoveLoading,
- onAddToRemove,
- onUpdateCart,
- isUpdateLoading,
- };
- };
|