From 510d1718f486f2e9f2f57cc1393c3fb7111d526c Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Thu, 18 Dec 2025 10:38:16 +0330 Subject: [PATCH] payble fix with discount --- .../(Dialogs)/cart/components/CartSummary.tsx | 15 ++++++- src/app/[name]/(Main)/types/Types.ts | 41 +++++++++++-------- src/components/listview/MenuItem.tsx | 38 +++++++++++++++-- 3 files changed, 71 insertions(+), 23 deletions(-) diff --git a/src/app/[name]/(Dialogs)/cart/components/CartSummary.tsx b/src/app/[name]/(Dialogs)/cart/components/CartSummary.tsx index d776785..c261917 100644 --- a/src/app/[name]/(Dialogs)/cart/components/CartSummary.tsx +++ b/src/app/[name]/(Dialogs)/cart/components/CartSummary.tsx @@ -9,6 +9,7 @@ import type { Food } from '@/app/[name]/(Main)/types/Types'; import { useCart } from '../hook/useCart'; import { useTranslation } from 'react-i18next'; import { useGetProfile } from '@/app/[name]/(Profile)/profile/hooks/userProfileData'; +import { useGetCartItems } from '../hooks/useCartData'; interface CartSummaryProps { description?: string; @@ -24,6 +25,7 @@ const CartSummary = ({ description = '', onDescriptionChange }: CartSummaryProps const { data: foodsResponse } = useGetFoods(); const foods = React.useMemo(() => foodsResponse?.data ?? [], [foodsResponse?.data]); const { items } = useCart(); + const { data: cartData } = useGetCartItems(isSuccess); const { t } = useTranslation('parallels', { keyPrefix: 'Cart' }); const cartFoods = React.useMemo(() => { @@ -37,11 +39,20 @@ const CartSummary = ({ description = '', onDescriptionChange }: CartSummaryProps }, [foods, items]); const totalPrice = React.useMemo(() => { + // اگر داده از API آمده و total وجود دارد، از آن استفاده می‌کنیم + if (isSuccess && cartData?.data?.total !== undefined) { + return cartData.data.total; + } + + // در غیر این صورت، محاسبه محلی با در نظر گرفتن تخفیف return cartFoods.reduce((sum, food) => { const qty = items[String(food.id)]?.quantity ?? 0; - return sum + (food.price ?? 0) * qty; + const basePrice = food.price ?? 0; + const discount = food.discount ?? 0; + const priceAfterDiscount = basePrice * (1 - discount / 100); + return sum + priceAfterDiscount * qty; }, 0); - }, [cartFoods, items]); + }, [cartFoods, items, isSuccess, cartData?.data?.total]); const isCartEmpty = cartFoods.length === 0; diff --git a/src/app/[name]/(Main)/types/Types.ts b/src/app/[name]/(Main)/types/Types.ts index 773eb59..fc358d6 100644 --- a/src/app/[name]/(Main)/types/Types.ts +++ b/src/app/[name]/(Main)/types/Types.ts @@ -12,7 +12,7 @@ export interface Category { deletedAt: string | null; title: string; isActive: boolean; - restId: string; + restaurant: string; avatarUrl: string | null; } @@ -22,23 +22,33 @@ export type FoodImage = string | { url: string; alt?: string | null }; export interface Food { id: string; - createdAt?: string; - updatedAt?: string; - deletedAt?: string | null; - restaurant?: string; - category?: Category; + createdAt: string; + updatedAt: string; + deletedAt: string | null; + restaurant: string; + category: Category; + inventory: string; + title: string; + desc: string; + content: string[]; + price: number; + order: number | null; + prepareTime: number | null; + weekDays: number[]; + mealTypes: unknown[]; + isActive: boolean; + images: string[]; + inPlaceServe: boolean; + pickupServe: boolean; + score: number; + discount: number; + isSpecialOffer: boolean; + // فیلدهای قدیمی برای سازگاری با سایر بخش‌های کد name?: string; - title?: string; foodName?: string; description?: string; - desc?: string | null; - content?: string[] | string | null; - price: number; image?: string | null; - images?: FoodImage[] | null; points?: number | null; - order?: number | null; - prepareTime?: number | null; sat?: boolean; sun?: boolean; mon?: boolean; @@ -47,12 +57,7 @@ export interface Food { dinner?: boolean; stock?: number; stockDefault?: number; - isActive?: boolean; - inPlaceServe?: boolean; - pickupServe?: boolean; rate?: number; - discount?: number; - isSpecialOffer?: boolean; [key: string]: unknown; } diff --git a/src/components/listview/MenuItem.tsx b/src/components/listview/MenuItem.tsx index eb73523..a3aae75 100644 --- a/src/components/listview/MenuItem.tsx +++ b/src/components/listview/MenuItem.tsx @@ -64,11 +64,30 @@ const MenuItem = ({ food }: MenuItemProps) => { return desc.replace(/,/g, '،'); }, [food.description, food.desc]); + const finalPrice = useMemo(() => { + const basePrice = food.price || 0; + const discount = food.discount || 0; + if (discount > 0) { + return Math.max(0, basePrice - discount); + } + return basePrice; + }, [food.price, food.discount]); + const formattedPrice = useMemo( + () => (finalPrice ? finalPrice.toLocaleString('fa-IR') : '0'), + [finalPrice] + ); + + const formattedOriginalPrice = useMemo( () => (food.price ? food.price.toLocaleString('fa-IR') : '0'), [food.price] ); + const hasDiscount = useMemo( + () => (food.discount || 0) > 0, + [food.discount] + ); + const handleAddToCart = () => { addToCart(food.id); }; @@ -104,9 +123,22 @@ const MenuItem = ({ food }: MenuItemProps) => {
- - {formattedPrice} T - +
+ {hasDiscount ? ( + <> + + {formattedOriginalPrice} T + + + {formattedPrice} T + + + ) : ( + + {formattedPrice} T + + )} +