2
2
3
3
import { topicsList } from "@/app/(auth)/match/page" ;
4
4
import { Button } from "@/components/ui/button" ;
5
- import {
6
- DialogClose ,
7
- DialogContent ,
8
- DialogHeader ,
9
- } from "@/components/ui/dialog" ;
10
5
import {
11
6
Form ,
12
7
FormControl ,
@@ -20,13 +15,33 @@ import { MultiSelect } from "@/components/ui/multiselect";
20
15
import { Textarea } from "@/components/ui/textarea" ;
21
16
import { QuestionDifficulty } from "@/types/find-match" ;
22
17
import { zodResolver } from "@hookform/resolvers/zod" ;
18
+ import { useEffect , useState } from "react" ;
23
19
import { useForm } from "react-hook-form" ;
24
20
import Swal from "sweetalert2" ;
25
21
import { z } from "zod" ;
22
+ import MoonLoader from "react-spinners/MoonLoader" ;
23
+ import { fetchSingleLeetcodeQuestion } from "@/api/leetcode-dashboard" ;
26
24
27
25
const QUESTION_SERVICE = process . env . NEXT_PUBLIC_QUESTION_SERVICE ;
28
26
29
- const EditQuestionDialog = ( { id } : { id : string } ) => {
27
+ interface EditQuestionDialogProp {
28
+ setClose : ( ) => void ;
29
+ questionId : string ;
30
+ }
31
+
32
+ const initialValues = {
33
+ questionTitle : "" ,
34
+ questionDifficulty : "" ,
35
+ questionTopics : [ ] ,
36
+ questionDescription : "" ,
37
+ } ;
38
+
39
+ const EditQuestionDialog = ( {
40
+ setClose,
41
+ questionId,
42
+ } : EditQuestionDialogProp ) => {
43
+ const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
44
+ const [ leetcodeData , setLeetcodeData ] = useState ( initialValues ) ;
30
45
const formSchema = z . object ( {
31
46
questionTitle : z . string ( ) . min ( 2 , {
32
47
message : "Title must be at least 2 characters." ,
@@ -44,49 +59,70 @@ const EditQuestionDialog = ({ id }: { id: string }) => {
44
59
45
60
const form = useForm < z . infer < typeof formSchema > > ( {
46
61
resolver : zodResolver ( formSchema ) ,
47
- defaultValues : {
48
- questionTitle : "" ,
49
- questionDifficulty : "" ,
50
- questionTopics : [ ] ,
51
- questionDescription : "" ,
52
- } ,
62
+ defaultValues : leetcodeData ,
53
63
} ) ;
54
64
55
- function onSubmit ( values : z . infer < typeof formSchema > ) {
56
- const url = `${ QUESTION_SERVICE } /${ id } /update` ;
57
- fetch ( url , {
58
- method : "POST" ,
59
- headers : {
60
- "Content-Type" : "application/json" ,
61
- } ,
62
- body : JSON . stringify ( values ) ,
63
- } )
64
- . then ( ( res ) => {
65
- if ( res . ok ) {
66
- Swal . fire ( {
67
- icon : "success" ,
68
- title : "Question updated successfully!" ,
69
- } ) ;
70
- } else {
71
- Swal . fire ( {
72
- icon : "error" ,
73
- title : "Failed to update question." ,
74
- } ) ;
75
- }
76
- } )
77
- . catch ( ( error ) => {
78
- Swal . fire ( {
79
- icon : "error" ,
80
- title : "Failed to update question." ,
81
- } ) ;
82
- } ) ;
65
+ const { reset } = form ;
66
+
67
+ useEffect ( ( ) => {
68
+ fetchSingleLeetcodeQuestion ( questionId ) . then ( ( resp ) => {
69
+ const questionData = {
70
+ questionTitle : resp . title ,
71
+ questionDifficulty : resp . complexity ,
72
+ questionTopics : resp . category . map ( ( x : string ) => x . toLowerCase ( ) ) ,
73
+ questionDescription : resp . description ,
74
+ } ;
75
+ console . log ( questionData ) ;
76
+ setLeetcodeData ( questionData ) ;
77
+ reset ( questionData ) ;
78
+ } ) ;
79
+ } , [ questionId , reset ] ) ;
80
+
81
+ async function onSubmit ( values : z . infer < typeof formSchema > ) {
82
+ console . log ( values ) ;
83
+ // setIsSubmitting(true);
84
+ // const url = `${QUESTION_SERVICE}/create`;
85
+ // await fetch(url, {
86
+ // method: "POST",
87
+ // headers: {
88
+ // "Content-Type": "application/json",
89
+ // },
90
+ // body: JSON.stringify({
91
+ // title: values.questionTitle,
92
+ // description: values.questionDescription,
93
+ // category: values.questionTopics,
94
+ // complexity: values.questionDifficulty,
95
+ // }),
96
+ // })
97
+ // .then((response) => {
98
+ // if (response.ok) {
99
+ // Swal.fire({
100
+ // icon: "success",
101
+ // title: "Question Added",
102
+ // text: "Question has been added successfully.",
103
+ // });
104
+ // }
105
+
106
+ // return response.json();
107
+ // })
108
+ // .catch((error) => {
109
+ // Swal.fire({
110
+ // icon: "error",
111
+ // title: "Question Add Failed",
112
+ // text: "Please try again later.",
113
+ // });
114
+ // })
115
+ // .finally(() => {
116
+ // setIsSubmitting(false);
117
+ // setClose();
118
+ // });
83
119
}
84
120
85
121
return (
86
- < DialogContent className = "bg-primary-700 h-80% " >
87
- < DialogHeader className = "text-[32px] font-semibold text-yellow-500" >
122
+ < div className = "bg-primary-700 p-10 w-[60vw] rounded-lg pb-14 " >
123
+ < div className = "text-[32px] font-semibold text-yellow-500" >
88
124
Edit Question
89
- </ DialogHeader >
125
+ </ div >
90
126
< Form { ...form } >
91
127
< form
92
128
onSubmit = { form . handleSubmit ( onSubmit ) } // Ensure form.handleSubmit is used correctly
@@ -123,7 +159,7 @@ const EditQuestionDialog = ({ id }: { id: string }) => {
123
159
className = "w-full bg-primary-800 text-white p-2 rounded-md border border-white capitalize"
124
160
{ ...field } // Connect field to the select element
125
161
>
126
- < option value = "" > Select difficulty</ option >
162
+ < div > Select difficulty</ div >
127
163
{ Object . values ( QuestionDifficulty ) . map ( ( qd ) => (
128
164
< option value = { qd } key = { qd } >
129
165
< span className = "capitalize" > { qd } </ span >
@@ -145,9 +181,9 @@ const EditQuestionDialog = ({ id }: { id: string }) => {
145
181
< FormControl >
146
182
< MultiSelect
147
183
options = { topicsList }
148
- onValueChange = { field . onChange } // Bind field.onChange for multi-select
149
- value = { field . value } // Bind the value for controlled component
150
- placeholder = "Select options "
184
+ onValueChange = { field . onChange }
185
+ value = { field . value }
186
+ placeholder = "Select topics "
151
187
variant = "inverted"
152
188
className = "bg-primary-800"
153
189
/>
@@ -167,19 +203,20 @@ const EditQuestionDialog = ({ id }: { id: string }) => {
167
203
< Textarea
168
204
placeholder = "Type your description here."
169
205
className = "text-white bg-primary-800"
170
- { ...field } // Bind the Textarea to form control
206
+ rows = { 6 }
207
+ { ...field }
171
208
/>
172
209
</ FormControl >
173
210
< FormMessage />
174
211
</ FormItem >
175
212
) }
176
213
/>
177
- < DialogClose >
178
- < Button type = "submit" > Submit </ Button >
179
- </ DialogClose >
214
+ < Button type = "submit" className = "mt-8" >
215
+ { isSubmitting ? < MoonLoader size = "20" /> : "Submit" }
216
+ </ Button >
180
217
</ form >
181
218
</ Form >
182
- </ DialogContent >
219
+ </ div >
183
220
) ;
184
221
} ;
185
222
0 commit comments