1
- import { usePathname , useRouter } from "next/navigation" ;
2
- import { useMutation } from "@tanstack/react-query" ;
3
- import { toast } from "react-hot-toast" ;
1
+ // This component needs to be refactored and optimized
2
+
3
+ import { useMutation , useQueryClient } from "@tanstack/react-query" ;
4
+ import persian_fa from "react-date-object/locales/persian_fa" ;
5
+ import persian from "react-date-object/calendars/persian" ;
6
+ import DatePicker from "react-multi-date-picker" ;
4
7
import { TbEdit } from "react-icons/tb" ;
8
+ import { toast } from "react-hot-toast" ;
5
9
import { useFormik } from "formik" ;
10
+ import Select from "react-select" ;
11
+ import { useState } from "react" ;
6
12
import * as Yup from "yup" ;
7
13
import {
8
14
Modal ,
@@ -14,58 +20,76 @@ import {
14
20
useDisclosure ,
15
21
} from "@nextui-org/react" ;
16
22
17
- import Box from "@mui/material/Box" ;
18
- import InputLabel from "@mui/material/InputLabel" ;
19
- import MenuItem from "@mui/material/MenuItem" ;
20
- import FormControl from "@mui/material/FormControl" ;
21
- import Select from "@mui/material/Select" ;
22
-
23
+ import { updateCoupon } from "@/services/adminServices" ;
24
+ import { useGetCourses } from "@/hooks/useProducts" ;
25
+ import RadioInput from "@/common/RadioInput" ;
23
26
import TextField from "@/common/TextField" ;
24
27
25
28
const initialValues = {
26
- type : "" ,
27
29
code : "" ,
28
30
amount : "" ,
29
31
usageLimit : "" ,
30
- productIds : "" ,
31
- expireDate : "" ,
32
32
} ;
33
33
34
- const validationSchema = Yup . object ( { } ) ;
34
+ const validationSchema = Yup . object ( {
35
+ code : Yup . string ( ) . required ( "این فیلد نمی تواند خالی باشد" ) ,
36
+ amount : Yup . number ( "عدد وارد کنید" ) . required ( "این فیلد نمی تواند خالی باشد" ) ,
37
+ usageLimit : Yup . number ( "عدد وارد کنید" ) . required ( "این فیلد نمی تواند خالی باشد" ) ,
38
+ } ) ;
35
39
36
- export default function UpdateCoupon ( { id, category } ) {
40
+ export default function UpdateCoupon ( { coupon } ) {
41
+ const [ type , setType ] = useState ( "" ) ;
42
+ const queryClient = useQueryClient ( ) ;
43
+ const [ productIds , setProductIds ] = useState ( [ ] ) ;
37
44
const { isOpen, onOpen, onOpenChange } = useDisclosure ( ) ;
38
- const router = useRouter ( ) ;
39
- const pathname = usePathname ( ) ;
45
+ const [ expireDate , setExpireDate ] = useState ( new Date ( ) ) ;
46
+
47
+ const { _id, code, usageLimit, amount } = coupon || { } ;
40
48
41
- // const { isLoading, mutateAsync } = useMutation({
42
- // mutationFn: updateCaoupon,
43
- // });
49
+ const { data : coursesData , isLoading : coursesLoading } = useGetCourses ( ) ;
50
+ const { products } = coursesData || { } ;
44
51
45
- // const sumbitHandler = async ({}) => {
46
- // const body = {} || {};
52
+ const { isLoading, mutateAsync } = useMutation ( {
53
+ mutationFn : updateCoupon ,
54
+ onSuccess : ( data ) => {
55
+ queryClient . invalidateQueries ( { queryKey : [ "get-coupons" ] } ) ;
56
+ toast . success ( data ?. message ) ;
57
+ } ,
58
+ onError : ( error ) => {
59
+ toast . error ( error ?. response ?. data ?. message ) ;
60
+ } ,
61
+ } ) ;
47
62
48
- // try {
49
- // const { message } = await mutateAsync({ body, id });
63
+ const sumbitHandler = async ( values ) => {
64
+ const data = {
65
+ ...values ,
66
+ type,
67
+ expireDate : new Date ( expireDate ) . toISOString ( ) ,
68
+ productIds : productIds ?. map ( ( product ) => product ?. _id ) ,
69
+ } ;
50
70
51
- // toast.success(message);
52
- // router.refresh(pathname);
53
- // } catch (error) {
54
- // toast.error(error?.response?.data?.message);
55
- // }
56
- // };
71
+ try {
72
+ const { message } = await mutateAsync ( {
73
+ id : _id ,
74
+ data,
75
+ } ) ;
76
+ } catch ( error ) { }
77
+ } ;
57
78
58
- // const formik = useFormik({
59
- // initialValues: initialValues,
60
- // onSubmit: sumbitHandler,
61
- // validationSchema: validationSchema,
62
- // validateOnMount: true,
63
- // });
79
+ const formik = useFormik ( {
80
+ initialValues : initialValues ,
81
+ onSubmit : sumbitHandler ,
82
+ validationSchema : validationSchema ,
83
+ validateOnMount : true ,
84
+ } ) ;
64
85
65
86
return (
66
87
< div >
67
88
< button onClick = { onOpen } >
68
- < TbEdit size = { 25 } className = "hover:text-slate-400 transition-all duration-250" />
89
+ < TbEdit
90
+ size = { 25 }
91
+ className = "hover:text-slate-400 transition-all duration-250"
92
+ />
69
93
</ button >
70
94
71
95
< Modal
@@ -79,12 +103,109 @@ export default function UpdateCoupon({ id, category }) {
79
103
< ModalContent className = "text-slate-900 bg-sky-100/70 p-3" >
80
104
{ ( onClose ) => (
81
105
< >
82
- < ModalHeader className = "text-xl font-extrabold" > آپدیت کردن کد تخفیف</ ModalHeader >
106
+ < ModalHeader className = "text-xl font-extrabold" >
107
+ آپدیت کردن کد تخفیف
108
+ </ ModalHeader >
109
+
110
+ < ModalBody >
111
+ < form
112
+ onSubmit = { formik . handleSubmit }
113
+ className = "space-y-5 md:p-10 p-5 rounded-xl"
114
+ >
115
+ < TextField
116
+ label = "کد"
117
+ name = "code"
118
+ formik = { formik }
119
+ placeholder = { code }
120
+ />
121
+
122
+ < TextField
123
+ label = "مقدار"
124
+ name = "amount"
125
+ type = "number"
126
+ formik = { formik }
127
+ placeholder = { amount }
128
+ />
129
+
130
+ < TextField
131
+ label = "ظرفیت"
132
+ name = "usageLimit"
133
+ type = "number"
134
+ formik = { formik }
135
+ placeholder = { usageLimit }
136
+ />
137
+
138
+ < div className = "flex flex-col gap-2" >
139
+ < span className = "" > نوع کد تخفیف</ span >
140
+ < div className = "flex items-center gap-4 border border-slate-300 rounded-xl p-3 w-[250px]" >
141
+ < RadioInput
142
+ id = "percent-type"
143
+ key = ""
144
+ label = "درصد"
145
+ name = "type"
146
+ value = "percent"
147
+ checked = { type === "percent" }
148
+ onChange = { ( e ) => setType ( e . target . value ) }
149
+ />
150
+
151
+ < RadioInput
152
+ id = "fixedProduct-type"
153
+ key = ""
154
+ label = "قیمت ثابت"
155
+ name = "type"
156
+ value = "fixedProduct"
157
+ checked = { type === "fixedProduct" }
158
+ onChange = { ( e ) => setType ( e . target . value ) }
159
+ />
160
+ </ div >
161
+ </ div >
162
+
163
+ < div className = "flex flex-col gap-2" >
164
+ < span className = "" > انتخاب دوره</ span >
165
+ < Select
166
+ instanceId = "products"
167
+ isMulti
168
+ onChange = { setProductIds }
169
+ options = { products }
170
+ getOptionLabel = { ( option ) => option . title }
171
+ getOptionValue = { ( option ) => option . _id }
172
+ />
173
+ </ div >
174
+
175
+ < div className = "flex flex-col gap-2" >
176
+ < span > تاریخ انقضا</ span >
177
+ < DatePicker
178
+ inputClass = "textField__input"
179
+ value = { expireDate }
180
+ onChange = { ( date ) => setExpireDate ( date ) }
181
+ format = "YYYY/MM/DD"
182
+ calendar = { persian }
183
+ locale = { persian_fa }
184
+ />
185
+ </ div >
83
186
84
- < ModalBody > </ ModalBody >
187
+ < Button
188
+ type = "submit"
189
+ color = "primary"
190
+ className = "w-full"
191
+ isLoading = { coursesLoading }
192
+ isDisabled = {
193
+ ! formik . isValid || ! type || ! productIds . length || ! expireDate
194
+ }
195
+ onPress = { onClose }
196
+ >
197
+ ثبت
198
+ </ Button >
199
+ </ form >
200
+ </ ModalBody >
85
201
86
202
< ModalFooter >
87
- < Button color = "danger" variant = "light" onPress = { onClose } className = "font-bold" >
203
+ < Button
204
+ color = "danger"
205
+ variant = "light"
206
+ onPress = { onClose }
207
+ className = "font-bold"
208
+ >
88
209
بستن
89
210
</ Button >
90
211
</ ModalFooter >
0 commit comments