EvalueTonSavoir/client/src/components/QuestionsDisplay/MultipleChoiceQuestionDisplay/MultipleChoiceQuestionDisplay.tsx

135 lines
6.2 KiB
TypeScript
Raw Normal View History

// MultipleChoiceQuestionDisplay.tsx
import React, { useEffect, useState } from 'react';
2024-03-29 20:08:34 -04:00
import '../questionStyle.css';
import { Button } from '@mui/material';
2025-01-26 09:33:42 -05:00
import { FormattedTextTemplate } from '../../GiftTemplate/templates/TextTypeTemplate';
import { MultipleChoiceQuestion } from 'gift-pegjs';
2025-03-08 11:05:25 -05:00
import { AnswerType } from 'src/pages/Student/JoinRoom/JoinRoom';
import { useQuizContext } from 'src/pages/Student/JoinRoom/QuizContext';
import { QuizContext } from 'src/pages/Student/JoinRoom/QuizContext';
2024-03-29 20:08:34 -04:00
const MultipleChoiceQuestionDisplay: React.FC = () => {
const { questions, index, answer, submitAnswer } = useQuizContext();
console.log('MultipleChoiceQuestionDisplay: passedAnswer', JSON.stringify(answer));
2024-03-29 20:08:34 -04:00
const question = questions[Number(index)].question as MultipleChoiceQuestion;
2025-03-21 11:05:16 -04:00
const [actualAnswer, setActualAnswer] = useState<AnswerType>(() => {
if (answer && answer.length > 0) {
return answer;
2025-03-21 11:05:16 -04:00
}
return [];
});
let disableButton = false;
if (submitAnswer === undefined) {
disableButton = true;
}
useEffect(() => {
console.log('MultipleChoiceQuestionDisplay: passedAnswer', JSON.stringify(answer));
if (answer !== undefined) {
setActualAnswer(answer);
2025-03-21 11:05:16 -04:00
} else {
setActualAnswer([]);
2025-03-21 00:25:25 -04:00
}
}, [answer, index]);
2024-03-29 20:08:34 -04:00
const handleOnClickAnswer = (choice: string) => {
setActualAnswer((answer) => {
console.log(`handleOnClickAnswer -- setAnswer(): prevAnswer: ${answer}, choice: ${choice}`);
2025-03-21 11:05:16 -04:00
const correctAnswersCount = question.choices.filter((c) => c.isCorrect).length;
if (correctAnswersCount === 1) {
// If only one correct answer, replace the current selection
return answer.includes(choice) ? [] : [choice];
2025-03-21 00:25:25 -04:00
} else {
2025-03-21 11:05:16 -04:00
// Allow multiple selections if there are multiple correct answers
if (answer.includes(choice)) {
2025-03-21 11:05:16 -04:00
// Remove the choice if it's already selected
return answer.filter((selected) => selected !== choice);
2025-03-21 11:05:16 -04:00
} else {
// Add the choice if it's not already selected
return [...answer, choice];
2025-03-21 11:05:16 -04:00
}
2025-03-21 00:25:25 -04:00
}
});
2024-03-29 20:08:34 -04:00
};
2025-03-21 00:25:25 -04:00
2024-03-29 20:08:34 -04:00
const alpha = Array.from(Array(26)).map((_e, i) => i + 65);
const alphabet = alpha.map((x) => String.fromCharCode(x));
2025-03-07 12:20:39 -05:00
2025-03-21 00:25:25 -04:00
return (
<QuizContext.Consumer>
{({ showAnswer }) => (
<div className="question-container">
<div className="question content">
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(question.formattedStem) }} />
</div>
<div className="choices-wrapper mb-1">
{question.choices.map((choice, i) => {
console.log(`answer: ${actualAnswer}, choice: ${choice.formattedText.text}`);
const selected = actualAnswer.includes(choice.formattedText.text) ? 'selected' : '';
return (
<div key={choice.formattedText.text + i} className="choice-container">
<Button
variant="text"
className="button-wrapper"
disabled={disableButton}
onClick={() => !showAnswer && handleOnClickAnswer(choice.formattedText.text)}
>
{showAnswer ? (
<div>{choice.isCorrect ? '✅' : '❌'}</div>
) : (
''
)}
<div className={`circle ${selected}`}>{alphabet[i]}</div>
<div className={`answer-text ${selected}`}>
<div
dangerouslySetInnerHTML={{
__html: FormattedTextTemplate(choice.formattedText),
}}
/>
</div>
{choice.formattedFeedback && showAnswer && (
<div className="feedback-container mb-1 mt-1/2">
<div
dangerouslySetInnerHTML={{
__html: FormattedTextTemplate(choice.formattedFeedback),
}}
/>
</div>
)}
</Button>
2024-03-29 20:08:34 -04:00
</div>
);
})}
</div>
{question.formattedGlobalFeedback && showAnswer && (
<div className="global-feedback mb-2">
<div
dangerouslySetInnerHTML={{
__html: FormattedTextTemplate(question.formattedGlobalFeedback),
}}
/>
2024-03-29 20:08:34 -04:00
</div>
)}
{!showAnswer && submitAnswer && (
<Button
variant="contained"
onClick={() =>
actualAnswer.length > 0 && submitAnswer && submitAnswer(actualAnswer)
}
disabled={answer.length === 0}
>
Répondre
</Button>
)}
2025-03-21 00:25:25 -04:00
</div>
2024-03-29 20:08:34 -04:00
)}
</QuizContext.Consumer>
2024-03-29 20:08:34 -04:00
);
};
export default MultipleChoiceQuestionDisplay;