Skip to content

Commit 87d9b4a

Browse files
Fix types
1 parent 6d0bd8b commit 87d9b4a

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

components/QuizExamFormUF.tsx

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { useEffect, useState, type FC } from "react";
22
import Image from "next/image";
3-
import { Question } from "./types";
43
import { useForm, useFieldArray, Controller } from "react-hook-form";
54
import { Button } from "./Button";
65
import useResults from "@azure-fundamentals/hooks/useResults";
6+
import { Question, Option } from "@azure-fundamentals/components/types";
77

88
type 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

components/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ type Image = {
33
url: string;
44
};
55

6-
type Option = {
6+
export type Option = {
77
text: string;
88
isAnswer: boolean;
99
};

0 commit comments

Comments
 (0)