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 6778b45..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(); }); @@ -98,36 +97,33 @@ 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); }); socket.on('user-disconnected', (userId: string) => { setUsers((prevUsers) => prevUsers.filter((user) => user.id !== userId)); - console.log(userId); }); 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; @@ -203,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]); }