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

90 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-03-29 20:08:34 -04:00
// NumericalQuestion.tsx
import React, { useState } from 'react';
import '../questionStyle.css';
import { Button, TextField } from '@mui/material';
import { FormatTextTemplate } from '../../GiftTemplate/templates/TextTypeTemplate';
2025-01-24 14:51:58 -05:00
import { NumericalQuestion, SimpleNumericalAnswer, RangeNumericalAnswer, HighLowNumericalAnswer } from 'gift-pegjs';
import { isSimpleNumericalAnswer, isRangeNumericalAnswer, isHighLowNumericalAnswer, isMultipleNumericalAnswer } from 'gift-pegjs/typeGuards';
2024-03-29 20:08:34 -04:00
interface Props {
2025-01-24 14:51:58 -05:00
question: NumericalQuestion;
2024-03-29 20:08:34 -04:00
handleOnSubmitAnswer?: (answer: number) => void;
showAnswer?: boolean;
}
2025-01-24 14:51:58 -05:00
const NumericalQuestionDisplay: React.FC<Props> = (props) => {
const { question, showAnswer, handleOnSubmitAnswer } =
2024-03-29 20:08:34 -04:00
props;
const [answer, setAnswer] = useState<number>();
2025-01-24 14:51:58 -05:00
const correctAnswers = question.choices;
let correctAnswer = '';
//const isSingleAnswer = correctAnswers.length === 1;
2024-03-29 20:08:34 -04:00
2025-01-24 14:51:58 -05:00
if (isSimpleNumericalAnswer(correctAnswers[0])) {
correctAnswer = `${(correctAnswers[0] as SimpleNumericalAnswer).number}`;
} else if (isRangeNumericalAnswer(correctAnswers[0])) {
const choice = correctAnswers[0] as RangeNumericalAnswer;
correctAnswer = `Entre ${choice.number - choice.range} et ${choice.number + choice.range}`;
2025-01-24 14:51:58 -05:00
} else if (isHighLowNumericalAnswer(correctAnswers[0])) {
const choice = correctAnswers[0] as HighLowNumericalAnswer;
correctAnswer = `Entre ${choice.numberLow} et ${choice.numberHigh}`;
2025-01-24 14:51:58 -05:00
} else if (isMultipleNumericalAnswer(correctAnswers[0])) {
correctAnswer = `MultipleNumericalAnswer is not supported yet`;
2025-01-24 14:51:58 -05:00
} else {
throw new Error('Unknown numerical answer type');
2025-01-24 14:51:58 -05:00
}
2024-03-29 20:08:34 -04:00
return (
<div className="question-wrapper">
2024-04-08 21:37:11 -04:00
<div>
<div dangerouslySetInnerHTML={{ __html: FormatTextTemplate(question.formattedStem) }} />
2024-03-29 20:08:34 -04:00
</div>
{showAnswer ? (
<>
<div className="correct-answer-text mb-2">{correctAnswer}</div>
2025-01-24 14:51:58 -05:00
{question.formattedGlobalFeedback && <div className="global-feedback mb-2">
<div dangerouslySetInnerHTML={{ __html: FormatTextTemplate(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="number"
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: React.ChangeEvent<HTMLInputElement>) => {
setAnswer(e.target.valueAsNumber);
}}
inputProps={{ 'data-testid': 'number-input' }}
/>
</div>
2025-01-24 14:51:58 -05:00
{question.formattedGlobalFeedback && showAnswer && (
<div className="global-feedback mb-2">
<div dangerouslySetInnerHTML={{ __html: FormatTextTemplate(question.formattedGlobalFeedback) }} />
2025-01-24 14:51:58 -05:00
</div>
2024-03-29 20:08:34 -04:00
)}
{handleOnSubmitAnswer && (
<Button
variant="contained"
onClick={() =>
answer !== undefined &&
handleOnSubmitAnswer &&
handleOnSubmitAnswer(answer)
}
disabled={answer === undefined || isNaN(answer)}
>
Répondre
</Button>
)}
</>
)}
</div>
);
};
2025-01-24 14:51:58 -05:00
export default NumericalQuestionDisplay;