add to cart online in khoshbinane
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { useMemo } from "react";
|
||||
import { useGetProfile } from "@/app/[name]/(Profile)/profile/hooks/userProfileData";
|
||||
import { useReceiptStore } from "@/zustand/receiptStore";
|
||||
import {
|
||||
@@ -39,7 +40,7 @@ export const useCart = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const getCartItems = () => {
|
||||
const cartItemsMap = useMemo(() => {
|
||||
if (isSuccess) {
|
||||
if (!cartItems?.data?.items) {
|
||||
return {};
|
||||
@@ -51,14 +52,14 @@ export const useCart = () => {
|
||||
} else {
|
||||
return items;
|
||||
}
|
||||
};
|
||||
}, [isSuccess, cartItems?.data?.items, items]);
|
||||
|
||||
const isItemLoading = () => {
|
||||
return false;
|
||||
};
|
||||
|
||||
return {
|
||||
items: getCartItems(),
|
||||
items: cartItemsMap,
|
||||
increment,
|
||||
decrement,
|
||||
clear,
|
||||
|
||||
@@ -29,6 +29,7 @@ export const useIncrementCart = () => {
|
||||
);
|
||||
|
||||
if (existingItem) {
|
||||
// بهروزرسانی خوشبینانه برای آیتم موجود
|
||||
const updatedItems = previousCart.data.items.map((item) => {
|
||||
if (item.foodId === String(id)) {
|
||||
return {
|
||||
@@ -47,6 +48,25 @@ export const useIncrementCart = () => {
|
||||
totalItems: previousCart.data.totalItems + 1,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// افزودن خوشبینانه برای آیتم جدید
|
||||
const newItem: CartItem = {
|
||||
foodId: String(id),
|
||||
foodTitle: "", // این بعداً از سرور پر میشود
|
||||
quantity: 1,
|
||||
price: 0,
|
||||
discount: 0,
|
||||
totalPrice: 0,
|
||||
};
|
||||
|
||||
queryClient.setQueryData<CartResponse>(["cart-items"], {
|
||||
...previousCart,
|
||||
data: {
|
||||
...previousCart.data,
|
||||
items: [...previousCart.data.items, newItem],
|
||||
totalItems: previousCart.data.totalItems + 1,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user