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

134 lines
5.6 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';
2024-03-29 20:08:34 -04:00
interface Props {
question: MultipleChoiceQuestion;
2025-03-08 11:05:25 -05:00
handleOnSubmitAnswer?: (answer: AnswerType) => void;
2024-03-29 20:08:34 -04:00
showAnswer?: boolean;
2025-03-08 11:05:25 -05:00
passedAnswer?: AnswerType;
2024-03-29 20:08:34 -04:00
}
const MultipleChoiceQuestionDisplay: React.FC<Props> = (props) => {
const { question, showAnswer, handleOnSubmitAnswer, passedAnswer } = props;
2025-03-21 11:05:16 -04:00
console.log('MultipleChoiceQuestionDisplay: passedAnswer', JSON.stringify(passedAnswer));
const [answer, setAnswer] = useState<AnswerType>(() => {
if (passedAnswer && passedAnswer.length > 0) {
return passedAnswer;
}
return [];
});
let disableButton = false;
2025-03-21 00:25:25 -04:00
if (handleOnSubmitAnswer === undefined) {
disableButton = true;
}
useEffect(() => {
2025-03-21 11:05:16 -04:00
console.log('MultipleChoiceQuestionDisplay: passedAnswer', JSON.stringify(passedAnswer));
2025-03-21 00:25:25 -04:00
if (passedAnswer !== undefined) {
setAnswer(passedAnswer);
2025-03-21 11:05:16 -04:00
} else {
setAnswer([]);
2025-03-21 00:25:25 -04:00
}
2025-03-21 11:05:16 -04:00
}, [passedAnswer, question.id]);
2024-03-29 20:08:34 -04:00
const handleOnClickAnswer = (choice: string) => {
2025-03-21 00:25:25 -04:00
setAnswer((prevAnswer) => {
2025-03-21 11:05:16 -04:00
console.log(`handleOnClickAnswer -- setAnswer(): prevAnswer: ${prevAnswer}, choice: ${choice}`);
const correctAnswersCount = question.choices.filter((c) => c.isCorrect).length;
if (correctAnswersCount === 1) {
// If only one correct answer, replace the current selection
return prevAnswer.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 (prevAnswer.includes(choice)) {
// Remove the choice if it's already selected
return prevAnswer.filter((selected) => selected !== choice);
} else {
// Add the choice if it's not already selected
return [...prevAnswer, choice];
}
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 (
2024-03-29 20:08:34 -04:00
<div className="question-container">
2024-04-08 21:37:11 -04:00
<div className="question content">
2025-01-26 09:33:42 -05:00
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(question.formattedStem) }} />
2024-03-29 20:08:34 -04:00
</div>
<div className="choices-wrapper mb-1">
{question.choices.map((choice, i) => {
2025-03-21 11:05:16 -04:00
console.log(`answer: ${answer}, choice: ${choice.formattedText.text}`);
2025-03-21 00:25:25 -04:00
const selected = answer.includes(choice.formattedText.text) ? 'selected' : '';
2024-03-29 20:08:34 -04:00
return (
<div key={choice.formattedText.text + i} className="choice-container">
<Button
2024-03-29 20:08:34 -04:00
variant="text"
className="button-wrapper"
disabled={disableButton}
2025-03-21 00:25:25 -04:00
onClick={() => !showAnswer && handleOnClickAnswer(choice.formattedText.text)}
>
{showAnswer ? (
<div>{choice.isCorrect ? '✅' : '❌'}</div>
) : (
''
)}
2024-03-29 20:08:34 -04:00
<div className={`circle ${selected}`}>{alphabet[i]}</div>
<div className={`answer-text ${selected}`}>
2025-03-21 00:25:25 -04:00
<div
dangerouslySetInnerHTML={{
__html: FormattedTextTemplate(choice.formattedText),
}}
/>
2024-03-29 20:08:34 -04:00
</div>
{choice.formattedFeedback && showAnswer && (
2025-03-21 00:25:25 -04:00
<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">
2025-03-21 00:25:25 -04:00
<div
dangerouslySetInnerHTML={{
__html: FormattedTextTemplate(question.formattedGlobalFeedback),
}}
/>
</div>
2024-03-29 20:08:34 -04:00
)}
{!showAnswer && handleOnSubmitAnswer && (
<Button
variant="contained"
onClick={() =>
2025-03-21 00:25:25 -04:00
answer.length > 0 && handleOnSubmitAnswer && handleOnSubmitAnswer(answer)
2024-03-29 20:08:34 -04:00
}
2025-03-21 00:25:25 -04:00
disabled={answer.length === 0}
2024-03-29 20:08:34 -04:00
>
Répondre
</Button>
)}
</div>
);
};
export default MultipleChoiceQuestionDisplay;