add to cart online in khoshbinane
This commit is contained in:
+1
-1
File diff suppressed because one or more lines are too long
@@ -1,3 +1,4 @@
|
|||||||
|
import { useMemo } from "react";
|
||||||
import { useGetProfile } from "@/app/[name]/(Profile)/profile/hooks/userProfileData";
|
import { useGetProfile } from "@/app/[name]/(Profile)/profile/hooks/userProfileData";
|
||||||
import { useReceiptStore } from "@/zustand/receiptStore";
|
import { useReceiptStore } from "@/zustand/receiptStore";
|
||||||
import {
|
import {
|
||||||
@@ -39,7 +40,7 @@ export const useCart = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getCartItems = () => {
|
const cartItemsMap = useMemo(() => {
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
if (!cartItems?.data?.items) {
|
if (!cartItems?.data?.items) {
|
||||||
return {};
|
return {};
|
||||||
@@ -51,14 +52,14 @@ export const useCart = () => {
|
|||||||
} else {
|
} else {
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
};
|
}, [isSuccess, cartItems?.data?.items, items]);
|
||||||
|
|
||||||
const isItemLoading = () => {
|
const isItemLoading = () => {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
items: getCartItems(),
|
items: cartItemsMap,
|
||||||
increment,
|
increment,
|
||||||
decrement,
|
decrement,
|
||||||
clear,
|
clear,
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ export const useIncrementCart = () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (existingItem) {
|
if (existingItem) {
|
||||||
|
// بهروزرسانی خوشبینانه برای آیتم موجود
|
||||||
const updatedItems = previousCart.data.items.map((item) => {
|
const updatedItems = previousCart.data.items.map((item) => {
|
||||||
if (item.foodId === String(id)) {
|
if (item.foodId === String(id)) {
|
||||||
return {
|
return {
|
||||||
@@ -47,6 +48,25 @@ export const useIncrementCart = () => {
|
|||||||
totalItems: previousCart.data.totalItems + 1,
|
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,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -119,9 +119,9 @@ function Combobox({ title, options, placeholder, icon: Icon, expanded, selectedI
|
|||||||
>
|
>
|
||||||
<label
|
<label
|
||||||
htmlFor="content-select"
|
htmlFor="content-select"
|
||||||
className="pointer-events-none inline-flex flex-col items-end justify-center px-1 py-0 absolute -top-6 right-0 z-[2]"
|
className="pointer-events-none inline-flex flex-col items-end justify-center px-1 py-0 absolute -top-6 right-0 z-2"
|
||||||
>
|
>
|
||||||
<span className="relative text-foreground w-fit mt-[-1px] text-xs tracking-[0] leading-4 whitespace-nowrap">
|
<span className="relative text-foreground w-fit -mt-px text-xs tracking-[0] leading-4 whitespace-nowrap">
|
||||||
{title}
|
{title}
|
||||||
</span>
|
</span>
|
||||||
</label>
|
</label>
|
||||||
|
|||||||
@@ -144,6 +144,7 @@ const MenuItem = ({ food }: MenuItemProps) => {
|
|||||||
<button
|
<button
|
||||||
onClick={handleAddToCart}
|
onClick={handleAddToCart}
|
||||||
className="inline-flex w-full justify-center items-center gap-2"
|
className="inline-flex w-full justify-center items-center gap-2"
|
||||||
|
|
||||||
>
|
>
|
||||||
<PlusIcon />
|
<PlusIcon />
|
||||||
<div className="text-sm2 pt-0.5 font-normal text-foreground">
|
<div className="text-sm2 pt-0.5 font-normal text-foreground">
|
||||||
|
|||||||
Reference in New Issue
Block a user