@@ -4,7 +4,7 @@ import { Props } from "./types";
4
4
import Image from "next/image" ;
5
5
import SelectionInput from "./SelectionInput" ;
6
6
import { Button } from "./Button" ;
7
- // import NumberInputComponent from "./NumberInputComponent";
7
+ import NumberInputComponent from "./NumberInputComponent" ;
8
8
import LoadingIndicator from "./LoadingIndicator" ;
9
9
10
10
const QuizForm : FC < Props > = ( {
@@ -15,10 +15,7 @@ const QuizForm: FC<Props> = ({
15
15
totalQuestions,
16
16
link,
17
17
} ) => {
18
- const { register, handleSubmit, watch } = useForm ( ) ;
19
- const [ isThinking , setIsThinking ] = useState < boolean > ( false ) ;
20
- const [ ollamaAvailable , setOllamaAvailable ] = useState < boolean > ( false ) ;
21
- const [ explanation , setExplanation ] = useState < string | null > ( null ) ;
18
+ const { register, handleSubmit, reset, watch } = useForm ( ) ;
22
19
23
20
const [ showCorrectAnswer , setShowCorrectAnswer ] = useState < boolean > ( false ) ;
24
21
const [ lastIndex , setLastIndex ] = useState < number > ( 1 ) ;
@@ -36,6 +33,10 @@ const QuizForm: FC<Props> = ({
36
33
alt : string ;
37
34
} | null > ( null ) ;
38
35
36
+ const [ isThinking , setIsThinking ] = useState < boolean > ( false ) ;
37
+ const [ ollamaAvailable , setOllamaAvailable ] = useState < boolean > ( false ) ;
38
+ const [ explanation , setExplanation ] = useState < string | null > ( null ) ;
39
+
39
40
useEffect ( ( ) => {
40
41
const handleEsc = ( event : KeyboardEvent ) => {
41
42
if ( event . key === "Escape" ) {
@@ -71,13 +72,6 @@ const QuizForm: FC<Props> = ({
71
72
checkOllamaStatus ( ) ;
72
73
} , [ ] ) ;
73
74
74
- const recordShowCorrectAnswer = ( ) => {
75
- setShowCorrectAnswer ( ( prev ) => ( {
76
- ...prev ,
77
- [ currentQuestionIndex ] : true ,
78
- } ) ) ;
79
- } ;
80
-
81
75
const isOptionChecked = ( optionText : string ) : boolean | undefined => {
82
76
const savedAnswer = savedAnswers [ currentQuestionIndex ] ;
83
77
if ( savedAnswer === null ) {
@@ -128,10 +122,11 @@ const QuizForm: FC<Props> = ({
128
122
} ;
129
123
130
124
if ( isLoading ) return < LoadingIndicator /> ;
125
+
131
126
//Error Handling for loading issues
132
127
if ( ! questionSet ) {
133
128
handleNextQuestion ( 1 ) ;
134
- return < p > Loading questions failed</ p > ;
129
+ return < p > Loading questions failed. </ p > ;
135
130
}
136
131
137
132
const { question, options, images } = questionSet ! ;
@@ -148,54 +143,22 @@ const QuizForm: FC<Props> = ({
148
143
} ;
149
144
150
145
const noOfAnswers = options . filter ( ( el ) => el . isAnswer === true ) . length ;
151
-
152
- const handleNextQueClick = ( ) => {
153
- setExplanation ( null ) ;
154
- setSavedAnswers ( ( prev ) => ( {
155
- ...prev ,
156
- [ currentQuestionIndex ] : watchInput ,
157
- } ) ) ;
158
- handleNextQuestion ( currentQuestionIndex + 1 ) ;
159
- } ;
160
-
161
- const isOptionCheckedWithoutReveal = (
162
- optionText : string ,
163
- ) : boolean | undefined => {
164
- const savedAnswer = checkedAnswers [ currentQuestionIndex ] ;
165
- if ( savedAnswer ?. length ) {
166
- return savedAnswer . includes ( optionText ) ;
167
- } else {
168
- return false ;
169
- }
170
- } ;
171
-
172
- const handleRadioCheckboxClick = ( event : any , isItMulti : boolean = false ) => {
173
- const valueToManage = event . target . value ;
174
- let finalData = [ valueToManage ] ;
175
- if ( isItMulti ) {
176
- const savedData = checkedAnswers [ currentQuestionIndex ] || [ ] ;
177
- if ( savedData . includes ( valueToManage ) ) {
178
- finalData = savedData . filter ( ( item ) => item !== valueToManage ) ;
179
- } else {
180
- finalData = [ ...savedData , valueToManage ] ;
181
- }
182
- }
183
- setCheckedAnswers ( ( prev ) => ( {
184
- ...prev ,
185
- [ currentQuestionIndex ] : finalData ,
186
- } ) ) ;
187
- } ;
188
-
189
146
return (
190
147
< form onSubmit = { handleSubmit ( onSubmit ) } >
191
148
< div className = "relative min-h-40" >
192
149
< div className = "flex justify-center " >
193
150
< button
194
151
type = "button"
195
152
onClick = { ( ) => {
153
+ if ( currentQuestionIndex < lastIndex + 2 ) {
154
+ setShowCorrectAnswer ( true ) ;
155
+ } else {
156
+ setShowCorrectAnswer ( false ) ;
157
+ }
158
+ reset ( ) ;
196
159
handleNextQuestion ( currentQuestionIndex - 1 ) ;
197
160
} }
198
- disabled = { currentQuestionIndex == 1 }
161
+ disabled = { ! ( currentQuestionIndex > 1 ) || ! canGoBack }
199
162
className = "group"
200
163
>
201
164
< svg
@@ -217,31 +180,27 @@ const QuizForm: FC<Props> = ({
217
180
< span className = "absolute text-white opacity-10 font-bold text-6xl bottom-0 -z-[1] select-none" >
218
181
Q& A
219
182
</ span >
220
- { /* Debounce rerendering issue on prev next click
221
- <NumberInputComponent
183
+ < NumberInputComponent
222
184
totalQuestions = { totalQuestions }
223
185
currentQuestionIndex = { currentQuestionIndex }
224
186
handleNextQuestion = { handleNextQuestion }
225
- /> */ }
226
-
227
- < input
228
- className = "w-[40px] text-white rounded-l-md border outline-0 border-slate-700 bg-slate-900 text-center text-md [-moz-appearance:_textfield] [&::-webkit-inner-spin-button]:m-0 [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:m-0 [&::-webkit-outer-spin-button]:appearance-none"
229
- type = "number"
230
- min = { 0 }
231
- max = { totalQuestions }
232
- value = { currentQuestionIndex }
233
- onChange = { ( e ) => {
234
- handleNextQuestion ( Number ( e . target . value ) ) ;
235
- } }
236
187
/>
237
188
< p className = "text-white text-md font-semibold text-center w-[40px] rounded-r-md border bg-slate-800 border-slate-700" >
238
189
{ totalQuestions }
239
190
</ p >
240
191
</ div >
241
192
< button
242
193
type = "button"
243
- onClick = { handleNextQueClick }
244
- disabled = { currentQuestionIndex == totalQuestions }
194
+ onClick = { ( ) => {
195
+ if ( currentQuestionIndex < lastIndex ) {
196
+ setShowCorrectAnswer ( true ) ;
197
+ } else {
198
+ setShowCorrectAnswer ( false ) ;
199
+ }
200
+ reset ( ) ;
201
+ handleNextQuestion ( currentQuestionIndex + 1 ) ;
202
+ } }
203
+ disabled = { ! ( currentQuestionIndex < lastIndex ) }
245
204
className = "group"
246
205
>
247
206
< svg
@@ -309,17 +268,9 @@ const QuizForm: FC<Props> = ({
309
268
type = { noOfAnswers > 1 ? "checkbox" : "radio" }
310
269
label = { option . text }
311
270
isAnswer = { option . isAnswer }
312
- showCorrectAnswer = {
313
- showCorrectAnswer [ currentQuestionIndex ] || false
314
- }
315
- disabled = { showCorrectAnswer [ currentQuestionIndex ] || false }
316
- checked = {
317
- isOptionChecked ( option . text ) ||
318
- isOptionCheckedWithoutReveal ( option . text )
319
- }
320
- handleChange = { ( e : any ) => {
321
- handleRadioCheckboxClick ( e , noOfAnswers > 1 ) ;
322
- } }
271
+ showCorrectAnswer = { showCorrectAnswer }
272
+ disabled = { showCorrectAnswer }
273
+ defaultChecked = { isOptionChecked ( option . text ) }
323
274
/>
324
275
</ li >
325
276
) ) }
@@ -332,7 +283,7 @@ const QuizForm: FC<Props> = ({
332
283
type = "submit"
333
284
intent = "secondary"
334
285
size = "medium"
335
- disabled = { showCorrectAnswer [ currentQuestionIndex ] || false }
286
+ disabled = { showCorrectAnswer }
336
287
>
337
288
Reveal Answer
338
289
</ Button >
@@ -343,9 +294,10 @@ const QuizForm: FC<Props> = ({
343
294
size = "medium"
344
295
disabled = { isThinking }
345
296
onClick = { ( ) => {
346
- recordShowCorrectAnswer ( ) ;
297
+ setShowCorrectAnswer ( true ) ;
347
298
setIsThinking ( true ) ;
348
299
explainCorrectAnswer ( ) ;
300
+ reset ( ) ;
349
301
} }
350
302
>
351
303
{ isThinking ? "Thinking..." : "Explain" }
@@ -364,6 +316,7 @@ const QuizForm: FC<Props> = ({
364
316
} ) ) ;
365
317
}
366
318
setShowCorrectAnswer ( false ) ;
319
+ setExplanation ( null ) ;
367
320
if ( currentQuestionIndex === totalQuestions ) {
368
321
handleNextQuestion ( 1 ) ;
369
322
setLastIndex ( 1 ) ;
0 commit comments