2025-01-23 22:38:22 -05:00
|
|
|
// MultipleChoiceQuestionDisplay.tsx
|
2024-04-16 12:14:20 -04:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2024-03-29 20:08:34 -04:00
|
|
|
import '../questionStyle.css';
|
|
|
|
|
import { Button } from '@mui/material';
|
2025-01-23 22:38:22 -05:00
|
|
|
import { textType } from '../../GiftTemplate/templates/TextType';
|
|
|
|
|
import { MultipleChoiceQuestion } from 'gift-pegjs';
|
2025-01-15 09:07:56 -05:00
|
|
|
import DOMPurify from 'dompurify';
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
interface Props {
|
2025-01-23 22:38:22 -05:00
|
|
|
question: MultipleChoiceQuestion;
|
2024-03-29 20:08:34 -04:00
|
|
|
handleOnSubmitAnswer?: (answer: string) => void;
|
|
|
|
|
showAnswer?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 22:38:22 -05:00
|
|
|
const MultipleChoiceQuestionDisplay: React.FC<Props> = (props) => {
|
2025-01-22 15:28:45 -05:00
|
|
|
|
2025-01-23 22:38:22 -05:00
|
|
|
const { question, showAnswer, handleOnSubmitAnswer } = props;
|
2024-03-29 20:08:34 -04:00
|
|
|
const [answer, setAnswer] = useState<string>();
|
|
|
|
|
|
2024-04-16 12:14:20 -04:00
|
|
|
useEffect(() => {
|
|
|
|
|
setAnswer(undefined);
|
2025-01-23 22:38:22 -05:00
|
|
|
}, [question]);
|
2024-04-16 12:14:20 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
const handleOnClickAnswer = (choice: string) => {
|
|
|
|
|
setAnswer(choice);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-16 12:14:20 -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));
|
|
|
|
|
return (
|
|
|
|
|
<div className="question-container">
|
2024-04-08 21:37:11 -04:00
|
|
|
<div className="question content">
|
2025-01-23 22:38:22 -05:00
|
|
|
<div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(textType({text: question.formattedStem})) }} />
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
|
|
|
|
<div className="choices-wrapper mb-1">
|
2025-01-23 22:38:22 -05:00
|
|
|
{question.choices.map((choice, i) => {
|
|
|
|
|
const selected = answer === choice.formattedText.text ? 'selected' : '';
|
2024-03-29 20:08:34 -04:00
|
|
|
return (
|
2025-01-23 22:38:22 -05:00
|
|
|
<div key={choice.formattedText.text + i} className="choice-container">
|
2024-09-15 01:26:06 -04:00
|
|
|
<Button
|
2024-03-29 20:08:34 -04:00
|
|
|
variant="text"
|
|
|
|
|
className="button-wrapper"
|
2025-01-23 22:38:22 -05:00
|
|
|
onClick={() => !showAnswer && handleOnClickAnswer(choice.formattedText.text)}
|
2024-03-29 20:08:34 -04:00
|
|
|
>
|
2025-01-23 22:38:22 -05:00
|
|
|
{choice.formattedFeedback === null &&
|
2024-03-29 20:08:34 -04:00
|
|
|
showAnswer &&
|
|
|
|
|
(choice.isCorrect ? '✅' : '❌')}
|
|
|
|
|
<div className={`circle ${selected}`}>{alphabet[i]}</div>
|
|
|
|
|
<div className={`answer-text ${selected}`}>
|
2025-01-23 22:38:22 -05:00
|
|
|
<div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(textType({ text: choice.formattedText })) }} />
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
|
|
|
|
</Button>
|
2025-01-23 22:38:22 -05:00
|
|
|
{choice.formattedFeedback && showAnswer && (
|
2024-03-29 20:08:34 -04:00
|
|
|
<div className="feedback-container mb-1 mt-1/2">
|
|
|
|
|
{choice.isCorrect ? '✅' : '❌'}
|
2025-01-23 22:38:22 -05:00
|
|
|
<div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(textType({ text: choice.formattedFeedback })) }} />
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
2025-01-23 22:38:22 -05:00
|
|
|
{question.formattedGlobalFeedback && showAnswer && (
|
|
|
|
|
<div className="global-feedback mb-2">
|
|
|
|
|
<p>${textType({ text: question.formattedGlobalFeedback })}</p>
|
|
|
|
|
</div>
|
2024-03-29 20:08:34 -04:00
|
|
|
)}
|
2025-01-21 15:35:07 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
{!showAnswer && handleOnSubmitAnswer && (
|
2025-01-21 15:35:07 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
<Button
|
|
|
|
|
variant="contained"
|
|
|
|
|
onClick={() =>
|
|
|
|
|
answer !== undefined && handleOnSubmitAnswer && handleOnSubmitAnswer(answer)
|
|
|
|
|
}
|
|
|
|
|
disabled={answer === undefined}
|
|
|
|
|
>
|
|
|
|
|
Répondre
|
2025-01-21 15:35:07 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-23 22:38:22 -05:00
|
|
|
export default MultipleChoiceQuestionDisplay;
|