diff --git a/src/hooks/useExamNavGuard.ts b/src/hooks/useExamNavGuard.ts new file mode 100644 index 0000000..383e239 --- /dev/null +++ b/src/hooks/useExamNavGuard.ts @@ -0,0 +1,14 @@ +import { useBlocker } from "react-router-dom"; +import { useSatExam } from "../stores/useSatExam"; + +export const useExamNavigationGuard = () => { + const phase = useSatExam((s) => s.phase); + + const blocker = useBlocker( + ({ currentLocation, nextLocation }) => + (phase === "MODULE" || phase === "BREAK") && + currentLocation.pathname !== nextLocation.pathname, + ); + + return blocker; +}; diff --git a/src/pages/student/practice/Test.tsx b/src/pages/student/practice/Test.tsx index 4edf354..c0924f3 100644 --- a/src/pages/student/practice/Test.tsx +++ b/src/pages/student/practice/Test.tsx @@ -37,8 +37,19 @@ import { DialogTitle, DialogTrigger, } from "../../../components/ui/dialog"; +import { useExamNavigationGuard } from "../../../hooks/useExamNavGuard"; export const Test = () => { + const blocker = useExamNavigationGuard(); + + const [showExitDialog, setShowExitDialog] = useState(false); + + useEffect(() => { + if (blocker.state === "blocked") { + setShowExitDialog(true); + } + }, [blocker.state]); + const navigate = useNavigate(); const { user } = useAuthStore(); const token = useAuthToken(); @@ -445,7 +456,7 @@ export const Test = () => {