@@ -15,9 +15,11 @@ import { MultiSelect } from "@/components/ui/multiselect";
15
15
import { Textarea } from "@/components/ui/textarea" ;
16
16
import { QuestionDifficulty } from "@/types/find-match" ;
17
17
import { zodResolver } from "@hookform/resolvers/zod" ;
18
+ import { useState } from "react" ;
18
19
import { useForm } from "react-hook-form" ;
19
20
import Swal from "sweetalert2" ;
20
21
import { z } from "zod" ;
22
+ import MoonLoader from "react-spinners/MoonLoader" ;
21
23
22
24
const QUESTION_SERVICE = process . env . NEXT_PUBLIC_QUESTION_SERVICE ;
23
25
@@ -26,6 +28,8 @@ interface AddQuestionDialogProp {
26
28
}
27
29
28
30
const AddQuestionDialog = ( { setClose } : AddQuestionDialogProp ) => {
31
+ const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
32
+
29
33
const formSchema = z . object ( {
30
34
questionTitle : z . string ( ) . min ( 2 , {
31
35
message : "Title must be at least 2 characters." ,
@@ -52,8 +56,8 @@ const AddQuestionDialog = ({ setClose }: AddQuestionDialogProp) => {
52
56
} ) ;
53
57
54
58
async function onSubmit ( values : z . infer < typeof formSchema > ) {
59
+ setIsSubmitting ( true ) ;
55
60
const url = `${ QUESTION_SERVICE } /create` ;
56
- console . log ( "url" , url ) ;
57
61
await fetch ( url , {
58
62
method : "POST" ,
59
63
headers : {
@@ -84,11 +88,14 @@ const AddQuestionDialog = ({ setClose }: AddQuestionDialogProp) => {
84
88
text : "Please try again later." ,
85
89
} ) ;
86
90
} )
87
- . finally ( ( ) => setClose ( ) ) ;
91
+ . finally ( ( ) => {
92
+ setIsSubmitting ( false ) ;
93
+ setClose ( ) ;
94
+ } ) ;
88
95
}
89
96
90
97
return (
91
- < div className = "bg-primary-700 p-10 w-[60vw] h-[80vh] rounded-lg" >
98
+ < div className = "bg-primary-700 p-10 w-[60vw] rounded-lg pb-14 " >
92
99
< div className = "text-[32px] font-semibold text-yellow-500" >
93
100
Add Question
94
101
</ div >
@@ -180,7 +187,9 @@ const AddQuestionDialog = ({ setClose }: AddQuestionDialogProp) => {
180
187
</ FormItem >
181
188
) }
182
189
/>
183
- < Button type = "submit" > Submit</ Button >
190
+ < Button type = "submit" className = "mt-8" >
191
+ { isSubmitting ? < MoonLoader size = "20" /> : "Submit" }
192
+ </ Button >
184
193
</ form >
185
194
</ Form >
186
195
</ div >
0 commit comments