import React, { useEffect, useState } from 'react'; import { Socket } from 'socket.io-client'; import { ENV_VARIABLES } from '../../../constants'; import StudentModeQuiz from '../../../components/StudentModeQuiz/StudentModeQuiz'; import TeacherModeQuiz from '../../../components/TeacherModeQuiz/TeacherModeQuiz'; import webSocketService from '../../../services/WebsocketService'; import DisconnectButton from '../../../components/DisconnectButton/DisconnectButton'; import './joinRoom.css'; import { QuestionType } from '../../../Types/QuestionType'; import { TextField } from '@mui/material'; import LoadingButton from '@mui/lab/LoadingButton'; import LoginContainer from '../../../components/LoginContainer/LoginContainer' const JoinRoom: React.FC = () => { const [roomName, setRoomName] = useState(''); const [username, setUsername] = useState(''); const [socket, setSocket] = useState(null); const [isWaitingForTeacher, setIsWaitingForTeacher] = useState(false); const [question, setQuestion] = useState(); const [quizMode, setQuizMode] = useState(); const [questions, setQuestions] = useState([]); const [connectionError, setConnectionError] = useState(''); const [isConnecting, setIsConnecting] = useState(false); useEffect(() => { handleCreateSocket(); return () => { disconnect(); }; }, []); const handleCreateSocket = () => { const socket = webSocketService.connect(ENV_VARIABLES.VITE_BACKEND_URL); socket.on('join-success', () => { setIsWaitingForTeacher(true); setIsConnecting(false); console.log('Successfully joined the room.'); }); socket.on('next-question', (question: QuestionType) => { setQuizMode('teacher'); setIsWaitingForTeacher(false); setQuestion(question); }); socket.on('launch-student-mode', (questions: QuestionType[]) => { setQuizMode('student'); setIsWaitingForTeacher(false); setQuestions(questions); setQuestion(questions[0]); }); socket.on('end-quiz', () => { disconnect(); }); socket.on('join-failure', (message) => { console.log('Failed to join the room.'); setConnectionError(`Erreur de connexion : ${message}`); setIsConnecting(false); }); socket.on('connect_error', (error) => { switch (error.message) { case 'timeout': setConnectionError("Le serveur n'est pas disponible"); break; case 'websocket error': setConnectionError("Le serveur n'est pas disponible"); break; } setIsConnecting(false); console.log('Connection Error:', error.message); }); setSocket(socket); }; const disconnect = () => { webSocketService.disconnect(); setSocket(null); setQuestion(undefined); setIsWaitingForTeacher(false); setQuizMode(''); setRoomName(''); setUsername(''); setIsConnecting(false); }; const handleSocket = () => { setIsConnecting(true); setConnectionError(''); if (!socket?.connected) { handleCreateSocket(); } if (username && roomName) { webSocketService.joinRoom(roomName, username); } }; const handleOnSubmitAnswer = (answer: string | number | boolean, idQuestion: string) => { webSocketService.submitAnswer(roomName, answer, username, idQuestion); }; if (isWaitingForTeacher) { return (
Salle: {roomName}
En attente que le professeur lance le questionnaire...
); } switch (quizMode) { case 'student': return ( ); case 'teacher': return ( question && ( ) ); default: return ( setUsername(e.target.value)} placeholder="Nom d'utilisateur" sx={{ marginBottom: '1rem' }} fullWidth /> setRoomName(e.target.value)} placeholder="Numéro de la salle" sx={{ marginBottom: '1rem' }} fullWidth /> Rejoindre ); } }; export default JoinRoom;