update cart
This commit is contained in:
@@ -53,7 +53,7 @@ export const useCart = () => {
|
||||
return {};
|
||||
}
|
||||
return cartItems.data.items.reduce((acc, item) => {
|
||||
acc[item.foodId] = { quantity: item.quantity };
|
||||
acc[item.productId] = { quantity: item.quantity };
|
||||
return acc;
|
||||
}, {} as Record<string | number, { quantity: number }>);
|
||||
} else {
|
||||
@@ -65,16 +65,14 @@ export const useCart = () => {
|
||||
return false;
|
||||
};
|
||||
|
||||
// برای کاربران لاگین شده، slug از API میآید (در cartItems.data.restaurantId)
|
||||
// برای کاربران لاگین شده، slug از API میآید (در cartItems.data.shopId)
|
||||
// برای کاربران لاگین نشده، slug از receiptStore میآید
|
||||
const cartSlug = useMemo(() => {
|
||||
if (isSuccess && cartItems?.data?.restaurantId) {
|
||||
// اگر از API آمده، میتوانیم restaurantId را برگردانیم
|
||||
// اما بهتر است slug را از URL بگیریم چون API ممکن است restaurantId را برگرداند نه slug
|
||||
if (isSuccess && cartItems?.data?.shopId) {
|
||||
return currentSlug;
|
||||
}
|
||||
return slug;
|
||||
}, [isSuccess, cartItems?.data?.restaurantId, currentSlug, slug]);
|
||||
}, [isSuccess, cartItems?.data?.shopId, currentSlug, slug]);
|
||||
|
||||
return {
|
||||
items: cartItemsMap,
|
||||
|
||||
@@ -28,7 +28,7 @@ export const useIncrementCart = () => {
|
||||
|
||||
if (previousCart?.data && productsData?.data) {
|
||||
const existingItem = previousCart.data.items.find(
|
||||
(item) => item.foodId === String(id)
|
||||
(item) => item.productId === String(id)
|
||||
);
|
||||
|
||||
let updatedItems: CartItem[];
|
||||
@@ -36,7 +36,7 @@ export const useIncrementCart = () => {
|
||||
|
||||
if (existingItem) {
|
||||
updatedItems = previousCart.data.items.map((item) => {
|
||||
if (item.foodId === String(id)) {
|
||||
if (item.productId === String(id)) {
|
||||
return {
|
||||
...item,
|
||||
quantity: item.quantity + 1,
|
||||
@@ -50,8 +50,8 @@ export const useIncrementCart = () => {
|
||||
const product = productsData.data.find((f) => String(f.id) === String(id));
|
||||
if (product) {
|
||||
const newItem: CartItem = {
|
||||
foodId: String(id),
|
||||
foodTitle: product.title || product.name || "",
|
||||
productId: String(id),
|
||||
productTitle: product.title || product.name || "",
|
||||
quantity: 1,
|
||||
price: product.price,
|
||||
discount: product.discount || 0,
|
||||
@@ -116,7 +116,7 @@ export const useDecrementCart = () => {
|
||||
if (previousCart?.data) {
|
||||
const updatedItems = previousCart.data.items
|
||||
.map((item: CartItem) => {
|
||||
if (item.foodId === String(id)) {
|
||||
if (item.productId === String(id)) {
|
||||
const newQuantity = item.quantity - 1;
|
||||
if (newQuantity <= 0) {
|
||||
return null;
|
||||
|
||||
@@ -12,20 +12,13 @@ import { useGetProducts } from '@/app/[name]/(Main)/hooks/useMenuData';
|
||||
import MenuItem from '@/components/listview/MenuItem';
|
||||
import MenuItemRenderer from '@/components/listview/MenuItemRenderer';
|
||||
import type { Product } from '@/app/[name]/(Main)/types/Types';
|
||||
import { useGetAbout } from '../../(Main)/about/hooks/useAboutData';
|
||||
import PagerModal from '@/components/pager/PagerModal';
|
||||
import Button from '@/components/button/PrimaryButton';
|
||||
import NotificationBellIcon from '@/components/icons/NotificationBellIcon';
|
||||
|
||||
const CartIndex = () => {
|
||||
const { t } = useTranslation('parallels', { keyPrefix: 'Cart' });
|
||||
const router = useRouter();
|
||||
const { clearCart, items } = useCart();
|
||||
const [showClearConfirm, setShowClearConfirm] = React.useState(false);
|
||||
const [showPagerModal, setShowPagerModal] = React.useState(false);
|
||||
const { data: productsResponse, isFetching } = useGetProducts();
|
||||
const { data: aboutData } = useGetAbout();
|
||||
const isPremium = aboutData?.data?.plan === 'premium';
|
||||
const products = React.useMemo(() => productsResponse?.data ?? [], [productsResponse?.data]);
|
||||
const isLoading = isFetching && !products.length;
|
||||
|
||||
@@ -102,36 +95,12 @@ const CartIndex = () => {
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex flex-col gap-4 pb-24">
|
||||
{!isPremium && (
|
||||
<div className='bg-container rounded-container p-4 border border-border shadow-sm'>
|
||||
<div className='flex items-start gap-3 mb-4'>
|
||||
<div className='shrink-0 mt-0.5'>
|
||||
<NotificationBellIcon width={24} height={24} className="currentColor" />
|
||||
</div>
|
||||
<div className='flex-1'>
|
||||
<p className='text-sm font-medium text-foreground mb-1'>
|
||||
ثبت سفارش
|
||||
</p>
|
||||
<p className='text-xs text-muted-foreground leading-5'>
|
||||
برای ثبت سفارش، لطفاً گارسون را صدا کنید
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
onClick={() => setShowPagerModal(true)}
|
||||
className='w-full dark:bg-white! dark:text-black! flex items-center justify-center gap-2'
|
||||
>
|
||||
<NotificationBellIcon width={18} height={18} className="text-white dark:text-black!" />
|
||||
<span>صدا کردن گارسون</span>
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
{cartProducts.map((product) => (
|
||||
<MenuItemRenderer key={product.id}>
|
||||
<MenuItem product={product} />
|
||||
</MenuItemRenderer>
|
||||
))}
|
||||
<CartSummary isPremium={isPremium} />
|
||||
<CartSummary isPremium />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -148,8 +117,6 @@ const CartIndex = () => {
|
||||
onClick={toggleClearConfirm}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<PagerModal visible={showPagerModal} onClose={() => setShowPagerModal(false)} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -7,12 +7,12 @@ export const bulkCart = async (params: BulkCartItem) => {
|
||||
};
|
||||
|
||||
export const increment = async (id: string | number) => {
|
||||
const { data } = await api.post(`/public/cart/items/${id}/increment`);
|
||||
const { data } = await api.post(`/public/cart/product/${id}/increment`);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const decrement = async (id: string | number) => {
|
||||
const { data } = await api.post(`/public/cart/items/${id}/decrement`);
|
||||
const { data } = await api.post(`/public/cart/product/${id}/decrement`);
|
||||
return data;
|
||||
};
|
||||
|
||||
|
||||
@@ -2,14 +2,14 @@ import { BaseResponse } from "@/app/[name]/(Main)/types/Types";
|
||||
|
||||
export type BulkCartItem = {
|
||||
items: {
|
||||
foodId: string;
|
||||
productId: string;
|
||||
quantity: number;
|
||||
}[];
|
||||
};
|
||||
|
||||
export interface CartItem {
|
||||
foodId: string;
|
||||
foodTitle: string;
|
||||
productId: string;
|
||||
productTitle: string;
|
||||
quantity: number;
|
||||
price: number;
|
||||
discount: number;
|
||||
@@ -24,8 +24,8 @@ export interface Coupon {
|
||||
|
||||
export interface CartData {
|
||||
userId: string;
|
||||
restaurantId: string;
|
||||
restaurantName: string;
|
||||
shopId: string;
|
||||
shopName: string;
|
||||
items: CartItem[];
|
||||
deliveryFee: number;
|
||||
subTotal: number;
|
||||
|
||||
@@ -289,7 +289,7 @@ const CustomMap: React.FC<CustomMapProps> = ({
|
||||
</button>
|
||||
</div>
|
||||
{isMapLoading && (
|
||||
<div className="absolute inset-0 bg-zinc-50/80 backdrop-blur-sm z-[400]">
|
||||
<div className="absolute inset-0 bg-zinc-50/80 backdrop-blur-sm z-400">
|
||||
<div className="grid grid-cols-3 gap-4 p-8 h-full">
|
||||
<Skeleton className="h-full" />
|
||||
<Skeleton className="h-full" />
|
||||
|
||||
@@ -33,7 +33,7 @@ export default function SplashScreen({
|
||||
initial={{ opacity: 1 }}
|
||||
exit={{ opacity: 0 }}
|
||||
transition={{ duration: 0.5 }}
|
||||
className="fixed inset-0 z-[9999] flex flex-col items-center justify-center"
|
||||
className="fixed inset-0 z-9999 flex flex-col items-center justify-center"
|
||||
>
|
||||
{/* دایرههای تزئینی پسزمینه */}
|
||||
<div className="absolute inset-0 overflow-hidden">
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
// export const API_BASE_URL = "https://dmenuplus-api.dev.danakcorp.com";
|
||||
// export const API_BASE_URL =
|
||||
// "https://dmenuplus-api-production.dev.danakcorp.com";
|
||||
export const API_BASE_URL = "http://192.168.99.209:4000";
|
||||
export const API_BASE_URL = "http://10.86.60.88:4000";
|
||||
export const TOKEN_NAME = "dmenu-t";
|
||||
export const REFRESH_TOKEN_NAME = "dmenu-rt";
|
||||
|
||||
@@ -55,12 +55,12 @@ const StepOtp = ({ phone, slug }: Props) => {
|
||||
const bulkCartItems = Object.entries(items).map(([key, value]) => {
|
||||
if (value.quantity > 0) {
|
||||
return {
|
||||
foodId: key,
|
||||
productId: key,
|
||||
quantity: value.quantity
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}).filter((item): item is { foodId: string; quantity: number } => item !== undefined)
|
||||
}).filter((item): item is { productId: string; quantity: number } => item !== undefined)
|
||||
if (!!bulkCartItems.length) {
|
||||
mutateBulkCart({ items: bulkCartItems }, {
|
||||
onSuccess: () => {
|
||||
|
||||
Reference in New Issue
Block a user