1- import { useEffect , useState } from "react" ;
1+ import { useEffect , useState , type FC } from "react" ;
22import Image from "next/image" ;
33import { Question } from "./types" ;
44import { FieldArray , FormikProvider , Field , useFormik } from "formik" ;
55import { Button } from "./Button" ;
66import useResults from "@azure-fundamentals/hooks/useResults" ;
7- import LoadingIndicator from "./LoadingIndicator" ;
87
98type Props = {
109 isLoading : boolean ;
@@ -25,7 +24,7 @@ type Props = {
2524 images ?: { url : string ; alt : string } [ ] ;
2625} ;
2726
28- const QuizExamForm : React . FC < Props > = ( {
27+ const QuizExamForm : FC < Props > = ( {
2928 isLoading,
3029 handleNextQuestion,
3130 handleSkipQuestion,
@@ -44,7 +43,8 @@ const QuizExamForm: React.FC<Props> = ({
4443 images,
4544} ) => {
4645 const [ showCorrectAnswer , setShowCorrectAnswer ] = useState < boolean > ( false ) ;
47- const { points, savedAnswers, setSavedAnswers } = useResults ( questions ) ;
46+ const [ savedAnswers , setSavedAnswers ] = useState < any > ( [ ] ) ;
47+ const { points, reCount } = useResults ( ) ;
4848 const [ selectedImage , setSelectedImage ] = useState < {
4949 url : string ;
5050 alt : string ;
@@ -64,7 +64,10 @@ const QuizExamForm: React.FC<Props> = ({
6464 ] ,
6565 } ,
6666 onSubmit : ( ) => {
67- saveAnswers ( false ) ;
67+ reCount ( {
68+ questions : questions ,
69+ answers : savedAnswers ,
70+ } ) ;
6871 stopTimer ( ) ;
6972 } ,
7073 } ) ;
@@ -85,25 +88,23 @@ const QuizExamForm: React.FC<Props> = ({
8588 }
8689 } , [ remainingTime ] ) ;
8790
88- const nextQuestion = ( skip : boolean ) => {
89- if ( skip === false ) {
91+ useEffect ( ( ) => {
92+ if ( savedAnswers . length > 0 ) {
9093 let done = true ;
9194 for ( let x = 0 ; x < savedAnswers . length ; x ++ ) {
92- if ( savedAnswers [ x ] === null && x !== currentQuestionIndex ) {
95+ if ( savedAnswers [ x ] === null ) {
9396 done = false ;
9497 break ;
9598 }
9699 }
97100 if ( done === true ) {
98- handleCountAnswered ( ) ;
99101 formik . submitForm ( ) ;
100- return ;
101- } else {
102- saveAnswers ( skip ) ;
103102 }
104- } else {
105- saveAnswers ( skip ) ;
106103 }
104+ } , [ savedAnswers ] ) ;
105+
106+ const nextQuestion = ( skip : boolean ) => {
107+ saveAnswers ( skip ) ;
107108 let areAllQuestionsAnswered = false ;
108109 let i = currentQuestionIndex + 1 ;
109110 while ( savedAnswers [ i ] !== null && i < totalQuestions ) {
@@ -187,7 +188,7 @@ const QuizExamForm: React.FC<Props> = ({
187188 setSavedAnswers ( saved ) ;
188189 } ;
189190
190- if ( isLoading ) return < LoadingIndicator / >;
191+ if ( isLoading ) return < p > Loading... </ p > ;
191192
192193 return (
193194 < FormikProvider value = { formik } >
@@ -300,7 +301,9 @@ const QuizExamForm: React.FC<Props> = ({
300301 } `}
301302 >
302303 < 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 ${
304307 showCorrectAnswer &&
305308 formik . values . options [ index ] ?. isAnswer
306309 ? "text-emerald-500 border-emerald-600"
@@ -317,7 +320,7 @@ const QuizExamForm: React.FC<Props> = ({
317320 : "hidden"
318321 } `}
319322 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 "
321324 clipRule = "evenodd"
322325 />
323326 </ svg >
0 commit comments