From d995b259e3ffc9e0fcab73bac379f9070e44fdb3 Mon Sep 17 00:00:00 2001 From: Louis-Antoine Caron Date: Sat, 6 Apr 2024 19:25:30 -0400 Subject: [PATCH 1/2] fix bug for user join --- .../pages/Teacher/ManageRoom/ManageRoom.tsx | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx b/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx index 6778b45..4ae31ac 100644 --- a/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx +++ b/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx @@ -98,25 +98,7 @@ const ManageRoom: React.FC = () => { console.log('Error creating room.'); }); - socket.on('user-joined', (user: UserType) => { - console.log("USER JOINED ! ") - console.log("quizMode: ", quizMode) - - setUsers((prevUsers) => [...prevUsers, user]); - - // This doesn't relaunch the quiz for users that connected late - if (quizMode === 'teacher') { - console.log("TEACHER") - - webSocketService.nextQuestion(roomName, currentQuestion); - - } else if (quizMode === 'student') { - - console.log(quizQuestions); - webSocketService.launchStudentModeQuiz(roomName, quizQuestions); - - } - }); + socket.on('join-failure', (message) => { setConnectingError(message); setSocket(null); @@ -128,6 +110,22 @@ const ManageRoom: React.FC = () => { setSocket(socket); }; + useEffect(() => { + // This is here to make sure the correct value is sent when user join + if (socket) { + socket.on('user-joined', (user: UserType) => { + + setUsers((prevUsers) => [...prevUsers, user]); + + if (quizMode === 'teacher') { + webSocketService.nextQuestion(roomName, currentQuestion); + } else if (quizMode === 'student') { + webSocketService.launchStudentModeQuiz(roomName, quizQuestions); + } + }); + } + }, [currentQuestion, quizQuestions]); + const nextQuestion = () => { if (!quizQuestions || !currentQuestion || !quiz?.content) return; From 949f0d10cc1edcf202f3435ca0e07957a4e1739a Mon Sep 17 00:00:00 2001 From: Louis-Antoine Caron Date: Sat, 6 Apr 2024 19:27:01 -0400 Subject: [PATCH 2/2] clean console --- client/src/pages/Student/JoinRoom/JoinRoom.tsx | 4 ---- client/src/pages/Teacher/ManageRoom/ManageRoom.tsx | 4 +--- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/client/src/pages/Student/JoinRoom/JoinRoom.tsx b/client/src/pages/Student/JoinRoom/JoinRoom.tsx index 23263db..fee8fb1 100644 --- a/client/src/pages/Student/JoinRoom/JoinRoom.tsx +++ b/client/src/pages/Student/JoinRoom/JoinRoom.tsx @@ -42,24 +42,20 @@ const JoinRoom: React.FC = () => { console.log('Successfully joined the room.'); }); socket.on('next-question', (question: QuestionType) => { - console.log("NEXT MODE!") setQuizMode('teacher'); setIsWaitingForTeacher(false); setQuestion(question); }); socket.on('launch-student-mode', (questions: QuestionType[]) => { - console.log("STODENT MODE!") setQuizMode('student'); setIsWaitingForTeacher(false); setQuestions(questions); setQuestion(questions[0]); }); socket.on('end-quiz', () => { - console.log("END!") disconnect(); }); socket.on('join-failure', (message) => { - console.log("BIG FAIL!") console.log('Failed to join the room.'); setConnectionError(`Erreur de connexion : ${message}`); setIsConnecting(false); diff --git a/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx b/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx index 4ae31ac..5d475d0 100644 --- a/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx +++ b/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx @@ -83,7 +83,6 @@ const ManageRoom: React.FC = () => { setConnectingError(''); const socket = webSocketService.connect(ENV_VARIABLES.VITE_BACKEND_URL); - console.log(socket); socket.on('connect', () => { webSocketService.createRoom(); }); @@ -105,7 +104,6 @@ const ManageRoom: React.FC = () => { }); socket.on('user-disconnected', (userId: string) => { setUsers((prevUsers) => prevUsers.filter((user) => user.id !== userId)); - console.log(userId); }); setSocket(socket); }; @@ -201,7 +199,7 @@ const ManageRoom: React.FC = () => { const showSelectedQuestion = (questionIndex: number) => { if (quiz?.content && quizQuestions) { setCurrentQuestion(quizQuestions[questionIndex]); - console.log(quizQuestions[questionIndex]); + if (quizMode === 'teacher') { webSocketService.nextQuestion(roomName, quizQuestions[questionIndex]); }