diff --git a/client/src/components/ReturnButton/ReturnButton.tsx b/client/src/components/ReturnButton/ReturnButton.tsx index a184356..8ec0bb1 100644 --- a/client/src/components/ReturnButton/ReturnButton.tsx +++ b/client/src/components/ReturnButton/ReturnButton.tsx @@ -1,65 +1,69 @@ -// GoBackButton.tsx -import React from 'react'; -import { useState } from 'react'; +import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; -import ConfirmDialog from '../ConfirmDialog/ConfirmDialog'; import { Button } from '@mui/material'; import { ChevronLeft } from '@mui/icons-material'; +import ApiService from '../../services/ApiService'; // Assuming this is where save logic lives interface Props { onReturn?: () => void; - askConfirm?: boolean; - message?: string; + quizTitle?: string; // Quiz title to save + quizContent?: string[]; // Quiz content to save + quizFolder?: string; // Folder ID to save + quizId?: string; // Quiz ID for updates (optional) + isNewQuiz?: boolean; // Flag to determine create or update } const ReturnButton: React.FC = ({ - askConfirm = false, - message = 'Êtes-vous sûr de vouloir quitter la page ?', - onReturn + onReturn, + quizTitle = '', + quizContent = [], + quizFolder = '', + quizId, + isNewQuiz = false, }) => { const navigate = useNavigate(); - const [showDialog, setShowDialog] = useState(false); + const [isSaving, setIsSaving] = useState(false); // Optional: to show saving state - const handleOnReturnButtonClick = () => { - if (askConfirm) { - setShowDialog(true); - } else { + const handleOnReturnButtonClick = async () => { + setIsSaving(true); + try { + // Automatically save the quiz + if (isNewQuiz && quizTitle && quizFolder) { + await ApiService.createQuiz(quizTitle, quizContent, quizFolder); + } else if (quizId && quizTitle) { + await ApiService.updateQuiz(quizId, quizTitle, quizContent); + } + // If no title/folder, proceed without saving (optional behavior) handleOnReturn(); + } catch (error) { + console.error('Error saving quiz on return:', error); + // Still navigate even if save fails, to avoid trapping the user + handleOnReturn(); + } finally { + setIsSaving(false); } }; - const handleConfirm = () => { - setShowDialog(false); - handleOnReturn(); - }; - const handleOnReturn = () => { if (onReturn) { onReturn(); } else { - navigate(-1); + navigate('/teacher/dashboard'); // Navigate to dashboard instead of -1 } }; return ( -
+
- setShowDialog(false)} - buttonOrderType="warning" - />
); }; diff --git a/client/src/pages/Teacher/EditorQuiz/EditorQuiz.tsx b/client/src/pages/Teacher/EditorQuiz/EditorQuiz.tsx index f92a8a0..76de8a3 100644 --- a/client/src/pages/Teacher/EditorQuiz/EditorQuiz.tsx +++ b/client/src/pages/Teacher/EditorQuiz/EditorQuiz.tsx @@ -225,8 +225,11 @@ const QuizForm: React.FC = () => {
Éditeur de Quiz