This commit is contained in:
hamid zarghami
2026-06-28 15:40:50 +03:30
parent 50cb3ecc65
commit 1cf0069db3
8 changed files with 422 additions and 23 deletions
+26 -1
View File
@@ -3,6 +3,7 @@ import { useParams } from "next/navigation";
import { useGetProfile } from "@/app/[name]/(Profile)/profile/hooks/userProfileData";
import { useReceiptStore } from "@/zustand/receiptStore";
import {
useBulkCart,
useClearCart,
useDecrementCart,
useGetCartItems,
@@ -19,6 +20,7 @@ export const useCart = () => {
clear,
increment,
decrement,
setQuantity,
items,
slug,
setSlug,
@@ -32,6 +34,10 @@ export const useCart = () => {
isPending: isDecrementPending,
} = useDecrementCart();
const { mutate: mutateClearCart } = useClearCart();
const {
mutate: mutateBulkCart,
isPending: isBulkPending,
} = useBulkCart();
const { data: cartItems } = useGetCartItems(isSuccess);
const addToCart = (id: string | number) => {
@@ -62,6 +68,24 @@ export const useCart = () => {
}
};
const setCartQuantity = (id: string | number, quantity: number) => {
if (isSuccess) {
mutateBulkCart(
{ items: [{ variantId: String(id), quantity }] },
{
onError: (error) => {
toast(extractErrorMessage(error), "error");
},
}
);
} else {
if (currentSlug && slug !== currentSlug) {
setSlug(currentSlug);
}
setQuantity(id, quantity);
}
};
const clearCart = useCallback(() => {
if (isSuccess) {
mutateClearCart(undefined, {
@@ -92,7 +116,7 @@ export const useCart = () => {
}
}, [isSuccess, cartItems?.data?.items, items]);
const isCartMutating = isIncrementPending || isDecrementPending;
const isCartMutating = isIncrementPending || isDecrementPending || isBulkPending;
// برای کاربران لاگین شده، slug از API می‌آید (در cartItems.data.shopId)
// برای کاربران لاگین نشده، slug از receiptStore می‌آید
@@ -111,6 +135,7 @@ export const useCart = () => {
clear,
addToCart,
removeFromCart,
setCartQuantity,
clearCart,
isCartMutating,
};