Réponse à une question non enregistrée lorsque Étudiant reviens en arrière dans le quiz

Fixes #200
This commit is contained in:
JubaAzul 2025-03-04 16:49:12 -05:00
parent fe44409e16
commit 60ad2df67d
5 changed files with 40 additions and 23 deletions

View file

@ -1,5 +1,5 @@
// MultipleChoiceQuestionDisplay.tsx // MultipleChoiceQuestionDisplay.tsx
import React, { useEffect, useState } from 'react'; import React, { useState } from 'react';
import '../questionStyle.css'; import '../questionStyle.css';
import { Button } from '@mui/material'; import { Button } from '@mui/material';
import { FormattedTextTemplate } from '../../GiftTemplate/templates/TextTypeTemplate'; import { FormattedTextTemplate } from '../../GiftTemplate/templates/TextTypeTemplate';
@ -7,22 +7,21 @@ import { MultipleChoiceQuestion } from 'gift-pegjs';
interface Props { interface Props {
question: MultipleChoiceQuestion; question: MultipleChoiceQuestion;
handleOnSubmitAnswer?: (answer: string) => void; handleOnSubmitAnswer?: (answer: string | number | boolean) => void;
showAnswer?: boolean; showAnswer?: boolean;
passedAnswer?: string | number | boolean;
} }
const MultipleChoiceQuestionDisplay: React.FC<Props> = (props) => { const MultipleChoiceQuestionDisplay: React.FC<Props> = (props) => {
const { question, showAnswer, handleOnSubmitAnswer } = props; const { question, showAnswer, handleOnSubmitAnswer, passedAnswer } = props;
const [answer, setAnswer] = useState<string>(); const [answer, setAnswer] = useState<string | number | boolean>(passedAnswer || '');
useEffect(() => {
setAnswer(undefined);
}, [question]);
const handleOnClickAnswer = (choice: string) => { const handleOnClickAnswer = (choice: string) => {
setAnswer(choice); setAnswer(choice);
}; };
const alpha = Array.from(Array(26)).map((_e, i) => i + 65); const alpha = Array.from(Array(26)).map((_e, i) => i + 65);
const alphabet = alpha.map((x) => String.fromCharCode(x)); const alphabet = alpha.map((x) => String.fromCharCode(x));
return ( return (
@ -67,9 +66,9 @@ const MultipleChoiceQuestionDisplay: React.FC<Props> = (props) => {
<Button <Button
variant="contained" variant="contained"
onClick={() => onClick={() =>
answer !== undefined && handleOnSubmitAnswer && handleOnSubmitAnswer(answer) answer !== "" && handleOnSubmitAnswer && handleOnSubmitAnswer(answer)
} }
disabled={answer === undefined} disabled={answer === ''}
> >
Répondre Répondre

View file

@ -11,11 +11,13 @@ interface QuestionProps {
question: Question; question: Question;
handleOnSubmitAnswer?: (answer: string | number | boolean) => void; handleOnSubmitAnswer?: (answer: string | number | boolean) => void;
showAnswer?: boolean; showAnswer?: boolean;
answer?: string | number | boolean;
} }
const QuestionDisplay: React.FC<QuestionProps> = ({ const QuestionDisplay: React.FC<QuestionProps> = ({
question, question,
handleOnSubmitAnswer, handleOnSubmitAnswer,
showAnswer, showAnswer,
answer,
}) => { }) => {
// const isMobile = useCheckMobileScreen(); // const isMobile = useCheckMobileScreen();
// const imgWidth = useMemo(() => { // const imgWidth = useMemo(() => {
@ -30,6 +32,7 @@ const QuestionDisplay: React.FC<QuestionProps> = ({
question={question} question={question}
handleOnSubmitAnswer={handleOnSubmitAnswer} handleOnSubmitAnswer={handleOnSubmitAnswer}
showAnswer={showAnswer} showAnswer={showAnswer}
passedAnswer={answer}
/> />
); );
break; break;
@ -39,6 +42,7 @@ const QuestionDisplay: React.FC<QuestionProps> = ({
question={question} question={question}
handleOnSubmitAnswer={handleOnSubmitAnswer} handleOnSubmitAnswer={handleOnSubmitAnswer}
showAnswer={showAnswer} showAnswer={showAnswer}
passedAnswer={answer}
/> />
); );
break; break;

View file

@ -1,25 +1,35 @@
// TrueFalseQuestion.tsx // TrueFalseQuestion.tsx
import React, { useState, useEffect } from 'react'; import React, { useState } from 'react';
import '../questionStyle.css'; import '../questionStyle.css';
import { Button } from '@mui/material'; import { Button } from '@mui/material';
import { TrueFalseQuestion } from 'gift-pegjs'; import { TrueFalseQuestion } from 'gift-pegjs';
import { FormattedTextTemplate } from 'src/components/GiftTemplate/templates/TextTypeTemplate'; import { FormattedTextTemplate } from 'src/components/GiftTemplate/templates/TextTypeTemplate';
import { Answer } from 'src/Types/StudentType';
interface Props { interface Props {
question: TrueFalseQuestion; question: TrueFalseQuestion;
handleOnSubmitAnswer?: (answer: boolean) => void; handleOnSubmitAnswer?: (answer: Answer) => void;
showAnswer?: boolean; showAnswer?: boolean;
passedAnswer?: string | number | boolean;
} }
const TrueFalseQuestionDisplay: React.FC<Props> = (props) => { const TrueFalseQuestionDisplay: React.FC<Props> = (props) => {
const { question, showAnswer, handleOnSubmitAnswer } = const { question, showAnswer, handleOnSubmitAnswer, passedAnswer} =
props; props;
const [answer, setAnswer] = useState<boolean | undefined>(undefined); console.log("Passedanswer", passedAnswer);
useEffect(() => { const [answer, setAnswer] = useState<boolean | undefined>(() => {
setAnswer(undefined); if (typeof passedAnswer === 'boolean') {
}, [question]); return passedAnswer;
}
return undefined;
});
const handleOnClickAnswer = (choice: boolean) => {
setAnswer(choice);
};
console.log("Answer", answer);
const selectedTrue = answer ? 'selected' : ''; const selectedTrue = answer ? 'selected' : '';
const selectedFalse = answer !== undefined && !answer ? 'selected' : ''; const selectedFalse = answer !== undefined && !answer ? 'selected' : '';
return ( return (
@ -30,7 +40,7 @@ const TrueFalseQuestionDisplay: React.FC<Props> = (props) => {
<div className="choices-wrapper mb-1"> <div className="choices-wrapper mb-1">
<Button <Button
className="button-wrapper" className="button-wrapper"
onClick={() => !showAnswer && setAnswer(true)} onClick={() => !showAnswer && handleOnClickAnswer(true)}
fullWidth fullWidth
> >
{showAnswer? (<div> {(question.isTrue ? '✅' : '❌')}</div>):``} {showAnswer? (<div> {(question.isTrue ? '✅' : '❌')}</div>):``}
@ -39,7 +49,7 @@ const TrueFalseQuestionDisplay: React.FC<Props> = (props) => {
</Button> </Button>
<Button <Button
className="button-wrapper" className="button-wrapper"
onClick={() => !showAnswer && setAnswer(false)} onClick={() => !showAnswer && handleOnClickAnswer(false)}
fullWidth fullWidth
> >
{showAnswer? (<div> {(!question.isTrue ? '✅' : '❌')}</div>):``} {showAnswer? (<div> {(!question.isTrue ? '✅' : '❌')}</div>):``}

View file

@ -32,8 +32,6 @@ const StudentModeQuiz: React.FC<StudentModeQuizProps> = ({
}; };
useEffect(() => { useEffect(() => {
const answer = localStorage.getItem(`Answer${questionInfos.question.id}`); const answer = localStorage.getItem(`Answer${questionInfos.question.id}`);
if (answer !== null) { if (answer !== null) {
@ -81,6 +79,7 @@ const StudentModeQuiz: React.FC<StudentModeQuizProps> = ({
handleOnSubmitAnswer={handleOnSubmitAnswer} handleOnSubmitAnswer={handleOnSubmitAnswer}
question={questionInfos.question as Question} question={questionInfos.question as Question}
showAnswer={isAnswerSubmitted} showAnswer={isAnswerSubmitted}
answer={localStorage.getItem(`Answer${questionInfos.question.id}`) || undefined}
/> />
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', marginTop: '1rem' }}> <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', marginTop: '1rem' }}>
<div> <div>

View file

@ -21,6 +21,7 @@ const TeacherModeQuiz: React.FC<TeacherModeQuizProps> = ({
const [isAnswerSubmitted, setIsAnswerSubmitted] = useState(false); const [isAnswerSubmitted, setIsAnswerSubmitted] = useState(false);
const [isFeedbackDialogOpen, setIsFeedbackDialogOpen] = useState(false); const [isFeedbackDialogOpen, setIsFeedbackDialogOpen] = useState(false);
const [feedbackMessage, setFeedbackMessage] = useState<React.ReactNode>(''); const [feedbackMessage, setFeedbackMessage] = useState<React.ReactNode>('');
const [answer, setAnswer] = useState<string | number | boolean>('');
const renderFeedbackMessage = (answer: string) => { const renderFeedbackMessage = (answer: string) => {
@ -36,17 +37,18 @@ const TeacherModeQuiz: React.FC<TeacherModeQuizProps> = ({
</span> </span>
);} );}
}; };
useEffect(() => { useEffect(() => {
// Close the feedback dialog when the question changes // Close the feedback dialog when the question changes
handleFeedbackDialogClose(); handleFeedbackDialogClose();
setIsAnswerSubmitted(false); setIsAnswerSubmitted(false);
const answer = localStorage.getItem(`Answer${questionInfos.question.id}`); setAnswer(JSON.parse(localStorage.getItem(`Answer${questionInfos.question.id}`)||'null'));
if (answer !== null) { if (answer) {
setIsAnswerSubmitted(true); setIsAnswerSubmitted(true);
setIsFeedbackDialogOpen(true); setIsFeedbackDialogOpen(true);
} }
}, [questionInfos.question]); }, [questionInfos.question , answer]);
const handleOnSubmitAnswer = (answer: string | number | boolean) => { const handleOnSubmitAnswer = (answer: string | number | boolean) => {
const idQuestion = Number(questionInfos.question.id) || -1; const idQuestion = Number(questionInfos.question.id) || -1;
@ -85,6 +87,7 @@ const TeacherModeQuiz: React.FC<TeacherModeQuizProps> = ({
<QuestionComponent <QuestionComponent
handleOnSubmitAnswer={handleOnSubmitAnswer} handleOnSubmitAnswer={handleOnSubmitAnswer}
question={questionInfos.question as Question} question={questionInfos.question as Question}
answer={answer}
/> />
)} )}
@ -109,6 +112,8 @@ const TeacherModeQuiz: React.FC<TeacherModeQuizProps> = ({
handleOnSubmitAnswer={handleOnSubmitAnswer} handleOnSubmitAnswer={handleOnSubmitAnswer}
question={questionInfos.question as Question} question={questionInfos.question as Question}
showAnswer={true} showAnswer={true}
answer={answer}
/> />
</DialogContent> </DialogContent>
<DialogActions> <DialogActions>