1
1
"use client" ;
2
2
3
- import { useRouter } from "next/navigation " ;
3
+ import { useState } from "react " ;
4
4
import toast from "react-hot-toast" ;
5
+ import { useRouter } from "next/navigation" ;
5
6
import { Button } from "@nextui-org/react" ;
6
7
import { TbReceipt } from "react-icons/tb" ;
7
8
import { toPersianNumbersWithComma } from "@/utils/toPersianNumbers" ;
8
9
import { useMutation , useQueryClient } from "@tanstack/react-query" ;
9
10
import { createPayment } from "@/services/paymentService" ;
11
+ import { addCouponToCart } from "@/services/cartService" ;
10
12
11
13
export default function CartSummary ( { payDetail, setPaying } ) {
14
+ const [ couponCode , setCouponCode ] = useState ( "" ) ;
12
15
const { totalOffAmount, totalPrice, totalGrossPrice } = payDetail ;
13
16
const queryClient = useQueryClient ( ) ;
14
17
const router = useRouter ( ) ;
15
18
16
19
const { isLoading, mutateAsync } = useMutation ( { mutationFn : createPayment } ) ;
17
20
21
+ const { isLoading : addCouponLoading , mutateAsync : addCouponMutate } = useMutation ( {
22
+ mutationFn : addCouponToCart ,
23
+ onSuccess : ( data ) => {
24
+ queryClient . invalidateQueries ( { queryKey : [ "get-user" ] } ) ;
25
+ toast . success ( "کد تخفیف با موفقیت اعمال شد" ) ;
26
+ } ,
27
+ onError : ( error ) => {
28
+ toast . error ( error ?. response ?. data ?. message ) ;
29
+ } ,
30
+ } ) ;
31
+
32
+ const addCouponToCartHandler = async ( e ) => {
33
+ e . preventDefault ( ) ;
34
+ try {
35
+ await addCouponMutate ( { couponCode } ) ;
36
+ } catch ( error ) { }
37
+ } ;
38
+
18
39
const createPaymentHandler = async ( ) => {
19
40
try {
20
41
const { message, url } = await mutateAsync ( ) ;
@@ -39,6 +60,35 @@ export default function CartSummary({ payDetail, setPaying }) {
39
60
40
61
< hr className = "border-slate-500 mb-6" />
41
62
63
+ < div className = "mb-2" > کد تخفیف</ div >
64
+
65
+ < form
66
+ onSubmit = { addCouponToCartHandler }
67
+ className = "flex items-center mb-4 gap-2"
68
+ >
69
+ < input
70
+ autoComplete = "off"
71
+ className = "textField__input py-2 rounded-xl"
72
+ type = "text"
73
+ name = "code"
74
+ id = "code"
75
+ value = { couponCode }
76
+ onChange = { ( e ) => setCouponCode ( e . target . value ) }
77
+ />
78
+
79
+ < Button
80
+ isLoading = { addCouponLoading }
81
+ isDisabled = { couponCode === "" }
82
+ className = "btn"
83
+ color = "primary"
84
+ type = "submit"
85
+ >
86
+ اعمال
87
+ </ Button >
88
+ </ form >
89
+
90
+ < hr className = "border-slate-500 mb-6" />
91
+
42
92
< div className = "mb-4 flex items-center justify-between" >
43
93
< span > جمع کل</ span >
44
94
< span > { toPersianNumbersWithComma ( totalGrossPrice ) } </ span >
@@ -56,7 +106,8 @@ export default function CartSummary({ payDetail, setPaying }) {
56
106
< div className = "mb-6 flex items-center justify-between font-bold" >
57
107
< span > مبلغ قابل پرداخت</ span >
58
108
< div className = "text-xl font-extrabold text-sky-500 flex items-center gap-1" >
59
- { toPersianNumbersWithComma ( totalPrice ) } < span className = "text-xs" > تومان</ span >
109
+ { toPersianNumbersWithComma ( totalPrice ) } { " " }
110
+ < span className = "text-xs" > تومان</ span >
60
111
</ div >
61
112
</ div >
62
113
0 commit comments