From bf0a770ee0e003a6ab6c80b2902dbf88901965af Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Wed, 29 Oct 2025 10:41:57 +0330 Subject: [PATCH] add coupon + success + failed Page --- src/app/cart/components/DiscountCard.tsx | 65 ++++++---- src/app/cart/hooks/useCartData.ts | 6 + src/app/cart/payment/page.tsx | 31 +---- src/app/cart/service/Service.ts | 6 + src/app/cart/types/Types.ts | 37 +++++- src/app/checkout/thank-u/payment/page.tsx | 148 ++++++++++++++++++++++ src/share/components/Cart.tsx | 2 +- 7 files changed, 244 insertions(+), 51 deletions(-) create mode 100644 src/app/checkout/thank-u/payment/page.tsx diff --git a/src/app/cart/components/DiscountCard.tsx b/src/app/cart/components/DiscountCard.tsx index 1edf1bd..a8ab716 100644 --- a/src/app/cart/components/DiscountCard.tsx +++ b/src/app/cart/components/DiscountCard.tsx @@ -1,32 +1,50 @@ import { Button } from '@/components/ui/button' import Input from '@/components/Input' import { FC, useState } from 'react' +import { useAddCoupon, useGetCart } from '../hooks/useCartData' +import { toast } from '@/components/Toast' +import { extractErrorMessage } from '@/helpers/errorUtils' -type DiscountCardProps = { - discountCode: string - setDiscountCode: (code: string) => void - onSubmit: () => void - isLoading?: boolean -} -const DiscountCard: FC = ({ - discountCode, - setDiscountCode, - onSubmit, - isLoading = false -}) => { + +const DiscountCard: FC = () => { const [isExpanded, setIsExpanded] = useState(false) + const [couponCode, setCouponCode] = useState('') + const addCouponMutation = useAddCoupon() + const { data: cartData, refetch } = useGetCart() - const handleSubmit = (e: React.FormEvent) => { + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() - onSubmit() + + if (!couponCode.trim()) { + toast('لطفاً کد کوپن را وارد کنید', 'error') + return + } + + await addCouponMutation.mutateAsync({ + code: couponCode, + cartShipmentItemId: cartData?.results?.cart?.cartShipmentItems?.[0]._id as string + }, { + onSuccess: () => { + toast('کوپن با موفقیت اعمال شد', 'success') + refetch() + setCouponCode('') + setIsExpanded(false) + }, + onError: (error) => { + toast(extractErrorMessage(error), 'error') + } + }) + + } + if (!isExpanded) { return (
- کد تخفیف دارید؟ + کد کوپن دارید؟
+ +
+ + + ) + } + + // صفحه پرداخت موفق + if (status === 'success') { + return ( +
+
+ {/* آیکون موفقیت */} +
+
+ +
+
+ + {/* پیام اصلی */} +

+ پرداخت با موفقیت انجام شد +

+

+ سفارش شما ثبت شد و به زودی ارسال می‌شود +

+ + {/* اطلاعات سفارش */} +
+
+
+ شماره سفارش: + {orderId} +
+
+ کد پیگیری: + {paymentId} +
+
+
+ + {/* دکمه‌های اکشن */} +
+ + +
+
+
+ ) + } + + // در صورتی که status معتبر نباشد + return ( +
+
+
+ اطلاعات پرداخت نامعتبر است +
+ +
+
+ ) +} + +const ThankYouPage = () => { + return ( + + +
+ + } + > + +
+
+ ) +} + +export default ThankYouPage \ No newline at end of file diff --git a/src/share/components/Cart.tsx b/src/share/components/Cart.tsx index 88f09d6..2b2677b 100644 --- a/src/share/components/Cart.tsx +++ b/src/share/components/Cart.tsx @@ -145,7 +145,7 @@ const Cart = () => { {/* View Cart Button */} - {itemsCount > 0 && ( + {itemsCount > 0 && isLogin && (