// MultipleChoiceQuestionDisplay.tsx import React, { useEffect, useState } from 'react'; import '../questionStyle.css'; import { Button } from '@mui/material'; import { FormattedTextTemplate } from '../../GiftTemplate/templates/TextTypeTemplate'; import { MultipleChoiceQuestion } from 'gift-pegjs'; import { AnswerType } from 'src/pages/Student/JoinRoom/JoinRoom'; interface Props { question: MultipleChoiceQuestion; handleOnSubmitAnswer?: (answer: AnswerType) => void; showAnswer?: boolean; passedAnswer?: AnswerType; } const MultipleChoiceQuestionDisplay: React.FC = (props) => { const { question, showAnswer, handleOnSubmitAnswer, passedAnswer } = props; const [answer, setAnswer] = useState(passedAnswer || []); let disableButton = false; if (handleOnSubmitAnswer === undefined) { disableButton = true; } useEffect(() => { if (passedAnswer !== undefined) { setAnswer(passedAnswer); } }, [passedAnswer]); const handleOnClickAnswer = (choice: string) => { setAnswer((prevAnswer) => { 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]; } }); }; const alpha = Array.from(Array(26)).map((_e, i) => i + 65); const alphabet = alpha.map((x) => String.fromCharCode(x)); return (
{question.choices.map((choice, i) => { const selected = answer.includes(choice.formattedText.text) ? 'selected' : ''; return (
); })}
{question.formattedGlobalFeedback && showAnswer && (
)} {!showAnswer && handleOnSubmitAnswer && ( )}
); }; export default MultipleChoiceQuestionDisplay;