From d1dd49a098f18eec854976c197dd4d755a615097 Mon Sep 17 00:00:00 2001 From: Teclit Date: Fri, 23 Aug 2024 00:44:10 +0200 Subject: [PATCH] fix (UI) [QCMPLUS-38] -update Header Signed-off-by: Teclit --- qcmplusweb/src/components/Exam/Exam.css | 2 +- qcmplusweb/src/components/Exam/Exam.jsx | 15 +++++------ .../src/components/Exam/ExamQuestion.jsx | 2 +- .../src/components/Exam/ExamResults.jsx | 3 +-- .../src/components/Exam/useExamTimer.jsx | 26 +++++++++---------- 5 files changed, 23 insertions(+), 25 deletions(-) diff --git a/qcmplusweb/src/components/Exam/Exam.css b/qcmplusweb/src/components/Exam/Exam.css index bc9d642..3950cc0 100644 --- a/qcmplusweb/src/components/Exam/Exam.css +++ b/qcmplusweb/src/components/Exam/Exam.css @@ -11,7 +11,7 @@ .exam-results-list { - max-height: 600px; + max-height: 500px; overflow-y: auto; padding: 10px; background-color: var(--bg-default-light-color); diff --git a/qcmplusweb/src/components/Exam/Exam.jsx b/qcmplusweb/src/components/Exam/Exam.jsx index 158b06b..af96210 100644 --- a/qcmplusweb/src/components/Exam/Exam.jsx +++ b/qcmplusweb/src/components/Exam/Exam.jsx @@ -9,7 +9,7 @@ import './Exam.css'; import {submitExamSession} from '../../services/ExamService'; //TODO: Replace with MAX_QUESTIONS to 20 AND QUESTION_TIME_LIMIT to 60 -const MAX_QUESTIONS = 5; +const MAX_QUESTIONS = 20; const QUESTION_TIME_LIMIT = 60; const Exam = ({quizId}) => { @@ -64,11 +64,10 @@ const Exam = ({quizId}) => { }, [questions, startTime]); const handleSubmit = useCallback(async () => { + if (!getUser || !getUser.userId) { + throw new Error('User is not authenticated'); + } try { - if (!getUser || !getUser.userId) { - throw new Error('User is not authenticated'); - } - const endTime = new Date(); const timeSpent = Math.floor((endTime - startTime) / 1000); // Time spent in seconds @@ -83,7 +82,7 @@ const Exam = ({quizId}) => { const examSessionObject = convertToExamSessionObject(sessionData); console.log("examSessionObject:", examSessionObject); - await submitExamSession(examSessionObject); + await submitExamSession(examSessionObject); setScore(calculateScore()); setExamCompleted(true); setShowResults(true); @@ -92,10 +91,10 @@ const Exam = ({quizId}) => { console.error('Unauthorized access - 401'); // Optionally handle token refresh or re-authentication here } else { - console.error('Error submitting exam session:', err); + console.error('Error submitting exam session:', err.message || err); } } - }, [getUser, quizId, userAnswers, calculateScore, startTime]); + }, [getUser, quizId, calculateScore, startTime]); diff --git a/qcmplusweb/src/components/Exam/ExamQuestion.jsx b/qcmplusweb/src/components/Exam/ExamQuestion.jsx index f92971a..b5a0ed7 100644 --- a/qcmplusweb/src/components/Exam/ExamQuestion.jsx +++ b/qcmplusweb/src/components/Exam/ExamQuestion.jsx @@ -12,7 +12,7 @@ const ExamQuestion = ({ handleSubmit, currentQuestionIndex, questionsLength, - timer // Receive the timer as a prop + timer }) => (

QUiz {quiz.title}

diff --git a/qcmplusweb/src/components/Exam/ExamResults.jsx b/qcmplusweb/src/components/Exam/ExamResults.jsx index 06b8d46..8e088d0 100644 --- a/qcmplusweb/src/components/Exam/ExamResults.jsx +++ b/qcmplusweb/src/components/Exam/ExamResults.jsx @@ -3,8 +3,7 @@ import {Alert, Container, ListGroup} from 'react-bootstrap'; const ExamResults = ({quiz, questions, answers, userAnswers, score}) => ( -

Quiz {quiz.title}

-

Exam Results: {score} / {questions.length}

+

{quiz.title} Quiz Results: {score} / {questions.length} Correct Answers


{questions.map((question, index) => ( diff --git a/qcmplusweb/src/components/Exam/useExamTimer.jsx b/qcmplusweb/src/components/Exam/useExamTimer.jsx index aadc084..a951095 100644 --- a/qcmplusweb/src/components/Exam/useExamTimer.jsx +++ b/qcmplusweb/src/components/Exam/useExamTimer.jsx @@ -1,22 +1,22 @@ import {useEffect, useState} from 'react'; -const useExamTimer = (initialTime, onTimeUp) => { - const [timer, setTimer] = useState(initialTime); +const useExamTimer = (initialTime, onTimeExpire) => { + const [timeLeft, setTimeLeft] = useState(initialTime); useEffect(() => { - if (timer > 0) { - const timerId = setTimeout(() => setTimer(timer - 1), 1000); - return () => clearTimeout(timerId); - } else { - onTimeUp(); + if (timeLeft <= 0) { + onTimeExpire(); + return; } - }, [timer, onTimeUp]); - const resetTimer = () => { - setTimer(initialTime); - }; + const intervalId = setInterval(() => { + setTimeLeft((prevTime) => prevTime - 1); + }, 1000); - return [timer, resetTimer]; + return () => clearInterval(intervalId); + }, [timeLeft, onTimeExpire]); + + return [timeLeft, setTimeLeft]; }; -export default useExamTimer; +export default useExamTimer; \ No newline at end of file