@@ -4,7 +4,7 @@ import { Props } from "./types";
44import Image from "next/image" ;
55import SelectionInput from "./SelectionInput" ;
66import { Button } from "./Button" ;
7- // import NumberInputComponent from "./NumberInputComponent";
7+ import NumberInputComponent from "./NumberInputComponent" ;
88import LoadingIndicator from "./LoadingIndicator" ;
99
1010const QuizForm : FC < Props > = ( {
@@ -15,10 +15,7 @@ const QuizForm: FC<Props> = ({
1515 totalQuestions,
1616 link,
1717} ) => {
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 ( ) ;
2219
2320 const [ showCorrectAnswer , setShowCorrectAnswer ] = useState < boolean > ( false ) ;
2421 const [ lastIndex , setLastIndex ] = useState < number > ( 1 ) ;
@@ -36,6 +33,10 @@ const QuizForm: FC<Props> = ({
3633 alt : string ;
3734 } | null > ( null ) ;
3835
36+ const [ isThinking , setIsThinking ] = useState < boolean > ( false ) ;
37+ const [ ollamaAvailable , setOllamaAvailable ] = useState < boolean > ( false ) ;
38+ const [ explanation , setExplanation ] = useState < string | null > ( null ) ;
39+
3940 useEffect ( ( ) => {
4041 const handleEsc = ( event : KeyboardEvent ) => {
4142 if ( event . key === "Escape" ) {
@@ -71,13 +72,6 @@ const QuizForm: FC<Props> = ({
7172 checkOllamaStatus ( ) ;
7273 } , [ ] ) ;
7374
74- const recordShowCorrectAnswer = ( ) => {
75- setShowCorrectAnswer ( ( prev ) => ( {
76- ...prev ,
77- [ currentQuestionIndex ] : true ,
78- } ) ) ;
79- } ;
80-
8175 const isOptionChecked = ( optionText : string ) : boolean | undefined => {
8276 const savedAnswer = savedAnswers [ currentQuestionIndex ] ;
8377 if ( savedAnswer === null ) {
@@ -128,10 +122,11 @@ const QuizForm: FC<Props> = ({
128122 } ;
129123
130124 if ( isLoading ) return < LoadingIndicator /> ;
125+
131126 //Error Handling for loading issues
132127 if ( ! questionSet ) {
133128 handleNextQuestion ( 1 ) ;
134- return < p > Loading questions failed</ p > ;
129+ return < p > Loading questions failed. </ p > ;
135130 }
136131
137132 const { question, options, images } = questionSet ! ;
@@ -148,54 +143,22 @@ const QuizForm: FC<Props> = ({
148143 } ;
149144
150145 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-
189146 return (
190147 < form onSubmit = { handleSubmit ( onSubmit ) } >
191148 < div className = "relative min-h-40" >
192149 < div className = "flex justify-center " >
193150 < button
194151 type = "button"
195152 onClick = { ( ) => {
153+ if ( currentQuestionIndex < lastIndex + 2 ) {
154+ setShowCorrectAnswer ( true ) ;
155+ } else {
156+ setShowCorrectAnswer ( false ) ;
157+ }
158+ reset ( ) ;
196159 handleNextQuestion ( currentQuestionIndex - 1 ) ;
197160 } }
198- disabled = { currentQuestionIndex == 1 }
161+ disabled = { ! ( currentQuestionIndex > 1 ) || ! canGoBack }
199162 className = "group"
200163 >
201164 < svg
@@ -217,31 +180,27 @@ const QuizForm: FC<Props> = ({
217180 < span className = "absolute text-white opacity-10 font-bold text-6xl bottom-0 -z-[1] select-none" >
218181 Q& A
219182 </ span >
220- { /* Debounce rerendering issue on prev next click
221- <NumberInputComponent
183+ < NumberInputComponent
222184 totalQuestions = { totalQuestions }
223185 currentQuestionIndex = { currentQuestionIndex }
224186 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- } }
236187 />
237188 < p className = "text-white text-md font-semibold text-center w-[40px] rounded-r-md border bg-slate-800 border-slate-700" >
238189 { totalQuestions }
239190 </ p >
240191 </ div >
241192 < button
242193 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 ) }
245204 className = "group"
246205 >
247206 < svg
@@ -309,17 +268,9 @@ const QuizForm: FC<Props> = ({
309268 type = { noOfAnswers > 1 ? "checkbox" : "radio" }
310269 label = { option . text }
311270 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 ) }
323274 />
324275 </ li >
325276 ) ) }
@@ -332,7 +283,7 @@ const QuizForm: FC<Props> = ({
332283 type = "submit"
333284 intent = "secondary"
334285 size = "medium"
335- disabled = { showCorrectAnswer [ currentQuestionIndex ] || false }
286+ disabled = { showCorrectAnswer }
336287 >
337288 Reveal Answer
338289 </ Button >
@@ -343,9 +294,10 @@ const QuizForm: FC<Props> = ({
343294 size = "medium"
344295 disabled = { isThinking }
345296 onClick = { ( ) => {
346- recordShowCorrectAnswer ( ) ;
297+ setShowCorrectAnswer ( true ) ;
347298 setIsThinking ( true ) ;
348299 explainCorrectAnswer ( ) ;
300+ reset ( ) ;
349301 } }
350302 >
351303 { isThinking ? "Thinking..." : "Explain" }
@@ -364,6 +316,7 @@ const QuizForm: FC<Props> = ({
364316 } ) ) ;
365317 }
366318 setShowCorrectAnswer ( false ) ;
319+ setExplanation ( null ) ;
367320 if ( currentQuestionIndex === totalQuestions ) {
368321 handleNextQuestion ( 1 ) ;
369322 setLastIndex ( 1 ) ;
0 commit comments