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

94 lines
3.8 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';
2024-03-29 20:08:34 -04:00
interface Props {
question: MultipleChoiceQuestion;
handleOnSubmitAnswer?: (answer: string | number | boolean) => void;
2024-03-29 20:08:34 -04:00
showAnswer?: boolean;
passedAnswer?: string | number | boolean;
2024-03-29 20:08:34 -04:00
}
const MultipleChoiceQuestionDisplay: React.FC<Props> = (props) => {
const { question, showAnswer, handleOnSubmitAnswer, passedAnswer } = props;
2025-03-07 12:20:39 -05:00
const [answer, setAnswer] = useState<string | number | boolean>(passedAnswer || '');
let disableButton = false;
if(handleOnSubmitAnswer === undefined){
disableButton = true;
}
useEffect(() => {
if (passedAnswer !== undefined) {
setAnswer(passedAnswer);
}
}, [passedAnswer]);
2024-03-29 20:08:34 -04:00
const handleOnClickAnswer = (choice: string) => {
setAnswer(choice);
};
const alpha = Array.from(Array(26)).map((_e, i) => i + 65);
const alphabet = alpha.map((x) => String.fromCharCode(x));
return (
2025-03-07 12:20:39 -05:00
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) => {
const selected = answer === 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}
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-01-26 09:33:42 -05:00
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(choice.formattedText) }} />
2024-03-29 20:08:34 -04:00
</div>
{choice.formattedFeedback && showAnswer && (
2024-03-29 20:08:34 -04:00
<div className="feedback-container mb-1 mt-1/2">
2025-01-26 09:33:42 -05:00
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(choice.formattedFeedback) }} />
2024-03-29 20:08:34 -04:00
</div>
)}
</Button>
2024-03-29 20:08:34 -04:00
</div>
);
})}
</div>
{question.formattedGlobalFeedback && showAnswer && (
<div className="global-feedback mb-2">
2025-01-26 09:33:42 -05:00
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(question.formattedGlobalFeedback) }} />
2025-01-25 02:02:18 -05:00
</div>
2024-03-29 20:08:34 -04:00
)}
2024-03-29 20:08:34 -04:00
{!showAnswer && handleOnSubmitAnswer && (
2024-03-29 20:08:34 -04:00
<Button
variant="contained"
onClick={() =>
answer !== "" && handleOnSubmitAnswer && handleOnSubmitAnswer(answer)
2024-03-29 20:08:34 -04:00
}
2025-03-07 12:20:39 -05:00
disabled={answer === '' || answer === null}
2024-03-29 20:08:34 -04:00
>
Répondre
2024-03-29 20:08:34 -04:00
</Button>
)}
</div>
);
};
export default MultipleChoiceQuestionDisplay;