Skip to content

Commit

Permalink
refactor: when selecting total-questions-of-quiz - better to pass num…
Browse files Browse the repository at this point in the history
…ber (#964)
  • Loading branch information
shootermv committed Jun 18, 2024
1 parent caba804 commit b12b22d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/components/QuizTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ const QuizTemplate: React.FC = () => {
navigate(`/quizzes/${category}/questionsTotal`);
};

const startQuiz = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
const userAnswer = parseInt(e.currentTarget.value);
const shuffledQuiz = shuffle(filteredQuestions).slice(0, userAnswer);
const startQuiz = (quizQuestionsCount: number) => {
const shuffledQuiz = shuffle(filteredQuestions).slice(
0,
quizQuestionsCount
);

// Shuffle the answer options
const choicesArr: string[][] = shuffledQuiz.map(obj => {
Expand All @@ -80,7 +82,9 @@ const QuizTemplate: React.FC = () => {

setQuiz(shuffledQuiz);
setChoicesArr(choicesArr);
navigate(`/quizzes/${selectedCategory}/questions/1/of/${userAnswer}`);
navigate(
`/quizzes/${selectedCategory}/questions/1/of/${quizQuestionsCount}`
);
};

// Function to start a random quiz
Expand Down
8 changes: 3 additions & 5 deletions src/components/SelectQuestionsTotal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { QUESTION_NUMS } from "../constants";

interface SelectQuestionsTotalProps {
startQuiz: (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
startQuiz: (e: number) => void;
selectedCategory: string; // Add the selectedCategory prop
totalQuestions: number; // Add the totalQuestions prop
}
Expand All @@ -22,8 +22,7 @@ const SelectQuestionsTotal: React.FC<SelectQuestionsTotalProps> = ({
{availableQuizLengths.map((choice: number, index: number) => (
<button
className="select-btns"
onClick={e => startQuiz(e)}
value={choice}
onClick={() => startQuiz(choice)}
key={index}
>
{choice}
Expand All @@ -32,8 +31,7 @@ const SelectQuestionsTotal: React.FC<SelectQuestionsTotalProps> = ({

<button
className="select-btns"
onClick={startQuiz}
value={totalQuestions}
onClick={() => startQuiz(totalQuestions)}
>
All ({totalQuestions})
</button>
Expand Down

0 comments on commit b12b22d

Please sign in to comment.