diff --git a/app/src/pages/plans/PlanEditor.tsx b/app/src/pages/plans/PlanEditor.tsx index 8de92bb..404a955 100644 --- a/app/src/pages/plans/PlanEditor.tsx +++ b/app/src/pages/plans/PlanEditor.tsx @@ -16,6 +16,7 @@ import type { Exercise, LatestExerciseResult } from '../../api/types'; import { BackButton, Button, + ConfirmDialog, ErrorText, isPastWeek, Skeleton, @@ -62,6 +63,11 @@ export default function PlanEditor() { ? `/plans/${planIdParam}` : '/plans'; + const requestExit = () => { + if (editor.dirty) setConfirmExit(true); + else navigate(backPath); + }; + useTopbar({ title: isTraineeMode ? isEdit @@ -75,6 +81,8 @@ export default function PlanEditor() { : isEdit ? `/plans/${planIdParam}` : '/plans', + // mobile „<” w dolnym pasku topmenu: potwierdzenie przy niezapisanych zmianach + onBack: editor.dirty ? requestExit : undefined, }); // Inicjalizacja trybu edycji po załadowaniu danych (useEffect, nie setState during render) @@ -88,10 +96,23 @@ export default function PlanEditor() { const [saving, setSaving] = useState(false); const [expandedWeek, setExpandedWeek] = useState(0); const [resultsOpenKey, setResultsOpenKey] = useState(null); + const [confirmExit, setConfirmExit] = useState(false); + const [clearWeekIdx, setClearWeekIdx] = useState(null); const exercises = exData?.exercises ?? []; const loading = isEdit && !editor.initialized; + // Guard niezapisanych zmian: F5/odświeżenie + wyjście (Anuluj/Wstecz/mobile „<”) + useEffect(() => { + if (!editor.dirty) return; + const handler = (e: BeforeUnloadEvent) => { + e.preventDefault(); + e.returnValue = ''; + }; + window.addEventListener('beforeunload', handler); + return () => window.removeEventListener('beforeunload', handler); + }, [editor.dirty]); + const grouped = useMemo(() => { const g = new Map(); for (const p of exercises) { @@ -201,7 +222,7 @@ export default function PlanEditor() { return ( <>
- navigate(backPath)} /> +
0} onCopyPrev={() => editor.copyPrevWeek(wi)} canClear={days.length > 0 || !w.detached} - onClear={() => editor.clearWeek(wi)} + onClear={() => setClearWeekIdx(wi)} > {saving ? 'Zapisywanie…' : isEdit ? 'Zapisz zmiany' : 'Utwórz plan'} - + + setConfirmExit(false)} + onConfirm={() => { + setConfirmExit(false); + navigate(backPath); + }} + title="Niezapisane zmiany" + description="Masz niezapisane zmiany. Opuścić stronę i utracić je?" + confirmLabel="Opuść" + danger + /> + setClearWeekIdx(null)} + onConfirm={() => { + if (clearWeekIdx !== null) editor.clearWeek(clearWeekIdx); + setClearWeekIdx(null); + }} + title={`Wyczyść tydzień ${(clearWeekIdx ?? 0) + 1}`} + description="Usunąć wszystkie dni i ćwiczenia tego tygodnia?" + confirmLabel="Wyczyść" + danger + /> ); } diff --git a/app/src/topbar/Topbar.tsx b/app/src/topbar/Topbar.tsx index f29e40a..90bbc1e 100644 --- a/app/src/topbar/Topbar.tsx +++ b/app/src/topbar/Topbar.tsx @@ -30,7 +30,13 @@ function SectionSearchInput({ section }: { section: SearchableSection }) { ); } -function ActionButton({ action, onNavigate }: { action: TopbarAction; onNavigate: (to: string) => void }) { +function ActionButton({ + action, + onNavigate, +}: { + action: TopbarAction; + onNavigate: (to: string) => void; +}) { const Icon = action.icon; return (