EvalueTonSavoir/client/src/components/Questions/ShortAnswerQuestion/ShortAnswerQuestion.tsx

78 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-03-29 20:08:34 -04:00
// ShortAnswerQuestion.tsx
import React, { useState } from 'react';
import Latex from 'react-latex';
import '../questionStyle.css';
import { Button, TextField } from '@mui/material';
type Choices = {
feedback: { format: string; text: string } | null;
isCorrect: boolean;
text: { format: string; text: string };
weigth?: number;
};
interface Props {
2024-04-08 21:37:11 -04:00
questionTitle: string | null;
questionContent: string;
2024-03-29 20:08:34 -04:00
choices: Choices[];
globalFeedback?: string | undefined;
handleOnSubmitAnswer?: (answer: string) => void;
showAnswer?: boolean;
}
const ShortAnswerQuestion: React.FC<Props> = (props) => {
2024-04-08 21:37:11 -04:00
const { questionTitle, questionContent, choices, showAnswer, handleOnSubmitAnswer, globalFeedback } = props;
2024-03-29 20:08:34 -04:00
const [answer, setAnswer] = useState<string>();
return (
<div className="question-wrapper">
<div className="title mb-1 text-center center-h-align">
2024-04-08 21:37:11 -04:00
{questionTitle}
</div>
<div className="question content">
<Latex>{questionContent}</Latex>
2024-03-29 20:08:34 -04:00
</div>
{showAnswer ? (
<>
<div className="correct-answer-text mb-1">
{choices.map((choice) => (
<div className="mb-1">{choice.text.text}</div>
))}
</div>
{globalFeedback && <div className="global-feedback mb-2">{globalFeedback}</div>}
</>
) : (
<>
<div className="answer-wrapper mb-1">
<TextField
type="text"
2024-04-08 21:37:11 -04:00
id={questionContent}
name={questionContent}
2024-03-29 20:08:34 -04:00
onChange={(e) => {
setAnswer(e.target.value);
}}
disabled={showAnswer}
inputProps={{ 'data-testid': 'text-input' }}
/>
</div>
{handleOnSubmitAnswer && (
<Button
variant="contained"
onClick={() =>
answer !== undefined &&
handleOnSubmitAnswer &&
handleOnSubmitAnswer(answer)
}
disabled={answer === undefined || answer === ''}
>
Répondre
</Button>
)}
</>
)}
</div>
);
};
export default ShortAnswerQuestion;