mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Debug join quiz
This commit is contained in:
parent
39ce176ae7
commit
94c728fa09
1 changed files with 105 additions and 80 deletions
|
|
@ -38,8 +38,14 @@ const ManageRoom: React.FC = () => {
|
||||||
const [currentQuestion, setCurrentQuestion] = useState<QuestionType | undefined>(undefined);
|
const [currentQuestion, setCurrentQuestion] = useState<QuestionType | undefined>(undefined);
|
||||||
const [quizStarted, setQuizStarted] = useState(false);
|
const [quizStarted, setQuizStarted] = useState(false);
|
||||||
const { selectedRoom } = useRooms();
|
const { selectedRoom } = useRooms();
|
||||||
const roomName = selectedRoom?.title || '';
|
|
||||||
|
|
||||||
|
const [roomName, setRoomName] = useState<string>(selectedRoom?.title || '');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (selectedRoom) {
|
||||||
|
setRoomName(selectedRoom.title);
|
||||||
|
}
|
||||||
|
}, [selectedRoom]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
|
|
@ -118,6 +124,10 @@ const ManageRoom: React.FC = () => {
|
||||||
console.error('ManageRoom: WebSocket connection error:', error);
|
console.error('ManageRoom: WebSocket connection error:', error);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on('create-success', (roomName: string) => {
|
||||||
|
setRoomName(roomName);
|
||||||
|
});
|
||||||
|
|
||||||
socket.on('user-joined', (student: StudentType) => {
|
socket.on('user-joined', (student: StudentType) => {
|
||||||
console.log(`Student joined: name = ${student.name}, id = ${student.id}`);
|
console.log(`Student joined: name = ${student.name}, id = ${student.id}`);
|
||||||
|
|
||||||
|
|
@ -156,13 +166,14 @@ const ManageRoom: React.FC = () => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (socket) {
|
if (socket) {
|
||||||
// handle the case where user submits an answer
|
// handle the case where user submits an answer
|
||||||
console.log(`Listening for submit-answer-room in room ${roomName}`);
|
console.log(`Listening for submit-answer-room in room ${roomName}`);
|
||||||
socket.on('submit-answer-room', (answerData: AnswerReceptionFromBackendType) => {
|
socket.on('submit-answer-room', (answerData: AnswerReceptionFromBackendType) => {
|
||||||
const { answer, idQuestion, idUser, username } = answerData;
|
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) {
|
if (!quizQuestions) {
|
||||||
console.log('Quiz questions not found (cannot update answers without them).');
|
console.log('Quiz questions not found (cannot update answers without them).');
|
||||||
return;
|
return;
|
||||||
|
|
@ -181,17 +192,33 @@ const ManageRoom: React.FC = () => {
|
||||||
console.log(`Comparing ${student.id} to ${idUser}`);
|
console.log(`Comparing ${student.id} to ${idUser}`);
|
||||||
if (student.id === idUser) {
|
if (student.id === idUser) {
|
||||||
foundStudent = true;
|
foundStudent = true;
|
||||||
const existingAnswer = student.answers.find((ans) => ans.idQuestion === idQuestion);
|
const existingAnswer = student.answers.find(
|
||||||
|
(ans) => ans.idQuestion === idQuestion
|
||||||
|
);
|
||||||
let updatedAnswers: Answer[] = [];
|
let updatedAnswers: Answer[] = [];
|
||||||
if (existingAnswer) {
|
if (existingAnswer) {
|
||||||
// Update the existing answer
|
// Update the existing answer
|
||||||
updatedAnswers = student.answers.map((ans) => {
|
updatedAnswers = student.answers.map((ans) => {
|
||||||
console.log(`Comparing ${ans.idQuestion} to ${idQuestion}`);
|
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 {
|
} else {
|
||||||
// Add a new answer
|
// 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];
|
updatedAnswers = [...student.answers, newAnswer];
|
||||||
}
|
}
|
||||||
return { ...student, answers: updatedAnswers };
|
return { ...student, answers: updatedAnswers };
|
||||||
|
|
@ -206,7 +233,6 @@ const ManageRoom: React.FC = () => {
|
||||||
});
|
});
|
||||||
setSocket(socket);
|
setSocket(socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
}, [socket, currentQuestion, quizQuestions]);
|
}, [socket, currentQuestion, quizQuestions]);
|
||||||
|
|
||||||
const nextQuestion = () => {
|
const nextQuestion = () => {
|
||||||
|
|
@ -417,7 +443,6 @@ const ManageRoom: React.FC = () => {
|
||||||
<div className="dumb"></div>
|
<div className="dumb"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
{/* the following breaks the css (if 'room' classes are nested) */}
|
{/* the following breaks the css (if 'room' classes are nested) */}
|
||||||
<div className="">
|
<div className="">
|
||||||
{quizQuestions ? (
|
{quizQuestions ? (
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue