Debug join quiz

This commit is contained in:
NouhailaAater 2025-02-24 03:29:36 -05:00
parent 39ce176ae7
commit 94c728fa09

View file

@ -38,8 +38,14 @@ const ManageRoom: React.FC = () => {
const [currentQuestion, setCurrentQuestion] = useState<QuestionType | undefined>(undefined);
const [quizStarted, setQuizStarted] = useState(false);
const { selectedRoom } = useRooms();
const roomName = selectedRoom?.title || '';
const [roomName, setRoomName] = useState<string>(selectedRoom?.title || '');
useEffect(() => {
if (selectedRoom) {
setRoomName(selectedRoom.title);
}
}, [selectedRoom]);
useEffect(() => {
const fetchData = async () => {
@ -118,6 +124,10 @@ const ManageRoom: React.FC = () => {
console.error('ManageRoom: WebSocket connection error:', error);
});
socket.on('create-success', (roomName: string) => {
setRoomName(roomName);
});
socket.on('user-joined', (student: StudentType) => {
console.log(`Student joined: name = ${student.name}, id = ${student.id}`);
@ -156,13 +166,14 @@ const ManageRoom: React.FC = () => {
});
}
if (socket) {
// handle the case where user submits an answer
console.log(`Listening for submit-answer-room in room ${roomName}`);
socket.on('submit-answer-room', (answerData: AnswerReceptionFromBackendType) => {
const { answer, idQuestion, idUser, username } = answerData;
console.log(`Received answer from ${username} for question ${idQuestion}: ${answer}`);
console.log(
`Received answer from ${username} for question ${idQuestion}: ${answer}`
);
if (!quizQuestions) {
console.log('Quiz questions not found (cannot update answers without them).');
return;
@ -181,17 +192,33 @@ const ManageRoom: React.FC = () => {
console.log(`Comparing ${student.id} to ${idUser}`);
if (student.id === idUser) {
foundStudent = true;
const existingAnswer = student.answers.find((ans) => ans.idQuestion === idQuestion);
const existingAnswer = student.answers.find(
(ans) => ans.idQuestion === idQuestion
);
let updatedAnswers: Answer[] = [];
if (existingAnswer) {
// Update the existing answer
updatedAnswers = student.answers.map((ans) => {
console.log(`Comparing ${ans.idQuestion} to ${idQuestion}`);
return (ans.idQuestion === idQuestion ? { ...ans, answer, isCorrect: checkIfIsCorrect(answer, idQuestion, quizQuestions!) } : ans);
return ans.idQuestion === idQuestion
? {
...ans,
answer,
isCorrect: checkIfIsCorrect(
answer,
idQuestion,
quizQuestions!
)
}
: ans;
});
} else {
// Add a new answer
const newAnswer = { idQuestion, answer, isCorrect: checkIfIsCorrect(answer, idQuestion, quizQuestions!) };
const newAnswer = {
idQuestion,
answer,
isCorrect: checkIfIsCorrect(answer, idQuestion, quizQuestions!)
};
updatedAnswers = [...student.answers, newAnswer];
}
return { ...student, answers: updatedAnswers };
@ -206,7 +233,6 @@ const ManageRoom: React.FC = () => {
});
setSocket(socket);
}
}, [socket, currentQuestion, quizQuestions]);
const nextQuestion = () => {
@ -417,7 +443,6 @@ const ManageRoom: React.FC = () => {
<div className="dumb"></div>
</div>
{/* the following breaks the css (if 'room' classes are nested) */}
<div className="">
{quizQuestions ? (