diff --git a/qcmplusweb/src/components/Exam/Exam.jsx b/qcmplusweb/src/components/Exam/Exam.jsx index 95e7fd9..2281f03 100644 --- a/qcmplusweb/src/components/Exam/Exam.jsx +++ b/qcmplusweb/src/components/Exam/Exam.jsx @@ -21,33 +21,17 @@ const Exam = ({ quizId }) => { const [timer, setTimer] = useState(QUESTION_TIME_LIMIT); const [score, setScore] = useState(0); - useEffect(() => { - const startQuiz = async () => { - setLoading(true); - try { - const response = await getQuestions(quizId); - if (response && response.data) { - const selectedQuestions = response.data.slice(0, MAX_QUESTIONS); - setQuestions(selectedQuestions); - setCurrentQuestionIndex(0); - setUserAnswers({}); - setExamCompleted(false); - setTimer(QUESTION_TIME_LIMIT); - } else { - setError('No questions found for this quiz.'); - } - } catch (error) { - console.error('Error fetching questions:', error); - setError('An error occurred while fetching questions.'); - } finally { - setLoading(false); + // Move the calculateScore function above the useCallback hooks + const calculateScore = useCallback(() => { + let score = 0; + questions.forEach((question) => { + const correctAnswer = answers[question.questionId]?.find(answer => answer.isCorrect); + if (correctAnswer && userAnswers[question.questionId] === correctAnswer.answerId) { + score += 1; } - }; - - if (quizId) { - startQuiz(); - } - }, [quizId]); + }); + return score; + }, [questions, answers, userAnswers]); const handleSubmit = useCallback(async () => { const sessionData = { @@ -57,7 +41,7 @@ const Exam = ({ quizId }) => { }; try { await submitExamSession(sessionData); - const calculatedScore = calculateScore(); + const calculatedScore = calculateScore(); // No warning now because calculateScore is defined before setScore(calculatedScore); setExamCompleted(true); setShowResults(true); @@ -75,6 +59,33 @@ const Exam = ({ quizId }) => { } }, [currentQuestionIndex, questions.length, handleSubmit]); + useEffect(() => { + const startQuiz = async () => { + setLoading(true); + try { + const response = await getQuestions(quizId); + if (response && response.data) { + const selectedQuestions = response.data.slice(0, MAX_QUESTIONS); + setQuestions(selectedQuestions); + setCurrentQuestionIndex(0); + setUserAnswers({}); + setExamCompleted(false); + setTimer(QUESTION_TIME_LIMIT); + } else { + setError('No questions found for this quiz.'); + } + } catch (error) { + console.error('Error fetching questions:', error); + setError('An error occurred while fetching questions.'); + } finally { + setLoading(false); + } + }; + + if (quizId) { + startQuiz(); + } + }, [quizId]); useEffect(() => { if (questions.length > 0 && timer > 0) { @@ -122,17 +133,6 @@ const Exam = ({ quizId }) => { } }; - const calculateScore = () => { - let score = 0; - questions.forEach((question) => { - const correctAnswer = answers[question.questionId]?.find(answer => answer.isCorrect); - if (correctAnswer && userAnswers[question.questionId] === correctAnswer.answerId) { - score += 1; - } - }); - return score; - }; - if (loading) { return (