Skip to content

Commit 791c04c

Browse files
committed
Add coupon to cart
1 parent 7398844 commit 791c04c

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/app/(user)/cart/CartSummary.js

+53-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11
"use client";
22

3-
import { useRouter } from "next/navigation";
3+
import { useState } from "react";
44
import toast from "react-hot-toast";
5+
import { useRouter } from "next/navigation";
56
import { Button } from "@nextui-org/react";
67
import { TbReceipt } from "react-icons/tb";
78
import { toPersianNumbersWithComma } from "@/utils/toPersianNumbers";
89
import { useMutation, useQueryClient } from "@tanstack/react-query";
910
import { createPayment } from "@/services/paymentService";
11+
import { addCouponToCart } from "@/services/cartService";
1012

1113
export default function CartSummary({ payDetail, setPaying }) {
14+
const [couponCode, setCouponCode] = useState("");
1215
const { totalOffAmount, totalPrice, totalGrossPrice } = payDetail;
1316
const queryClient = useQueryClient();
1417
const router = useRouter();
1518

1619
const { isLoading, mutateAsync } = useMutation({ mutationFn: createPayment });
1720

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+
1839
const createPaymentHandler = async () => {
1940
try {
2041
const { message, url } = await mutateAsync();
@@ -39,6 +60,35 @@ export default function CartSummary({ payDetail, setPaying }) {
3960

4061
<hr className="border-slate-500 mb-6" />
4162

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+
4292
<div className="mb-4 flex items-center justify-between">
4393
<span>جمع کل</span>
4494
<span>{toPersianNumbersWithComma(totalGrossPrice)}</span>
@@ -56,7 +106,8 @@ export default function CartSummary({ payDetail, setPaying }) {
56106
<div className="mb-6 flex items-center justify-between font-bold">
57107
<span>مبلغ قابل پرداخت</span>
58108
<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>
60111
</div>
61112
</div>
62113

src/services/cartService.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ export function addToCart(productId) {
77
export function removeFromCart(productId) {
88
return http.post("/cart/remove", { productId }).then(({ data }) => data.data);
99
}
10+
11+
export function addCouponToCart(couponCode) {
12+
return http.post("/cart/coupon", couponCode).then(({ data }) => data.data);
13+
}

0 commit comments

Comments
 (0)