Skip to content

Commit a162e69

Browse files
Merge pull request #45 from zhuoyuT/zhuoyu-enhancement
fix attemp
2 parents ea311a7 + 737ef61 commit a162e69

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

app/modes/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Modes: NextPage<{ searchParams: { url: string; name: string } }> = ({
1919
<ExamLink
2020
href={{
2121
pathname: "/practice",
22-
query: { url },
22+
query: { url, name },
2323
}}
2424
heading="Practice mode"
2525
paragraph="Learn and familiarize yourself with the questions and answers without any time constraint."
@@ -30,7 +30,7 @@ const Modes: NextPage<{ searchParams: { url: string; name: string } }> = ({
3030
<ExamLink
3131
href={{
3232
pathname: "/exam",
33-
query: { url },
33+
query: { url, name },
3434
}}
3535
heading="Exam mode"
3636
paragraph="Put your knowledge to the test by answering a fixed number of randomly selected questions under a time

components/QuizExamForm.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ const QuizExamForm: React.FC<Props> = ({
4444
images,
4545
}) => {
4646
const [showCorrectAnswer, setShowCorrectAnswer] = useState<boolean>(false);
47-
const [savedAnswers, setSavedAnswers] = useState<any>([]);
48-
const { points, reCount } = useResults(savedAnswers);
47+
const { points, savedAnswers, setSavedAnswers } = useResults(questions);
4948
const [selectedImage, setSelectedImage] = useState<{
5049
url: string;
5150
alt: string;
@@ -65,12 +64,7 @@ const QuizExamForm: React.FC<Props> = ({
6564
],
6665
},
6766
onSubmit: () => {
68-
saveAnswers(false).then(() => {
69-
reCount({
70-
questions: questions,
71-
answers: savedAnswers,
72-
});
73-
});
67+
saveAnswers(false);
7468
stopTimer();
7569
},
7670
});

hooks/useResults.tsx

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
11
import { Question } from "@azure-fundamentals/components/types";
2-
import { useState } from "react";
3-
4-
export type ResultsData = {
5-
questions: Question[];
6-
answers: [] | (string | string[] | null)[];
7-
};
2+
import { useState, useEffect } from "react";
83

94
export type ResultsHook = {
105
points: number;
11-
reCount: (data: ResultsData) => void;
6+
setSavedAnswers: (data: any[]) => void;
7+
savedAnswers: any;
128
};
139

14-
const useResults = (data: ResultsData): ResultsHook => {
10+
const useResults = (questions: Question[]): ResultsHook => {
1511
const [points, setPoints] = useState(0);
12+
const [savedAnswers, setSavedAnswers] = useState<any>([]);
1613

17-
const countPoints = (data: ResultsData) => {
14+
const countPoints = () => {
1815
let points = 0;
19-
for (let i = 0; i < data.questions?.length; i++) {
20-
if (!data.answers[i] || !data.questions[i]) continue;
21-
const noOfAnswers = data.questions[i]?.options
22-
? data.questions[i]?.options?.filter((el: any) => el.isAnswer === true)
16+
for (let i = 0; i < questions?.length; i++) {
17+
if (!savedAnswers[i] || !questions[i]) continue;
18+
const noOfAnswers = questions[i]?.options
19+
? questions[i]?.options?.filter((el: any) => el.isAnswer === true)
2320
.length
2421
: 0;
2522
if (noOfAnswers > 1) {
2623
let partialPoints = 0;
2724
const pointRaise = Math.round((1 / noOfAnswers) * 100) / 100;
2825
let isOneBad = false;
2926
let pointRaisedCount = 0;
30-
if (Array.isArray(data.answers[i])) {
31-
for (const answer of data.answers[i] ?? []) {
27+
if (Array.isArray(savedAnswers[i])) {
28+
for (const answer of savedAnswers[i] ?? []) {
3229
if (
33-
data.questions[i]?.options
30+
questions[i]?.options
3431
?.filter((el: any) => el.isAnswer === true)
3532
.some((el: any) => el.text === answer)
3633
) {
3734
partialPoints = partialPoints + pointRaise;
3835
pointRaisedCount = pointRaisedCount + 1;
3936
}
4037
if (
41-
data.questions[i]?.options
38+
questions[i]?.options
4239
?.filter((el: any) => el.isAnswer === false)
4340
.some((el: any) => el.text === answer)
4441
) {
@@ -55,11 +52,10 @@ const useResults = (data: ResultsData): ResultsHook => {
5552
(partialPoints > 0 ? Math.round(partialPoints * 100) / 100 : 0);
5653
points = Math.round(points * 100) / 100;
5754
}
58-
} else if (noOfAnswers === 1 && !Array.isArray(data.answers[i])) {
55+
} else if (noOfAnswers === 1 && !Array.isArray(savedAnswers[i])) {
5956
if (
60-
data.questions[i]?.options?.filter(
61-
(el: any) => el.isAnswer === true,
62-
)[0]?.text === data.answers[i]
57+
questions[i]?.options?.filter((el: any) => el.isAnswer === true)[0]
58+
?.text === savedAnswers[i]
6359
) {
6460
points = Math.round((points + 1) * 100) / 100;
6561
}
@@ -68,9 +64,17 @@ const useResults = (data: ResultsData): ResultsHook => {
6864
setPoints(points);
6965
};
7066

67+
// Trigger countPoints when savedAnswers is updated
68+
useEffect(() => {
69+
if (savedAnswers.length === questions.length) {
70+
countPoints();
71+
}
72+
}, [savedAnswers]); // Depend on savedAnswers to trigger the effect
73+
7174
return {
7275
points: points,
73-
reCount: countPoints,
76+
setSavedAnswers: setSavedAnswers,
77+
savedAnswers: savedAnswers,
7478
};
7579
};
7680

0 commit comments

Comments
 (0)