feat(test): add custom back navigation handling

This commit is contained in:
shafin-r
2026-01-31 16:28:02 +06:00
parent b24cb27e93
commit 9d2ffb5183
2 changed files with 61 additions and 1 deletions

View File

@ -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;
};

View File

@ -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 = () => {
<div className="flex justify-end gap-3 mt-4">
<DialogClose asChild>
<button className="px-4 py-2 border rounded-lg cursor-pointer font-satoshi">
Cancel
Stay
</button>
</DialogClose>
<button
@ -515,6 +526,41 @@ export const Test = () => {
</DialogContent>
</Dialog>
<Dialog open={showExitDialog} onOpenChange={setShowExitDialog}>
<DialogContent showCloseButton={false}>
<DialogHeader>
<DialogTitle className="font-satoshi-medium text-start">
Want to quit the test?
</DialogTitle>
<DialogDescription className="font-satoshi tracking-wide">
Your progress will be saved and you can resume later.
</DialogDescription>
</DialogHeader>
<div className="flex justify-end gap-3 mt-4">
<button
onClick={() => {
setShowExitDialog(false);
blocker.reset(); // ⬅️ stay on page
}}
className="px-4 py-2 border rounded-lg cursor-pointer font-satoshi"
>
Stay
</button>
<button
onClick={() => {
finishExam();
blocker.proceed(); // ⬅️ allow navigation
}}
className="px-4 py-2 bg-red-600 text-white rounded-lg cursor-pointer font-satoshi"
>
Quit Test
</button>
</div>
</DialogContent>
</Dialog>
<button
disabled={isSubmitting}
onClick={handleNext}