1
- ' use client' ;
2
- import { useState , ChangeEvent , MouseEvent , FormEvent } from ' react' ;
3
- import { QuestionBody , Difficulty , QuestionFullBody } from ' @/api/structs' ;
4
- import { addQuestion } from ' @/api/gateway' ;
1
+ " use client" ;
2
+ import { useState , ChangeEvent , MouseEvent , FormEvent } from " react" ;
3
+ import { QuestionBody , Difficulty , QuestionFullBody } from " @/api/structs" ;
4
+ import { addQuestion } from " @/api/gateway" ;
5
5
6
- type Props = { }
6
+ type Props = { } ;
7
7
8
8
interface Mapping {
9
- key : string ,
10
- value : string
9
+ key : string ;
10
+ value : string ;
11
11
}
12
12
13
13
function NewQuestion ( { } : Props ) {
14
- const [ testCases , setTestCases ] = useState < Mapping [ ] > ( [ {
15
- key : "" , value : ""
16
- } ] ) ;
14
+ const [ testCases , setTestCases ] = useState < Mapping [ ] > ( [
15
+ {
16
+ key : "" ,
17
+ value : "" ,
18
+ } ,
19
+ ] ) ;
17
20
const [ formData , setFormData ] = useState < QuestionBody > ( {
18
21
title : "" ,
19
22
difficulty : Difficulty . Easy ,
20
23
description : "" ,
24
+ categories : [ ] ,
21
25
} ) ;
22
26
23
- const handleTextInput = ( e : ChangeEvent < HTMLInputElement | HTMLTextAreaElement > ) => setFormData ( {
24
- ...formData ,
25
- [ e . target . name ] : e . target . value
26
- } ) ;
27
+ const handleTextInput = (
28
+ e : ChangeEvent < HTMLInputElement | HTMLTextAreaElement >
29
+ ) =>
30
+ setFormData ( {
31
+ ...formData ,
32
+ [ e . target . name ] : e . target . value ,
33
+ } ) ;
27
34
28
- const handleTestCaseInput = ( e : ChangeEvent < HTMLInputElement > , idx : number ) => {
35
+ const handleTestCaseInput = (
36
+ e : ChangeEvent < HTMLInputElement > ,
37
+ idx : number
38
+ ) => {
29
39
const values = [ ...testCases ] ;
30
40
values [ idx ] = {
31
41
...values [ idx ] ,
32
- [ e . target . name ] : e . target . value
42
+ [ e . target . name ] : e . target . value ,
33
43
} ;
34
- setTestCases ( values ) ;
35
- }
44
+ setTestCases ( values ) ;
45
+ } ;
36
46
37
47
const handleAddField = ( e : MouseEvent < HTMLElement > ) =>
38
- setTestCases ( [ ...testCases , { key : "" , value : "" } ] ) ;
48
+ setTestCases ( [ ...testCases , { key : "" , value : "" } ] ) ;
39
49
40
50
const handleDeleteField = ( e : MouseEvent < HTMLElement > , idx : number ) => {
41
51
const values = [ ...testCases ] ;
42
52
values . splice ( idx , 1 ) ;
43
53
setTestCases ( values ) ;
44
- }
45
-
54
+ } ;
55
+
46
56
const handleSubmission = async ( e : FormEvent < HTMLFormElement > ) => {
47
57
e . preventDefault ( ) ;
48
58
const question : QuestionFullBody = {
49
59
...formData ,
50
- test_cases : testCases . map ( ( elem : Mapping ) => ( {
51
- [ elem . key ] : elem . value
52
- } ) ) . reduce ( ( res , item ) => ( { ...res , ...item } ) , { } )
53
- }
60
+ test_cases : testCases
61
+ . map ( ( elem : Mapping ) => ( {
62
+ [ elem . key ] : elem . value ,
63
+ } ) )
64
+ . reduce ( ( res , item ) => ( { ...res , ...item } ) , { } ) ,
65
+ } ;
54
66
const status = await addQuestion ( question ) ;
55
67
if ( status . error ) {
56
68
console . log ( "Failed to add question." ) ;
57
69
console . log ( `Code ${ status . status } : ${ status . error } ` ) ;
58
70
return ;
59
71
}
60
72
console . log ( `Successfully added the question.` ) ;
61
- }
73
+ } ;
62
74
63
75
return (
64
76
< div >
65
- < form style = { { color : "black" , padding : "5px" } } onSubmit = { handleSubmission } >
66
- < input type = "text" name = "title" value = { formData . title } onChange = { handleTextInput } /> < br />
67
- < input type = "radio" id = "easy" name = "difficulty" value = { 1 } onChange = { handleTextInput } />
68
- < label htmlFor = "easy" > Easy</ label > < br />
69
- < input type = "radio" id = "med" name = "difficulty" value = { 2 } onChange = { handleTextInput } />
70
- < label htmlFor = "med" > Medium</ label > < br />
71
- < input type = "radio" id = "hard" name = "difficulty" value = { 3 } onChange = { handleTextInput } />
72
- < label htmlFor = "hard" > Hard</ label > < br />
73
- < textarea name = "description" value = { formData . description } onChange = { handleTextInput } /> < br />
77
+ < form
78
+ style = { { color : "black" , padding : "5px" } }
79
+ onSubmit = { handleSubmission }
80
+ >
81
+ < input
82
+ type = "text"
83
+ name = "title"
84
+ value = { formData . title }
85
+ onChange = { handleTextInput }
86
+ />
87
+ < br />
88
+ < input
89
+ type = "radio"
90
+ id = "easy"
91
+ name = "difficulty"
92
+ value = { 1 }
93
+ onChange = { handleTextInput }
94
+ />
95
+ < label htmlFor = "easy" > Easy</ label >
96
+ < br />
97
+ < input
98
+ type = "radio"
99
+ id = "med"
100
+ name = "difficulty"
101
+ value = { 2 }
102
+ onChange = { handleTextInput }
103
+ />
104
+ < label htmlFor = "med" > Medium</ label >
105
+ < br />
106
+ < input
107
+ type = "radio"
108
+ id = "hard"
109
+ name = "difficulty"
110
+ value = { 3 }
111
+ onChange = { handleTextInput }
112
+ />
113
+ < label htmlFor = "hard" > Hard</ label >
114
+ < br />
115
+ < textarea
116
+ name = "description"
117
+ value = { formData . description }
118
+ onChange = { handleTextInput }
119
+ />
120
+ < br />
74
121
{ testCases . map ( ( elem , idx ) => (
75
122
< >
76
123
< input
77
124
name = "key"
78
125
type = "text"
79
126
id = { `key_${ idx . toLocaleString ( ) } ` }
80
127
value = { elem . key }
81
- onChange = { e => handleTestCaseInput ( e , idx ) } />
128
+ onChange = { ( e ) => handleTestCaseInput ( e , idx ) }
129
+ />
82
130
< input
83
131
name = "value"
84
132
type = "text"
85
133
id = { `val_${ idx . toLocaleString ( ) } ` }
86
134
value = { elem . value }
87
- onChange = { e => handleTestCaseInput ( e , idx ) } />
135
+ onChange = { ( e ) => handleTestCaseInput ( e , idx ) }
136
+ />
88
137
< input
89
138
type = "button"
90
139
name = "del_entry"
91
140
value = "Delete..."
92
- onClick = { e => handleDeleteField ( e , idx ) }
93
- style = { { backgroundColor : "white" } } />
94
- < br />
141
+ onClick = { ( e ) => handleDeleteField ( e , idx ) }
142
+ style = { { backgroundColor : "white" } }
143
+ />
144
+ < br />
95
145
</ >
96
146
) ) }
97
147
< input
98
148
type = "button"
99
149
name = "add_entry"
100
150
value = "Add..."
101
151
onClick = { handleAddField }
102
- style = { { backgroundColor : "white" } } />
152
+ style = { { backgroundColor : "white" } }
153
+ />
103
154
< button
104
155
type = "submit"
105
156
name = "submit"
106
- style = { { backgroundColor : "white" } } > Submit</ button >
157
+ style = { { backgroundColor : "white" } }
158
+ >
159
+ Submit
160
+ </ button >
107
161
</ form >
108
162
</ div >
109
- )
163
+ ) ;
110
164
}
111
165
112
- export default NewQuestion ;
166
+ export default NewQuestion ;
0 commit comments