2025-01-23 22:38:22 -05:00
|
|
|
// MultipleChoiceQuestionDisplay.tsx
|
2025-03-07 11:36:43 -05: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-26 09:33:42 -05:00
|
|
|
import { FormattedTextTemplate } from '../../GiftTemplate/templates/TextTypeTemplate';
|
2025-01-23 22:38:22 -05:00
|
|
|
import { MultipleChoiceQuestion } from 'gift-pegjs';
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
interface Props {
|
2025-01-23 22:38:22 -05:00
|
|
|
question: MultipleChoiceQuestion;
|
2025-03-04 16:49:12 -05:00
|
|
|
handleOnSubmitAnswer?: (answer: string | number | boolean) => void;
|
2024-03-29 20:08:34 -04:00
|
|
|
showAnswer?: boolean;
|
2025-03-04 16:49:12 -05:00
|
|
|
passedAnswer?: string | number | boolean;
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-23 22:38:22 -05:00
|
|
|
const MultipleChoiceQuestionDisplay: React.FC<Props> = (props) => {
|
2025-03-04 16:49:12 -05:00
|
|
|
const { question, showAnswer, handleOnSubmitAnswer, passedAnswer } = props;
|
2025-03-07 11:36:43 -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-04-16 12:14:20 -04:00
|
|
|
|
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));
|
2025-03-07 11:36:43 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return (
|
|
|
|
|
<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">
|
2025-03-07 11:36:43 -05:00
|
|
|
|
2025-01-23 22:38:22 -05:00
|
|
|
{question.choices.map((choice, i) => {
|
|
|
|
|
const selected = answer === choice.formattedText.text ? 'selected' : '';
|
2025-03-07 11:36:43 -05:00
|
|
|
console.log("dsa", 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">
|
2025-01-30 23:01:04 -05:00
|
|
|
<Button
|
2024-03-29 20:08:34 -04:00
|
|
|
variant="text"
|
|
|
|
|
className="button-wrapper"
|
2025-03-07 11:36:43 -05:00
|
|
|
disabled={disableButton}
|
2025-01-30 23:01:04 -05:00
|
|
|
onClick={() => !showAnswer && handleOnClickAnswer(choice.formattedText.text)}>
|
2025-01-31 15:55:37 -05:00
|
|
|
{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>
|
2025-01-30 23:01:04 -05:00
|
|
|
{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>
|
|
|
|
|
)}
|
2025-01-30 23:01:04 -05:00
|
|
|
</Button>
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
2025-01-23 22:38:22 -05:00
|
|
|
{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
|
|
|
)}
|
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={() =>
|
2025-03-04 16:49:12 -05:00
|
|
|
answer !== "" && handleOnSubmitAnswer && handleOnSubmitAnswer(answer)
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
2025-03-04 16:49:12 -05:00
|
|
|
disabled={answer === ''}
|
2024-03-29 20:08:34 -04:00
|
|
|
>
|
|
|
|
|
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;
|