diff --git a/app/(main)/quiz/history/page.tsx b/app/(main)/quiz/history/page.tsx index da8c73a..6e75bee 100644 --- a/app/(main)/quiz/history/page.tsx +++ b/app/(main)/quiz/history/page.tsx @@ -125,7 +125,7 @@ const QuizHistory: React.FC = () => {
Quizzes

You currently have completed {quiz?.quizzes ? quiz.quizzes.length : 0} quizzes.

- + diff --git a/app/(main)/quiz/page.tsx b/app/(main)/quiz/page.tsx index 8de15b4..2244109 100644 --- a/app/(main)/quiz/page.tsx +++ b/app/(main)/quiz/page.tsx @@ -1,4 +1,5 @@ 'use client'; +import './quiz.css'; import { useEffect, useState } from 'react'; import { Quiz } from '@/types'; import { QuizService } from '../../../service/QuizService'; @@ -32,6 +33,7 @@ const QuizPage: React.FC = () => { const [selectedOptions, setSelectedOptions] = useState<{ [key: number]: number | 0 }>({}); const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0); const [isDisabled, setIsDisabled] = useState(false); + const [isAnswered, setIsAnswered] = useState(false); const [addedOptions, setAddedOptions] = useState([]); const [stem, setStem] = useState(''); @@ -42,8 +44,44 @@ const QuizPage: React.FC = () => { const [showOptionDialog, setShowOptionDialog] = useState(false); const [activeTab, setActiveTab] = useState(0); + const [explanationsVisible, setExplanationsVisible] = useState<{ [key: number]: boolean }>({}); + const [numberOfIncorrectOptions, setNumberOfIncorrectOptions] = useState(0); + + const handleOptionClick = (mcqId: number, optionNo: number) => { + if (!currentQuestion) { + console.error("Current question is undefined."); + return; + } + + setSelectedOptions({ + ...selectedOptions, + [mcqId]: optionNo + }); + + const isCorrectAnswer = currentQuestion.options.find(option => option.no === optionNo)?.isAnswer; + const totalOptions = currentQuestion.options.length; + + if (!isCorrectAnswer) { + const newNumberOfIncorrectOptions = numberOfIncorrectOptions + 1; + setNumberOfIncorrectOptions(newNumberOfIncorrectOptions); + + if (newNumberOfIncorrectOptions === totalOptions - 1) { + console.log('totalOptions', totalOptions); + console.log('newNum of incorrect options', newNumberOfIncorrectOptions); + setIsAnswered(true); + } + } else { + setIsAnswered(true); + } + + setExplanationsVisible({ + ...explanationsVisible, + [optionNo]: true + }); + }; useEffect(() => { + setIsAnswered(false) const fetchData = async () => { try { const responseData = await QuizService.getQuizInProgress(); @@ -79,6 +117,8 @@ const QuizPage: React.FC = () => { const handleNextQuestion = () => { if (currentQuestionLength > 0) { if (currentQuestionIndex < currentQuestionLength - 1) { + setExplanationsVisible({}); + setIsAnswered(false); setCurrentQuestionIndex(currentQuestionIndex + 1); } } @@ -376,29 +416,25 @@ const QuizPage: React.FC = () => { {currentQuestion && (
- Question {currentQuestionIndex + 1} of {quiz.mcqs.length}: - +

{currentQuestion.skills.map((skill) => skill.name).join(', ')}

+

{currentQuestion.options.map((option) => ( -
+
handleOptionClick(currentQuestion.id, option.no)}> -
- {option.isAnswer && Correct Answer} -

{option.explanation}

-
+ {explanationsVisible[option.no] && ( +
+ {option.isAnswer && Correct Answer} +

{option.explanation}

+
+ )}
))}
@@ -415,42 +451,36 @@ const QuizPage: React.FC = () => { console.error('Quiz ID is undefined or selected option is null'); } }} + disabled={!isAnswered} > ) : ( <>

No more questions in this quiz. Click to submit.

)}
)} -
Topics
+
Topic
    - {currentQuestion.topics.map((topic) => ( -
  • {topic.name}
  • - ))} + {currentQuestion.topics.map((topic) => topic.name).join(', ')}
-
Skills
-
    - {currentQuestion.skills.map((skill) => ( -
  • {skill.name}
  • - ))} -
- -
+ +
)} diff --git a/app/(main)/quiz/quiz.css b/app/(main)/quiz/quiz.css index dd49bdd..46a029f 100644 --- a/app/(main)/quiz/quiz.css +++ b/app/(main)/quiz/quiz.css @@ -10,4 +10,8 @@ overflow-wrap: break-word !important; word-wrap: break-word !important; word-break: break-word !important; +} + +.card { + margin-bottom: 0px; /* Adjust this value as needed */ } \ No newline at end of file