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

112 lines
4.1 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
2024-03-29 20:08:34 -04:00
import '../questionStyle.css';
import { Button, TextField } from '@mui/material';
2025-01-26 09:33:42 -05:00
import { FormattedTextTemplate } from '../../GiftTemplate/templates/TextTypeTemplate';
2025-01-24 14:51:58 -05:00
import { ShortAnswerQuestion } from 'gift-pegjs';
2025-03-08 11:05:25 -05:00
import { AnswerType } from 'src/pages/Student/JoinRoom/JoinRoom';
2024-03-29 20:08:34 -04:00
interface Props {
2025-01-24 14:51:58 -05:00
question: ShortAnswerQuestion;
2025-03-08 11:05:25 -05:00
handleOnSubmitAnswer?: (answer: AnswerType) => void;
2024-03-29 20:08:34 -04:00
showAnswer?: boolean;
2025-03-08 11:05:25 -05:00
passedAnswer?: AnswerType;
2024-03-29 20:08:34 -04:00
}
2025-01-24 14:51:58 -05:00
const ShortAnswerQuestionDisplay: React.FC<Props> = (props) => {
const { question, showAnswer, handleOnSubmitAnswer, passedAnswer } = props;
2025-03-08 11:05:25 -05:00
const [answer, setAnswer] = useState<AnswerType>(passedAnswer || '');
const [isGoodAnswer, setisGoodAnswer] = useState<boolean>(false);
useEffect(() => {
if (passedAnswer !== undefined) {
setAnswer(passedAnswer);
}
}, [passedAnswer]);
useEffect(() => {
checkAnswer();
}, [answer]);
const checkAnswer = () => {
const isCorrect = question.choices.some((choice) => choice.text.toLowerCase() === (answer as String).toLowerCase());
setisGoodAnswer(isCorrect);
};
2024-03-29 20:08:34 -04:00
return (
2024-03-29 20:08:34 -04:00
<div className="question-wrapper">
{showAnswer && (
<div>
<div className='question-feedback-validation'>
{isGoodAnswer ? '✅ Correct! ' : '❌ Incorrect!'}
</div>
<div className="question-title">
Question :
</div>
</div>
)}
<div>
2025-01-26 09:33:42 -05:00
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(question.formattedStem) }} />
2024-03-29 20:08:34 -04:00
</div>
{showAnswer ? (
<>
<div className="correct-answer-text mb-1">
<div>
<div className="question-title">
Réponse(s) accepté(es):
</div>
{question.choices.map((choice) => (
<div key={choice.text} className="accepted-answers">
{choice.text}
</div>
))}
</div>
<div>
<div className="question-title">
Votre réponse est: </div>
<div className="accepted-answers">{answer}</div>
</div>
2024-03-29 20:08:34 -04:00
</div>
2025-01-24 14:51:58 -05:00
{question.formattedGlobalFeedback && <div className="global-feedback mb-2">
2025-01-26 09:33:42 -05:00
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(question.formattedGlobalFeedback) }} />
2025-01-24 14:51:58 -05:00
</div>}
2024-03-29 20:08:34 -04:00
</>
) : (
<>
<div className="answer-wrapper mb-1">
<TextField
type="text"
2025-01-24 14:51:58 -05:00
id={question.formattedStem.text}
name={question.formattedStem.text}
2024-03-29 20:08:34 -04:00
onChange={(e) => {
setAnswer(e.target.value);
}}
disabled={showAnswer}
2025-01-24 14:51:58 -05:00
aria-label="short-answer-input"
2024-03-29 20:08:34 -04:00
/>
</div>
{handleOnSubmitAnswer && (
<Button
variant="contained"
onClick={() =>
answer !== undefined &&
handleOnSubmitAnswer &&
handleOnSubmitAnswer(answer)
}
disabled={answer === null || answer === ''}
2024-03-29 20:08:34 -04:00
>
Répondre
</Button>
)}
</>
)}
</div>
);
};
2025-01-24 14:51:58 -05:00
export default ShortAnswerQuestionDisplay;