11import { useEffect , useState , type FC } from "react" ;
22import Image from "next/image" ;
3- import { Question } from "./types" ;
43import { useForm , useFieldArray , Controller } from "react-hook-form" ;
54import { Button } from "./Button" ;
65import useResults from "@azure-fundamentals/hooks/useResults" ;
6+ import { Question , Option } from "@azure-fundamentals/components/types" ;
77
88type Props = {
99 isLoading : boolean ;
@@ -14,7 +14,7 @@ type Props = {
1414 totalQuestions : number ;
1515 question : string ;
1616 questions : Question [ ] ;
17- options : any ;
17+ options : Option [ ] ;
1818 stopTimer : ( ) => void ;
1919 getResultPoints : ( data : number ) => void ;
2020 revealExam ?: boolean ;
@@ -64,7 +64,7 @@ const QuizExamForm: FC<Props> = ({
6464 name : "options" ,
6565 } ) ;
6666
67- const onSubmit = ( data ) => {
67+ const onSubmit = ( ) => {
6868 reCount ( { questions : questions , answers : savedAnswers } ) ;
6969 stopTimer ( ) ;
7070 } ;
@@ -133,7 +133,7 @@ const QuizExamForm: FC<Props> = ({
133133 return options . some ( ( option ) => option . checked ) ;
134134 } ;
135135
136- const isOptionChecked = ( optionText : string ) : boolean | undefined => {
136+ const isOptionChecked = ( optionText : string ) : boolean => {
137137 const savedAnswer = savedAnswers [ currentQuestionIndex ] ;
138138 return typeof savedAnswer === "string" || ! savedAnswer
139139 ? savedAnswer === optionText
@@ -155,28 +155,17 @@ const QuizExamForm: FC<Props> = ({
155155 } , [ options ] ) ;
156156
157157 const saveAnswers = async ( skip = false ) => {
158+ const saved = [ ...savedAnswers ] ;
158159 if ( skip ) {
159- let saved = [ ...savedAnswers ] ;
160160 saved [ currentQuestionIndex ] = null ;
161- setSavedAnswers ( saved ) ;
162- return ;
161+ } else {
162+ const options = watch ( "options" ) ;
163+ const selectedArr = options
164+ . filter ( ( opt ) => opt . checked )
165+ . map ( ( opt ) => opt . text ) ;
166+ saved [ currentQuestionIndex ] =
167+ noOfAnswers > 1 ? selectedArr : selectedArr [ 0 ] || null ;
163168 }
164-
165- const options = watch ( "options" ) ;
166- let selectedArr = [ ] ;
167- let selected = null ;
168-
169- options . forEach ( ( answer ) => {
170- if ( answer . checked && noOfAnswers > 1 ) {
171- selectedArr . push ( answer . text ) ;
172- } else if ( answer . checked && noOfAnswers === 1 ) {
173- selected = answer . text ;
174- }
175- } ) ;
176-
177- let saved = [ ...savedAnswers ] ;
178- saved [ currentQuestionIndex ] =
179- noOfAnswers > 1 && selectedArr . length > 0 ? selectedArr : selected ;
180169 setSavedAnswers ( saved ) ;
181170 } ;
182171
0 commit comments