From 182402e6213aa3e5a6d303af5bfca19546e7af10 Mon Sep 17 00:00:00 2001 From: JubaAzul <118773284+JubaAzul@users.noreply.github.com> Date: Thu, 20 Mar 2025 12:49:15 -0400 Subject: [PATCH] =?UTF-8?q?[BUG]=20Le=20progr=C3=A8s=20des=20=C3=A9tudiant?= =?UTF-8?q?s=20n'est=20pas=20sauvegard=C3=A9=20lorsqu'ils=20quittent=20et?= =?UTF-8?q?=20reviennent=20dans=20le=20quiz.=20Fixes=20#296?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pages/Teacher/ManageRoom/ManageRoom.tsx | 50 ++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx b/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx index 01d9c27..83ae9f6 100644 --- a/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx +++ b/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx @@ -30,6 +30,7 @@ const ManageRoom: React.FC = () => { const navigate = useNavigate(); const [socket, setSocket] = useState(null); const [students, setStudents] = useState([]); + const [allStudents, setAllStudents] = useState([]); const { quizId = '', roomName = '' } = useParams<{ quizId: string, roomName: string }>(); const [quizQuestions, setQuizQuestions] = useState(); const [quiz, setQuiz] = useState(null); @@ -46,7 +47,8 @@ const ManageRoom: React.FC = () => { if (newlyConnectedUser) { console.log(`Handling newly connected user: ${newlyConnectedUser.name}`); setStudents((prevStudents) => [...prevStudents, newlyConnectedUser]); - + setAllStudents((prevStudents) => [...prevStudents, newlyConnectedUser]); + // only send nextQuestion if the quiz has started if (!quizStarted) { console.log(`!quizStarted: returning.... `); @@ -134,6 +136,8 @@ const ManageRoom: React.FC = () => { setQuizQuestions(undefined); setCurrentQuestion(undefined); setStudents(new Array()); + setAllStudents(new Array()); + } }; @@ -245,6 +249,48 @@ const ManageRoom: React.FC = () => { } return updatedStudents; }); + setAllStudents((prevStudents) => { + let foundStudent = false; + const updatedStudents = prevStudents.map((student) => { + console.log(`Comparing ${student.id} to ${idUser}`); + if (student.id === idUser) { + foundStudent = true; + const existingAnswer = student.answers.find( + (ans) => ans.idQuestion === idQuestion + ); + let updatedAnswers: Answer[] = []; + if (existingAnswer) { + updatedAnswers = student.answers.map((ans) => { + console.log(`Comparing ${ans.idQuestion} to ${idQuestion}`); + return ans.idQuestion === idQuestion + ? { + ...ans, + answer, + isCorrect: checkIfIsCorrect( + answer, + idQuestion, + quizQuestions! + ) + } + : ans; + }); + } else { + const newAnswer = { + idQuestion, + answer, + isCorrect: checkIfIsCorrect(answer, idQuestion, quizQuestions!) + }; + updatedAnswers = [...student.answers, newAnswer]; + } + return { ...student, answers: updatedAnswers }; + } + return student; + }); + if (!foundStudent) { + console.log(`Student ${username} not found in the list.`); + } + return updatedStudents; + }); }); setSocket(socket); } @@ -496,7 +542,7 @@ const ManageRoom: React.FC = () => { socket={socket} questions={quizQuestions} showSelectedQuestion={showSelectedQuestion} - students={students} + students={allStudents} >