1
- import { useEffect , useState } from "react" ;
1
+ import { useEffect , useState , type FC } from "react" ;
2
2
import Image from "next/image" ;
3
3
import { Question } from "./types" ;
4
4
import { FieldArray , FormikProvider , Field , useFormik } from "formik" ;
5
5
import { Button } from "./Button" ;
6
6
import useResults from "@azure-fundamentals/hooks/useResults" ;
7
- import LoadingIndicator from "./LoadingIndicator" ;
8
7
9
8
type Props = {
10
9
isLoading : boolean ;
@@ -25,7 +24,7 @@ type Props = {
25
24
images ?: { url : string ; alt : string } [ ] ;
26
25
} ;
27
26
28
- const QuizExamForm : React . FC < Props > = ( {
27
+ const QuizExamForm : FC < Props > = ( {
29
28
isLoading,
30
29
handleNextQuestion,
31
30
handleSkipQuestion,
@@ -44,7 +43,8 @@ const QuizExamForm: React.FC<Props> = ({
44
43
images,
45
44
} ) => {
46
45
const [ showCorrectAnswer , setShowCorrectAnswer ] = useState < boolean > ( false ) ;
47
- const { points, savedAnswers, setSavedAnswers } = useResults ( questions ) ;
46
+ const [ savedAnswers , setSavedAnswers ] = useState < any > ( [ ] ) ;
47
+ const { points, reCount } = useResults ( ) ;
48
48
const [ selectedImage , setSelectedImage ] = useState < {
49
49
url : string ;
50
50
alt : string ;
@@ -64,7 +64,10 @@ const QuizExamForm: React.FC<Props> = ({
64
64
] ,
65
65
} ,
66
66
onSubmit : ( ) => {
67
- saveAnswers ( false ) ;
67
+ reCount ( {
68
+ questions : questions ,
69
+ answers : savedAnswers ,
70
+ } ) ;
68
71
stopTimer ( ) ;
69
72
} ,
70
73
} ) ;
@@ -85,25 +88,23 @@ const QuizExamForm: React.FC<Props> = ({
85
88
}
86
89
} , [ remainingTime ] ) ;
87
90
88
- const nextQuestion = ( skip : boolean ) => {
89
- if ( skip === false ) {
91
+ useEffect ( ( ) => {
92
+ if ( savedAnswers . length > 0 ) {
90
93
let done = true ;
91
94
for ( let x = 0 ; x < savedAnswers . length ; x ++ ) {
92
- if ( savedAnswers [ x ] === null && x !== currentQuestionIndex ) {
95
+ if ( savedAnswers [ x ] === null ) {
93
96
done = false ;
94
97
break ;
95
98
}
96
99
}
97
100
if ( done === true ) {
98
- handleCountAnswered ( ) ;
99
101
formik . submitForm ( ) ;
100
- return ;
101
- } else {
102
- saveAnswers ( skip ) ;
103
102
}
104
- } else {
105
- saveAnswers ( skip ) ;
106
103
}
104
+ } , [ savedAnswers ] ) ;
105
+
106
+ const nextQuestion = ( skip : boolean ) => {
107
+ saveAnswers ( skip ) ;
107
108
let areAllQuestionsAnswered = false ;
108
109
let i = currentQuestionIndex + 1 ;
109
110
while ( savedAnswers [ i ] !== null && i < totalQuestions ) {
@@ -187,7 +188,7 @@ const QuizExamForm: React.FC<Props> = ({
187
188
setSavedAnswers ( saved ) ;
188
189
} ;
189
190
190
- if ( isLoading ) return < LoadingIndicator / >;
191
+ if ( isLoading ) return < p > Loading... </ p > ;
191
192
192
193
return (
193
194
< FormikProvider value = { formik } >
@@ -300,7 +301,9 @@ const QuizExamForm: React.FC<Props> = ({
300
301
} `}
301
302
>
302
303
< svg
303
- className = { `border rounded-full absolute h-5 w-5 p-0.5 ${
304
+ className = { `border ${
305
+ noOfAnswers > 1 ? "rounded" : "rounded-full"
306
+ } absolute h-5 w-5 p-0.5 ${
304
307
showCorrectAnswer &&
305
308
formik . values . options [ index ] ?. isAnswer
306
309
? "text-emerald-500 border-emerald-600"
@@ -317,7 +320,7 @@ const QuizExamForm: React.FC<Props> = ({
317
320
: "hidden"
318
321
} `}
319
322
fillRule = "evenodd"
320
- d = "M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM5.354 4.646a.5.5 0 1 0-.708.708L7.293 8l-2.647 2.646a.5.5 0 0 0 .708.708L8 8.707l2.646 2.647a.5.5 0 0 0 .708-.708L8.707 8l2.647-2.646a.5.5 0 0 0-.708-.708L8 7.293 5.354 4.646z "
323
+ d = "M 2 0 a 2 2 0 0 0 -2 2 v 12 a 2 2 0 0 0 2 2 h 12 a 2 2 0 0 0 2 -2 V 2 a 2 2 0 0 0 -2 -2 H 2 z z "
321
324
clipRule = "evenodd"
322
325
/>
323
326
</ svg >
0 commit comments