EvalueTonSavoir/client/src/components/Questions/MultipleChoiceQuestion/MultipleChoiceQuestion.tsx

91 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-03-29 20:08:34 -04:00
// MultipleChoiceQuestion.tsx
import React, { useEffect, useState } from 'react';
2024-03-29 20:08:34 -04:00
import '../questionStyle.css';
import { Button } from '@mui/material';
import textType, { formatLatex } from '../../GiftTemplate/templates/TextType';
import { TextFormat } from '../../GiftTemplate/templates/types';
// import Latex from 'react-latex';
2024-03-29 20:08:34 -04:00
type Choices = {
feedback: { format: string; text: string } | null;
isCorrect: boolean;
text: { format: string; text: string };
weigth?: number;
};
interface Props {
questionStem: TextFormat;
2024-03-29 20:08:34 -04:00
choices: Choices[];
globalFeedback?: string | undefined;
handleOnSubmitAnswer?: (answer: string) => void;
showAnswer?: boolean;
}
const MultipleChoiceQuestion: React.FC<Props> = (props) => {
const { questionStem: questionContent, choices, showAnswer, handleOnSubmitAnswer, globalFeedback } = props;
2024-03-29 20:08:34 -04:00
const [answer, setAnswer] = useState<string>();
useEffect(() => {
setAnswer(undefined);
}, [questionContent]);
2024-03-29 20:08:34 -04:00
const handleOnClickAnswer = (choice: string) => {
setAnswer(choice);
};
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">
<div dangerouslySetInnerHTML={{ __html: textType({text: questionContent}) }} />
2024-03-29 20:08:34 -04:00
</div>
<div className="choices-wrapper mb-1">
{choices.map((choice, i) => {
const selected = answer === choice.text.text ? 'selected' : '';
return (
2024-09-15 01:26:06 -04:00
<div key={choice.text.text + i} className="choice-container">
<Button
2024-03-29 20:08:34 -04:00
variant="text"
className="button-wrapper"
onClick={() => !showAnswer && handleOnClickAnswer(choice.text.text)}
>
{choice.feedback === null &&
showAnswer &&
(choice.isCorrect ? '✅' : '❌')}
<div className={`circle ${selected}`}>{alphabet[i]}</div>
<div className={`answer-text ${selected}`}>
<div dangerouslySetInnerHTML={{ __html: formatLatex(choice.text.text) }} />
2024-03-29 20:08:34 -04:00
</div>
</Button>
{choice.feedback && showAnswer && (
<div className="feedback-container mb-1 mt-1/2">
{choice.isCorrect ? '✅' : '❌'}
{choice.feedback?.text}
</div>
)}
</div>
);
})}
</div>
{globalFeedback && showAnswer && (
<div className="global-feedback mb-2">{globalFeedback}</div>
)}
{!showAnswer && handleOnSubmitAnswer && (
<Button
variant="contained"
onClick={() =>
answer !== undefined && handleOnSubmitAnswer && handleOnSubmitAnswer(answer)
}
disabled={answer === undefined}
>
Répondre
</Button>
)}
</div>
);
};
export default MultipleChoiceQuestion;