feat(nav): add flow-guarding for exam result screens

This commit is contained in:
shafin-r
2025-09-01 17:53:44 +06:00
parent 5fd76bc0ec
commit 3b2488054c
5 changed files with 123 additions and 20 deletions

View File

@ -1,7 +1,7 @@
"use client";
import { useRouter } from "next/navigation";
import React from "react";
import React, { useEffect } from "react";
import { ArrowLeft } from "lucide-react";
import { useExamStore } from "@/stores/examStore";
import QuestionItem from "@/components/QuestionItem";
@ -10,7 +10,20 @@ import { getResultViews } from "@/lib/gallery-views";
export default function ResultsPage() {
const router = useRouter();
const { result, clearResult } = useExamStore();
const { result, clearResult, setStatus, status } = useExamStore();
useEffect(() => {
const handlePopState = () => {
if (status !== "finished") {
router.replace(`/categories`);
}
};
window.addEventListener("popstate", handlePopState);
return () => {
window.removeEventListener("popstate", handlePopState);
};
}, [status, router, setStatus]);
if (!result) {
return (
@ -21,8 +34,9 @@ export default function ResultsPage() {
}
const handleBackToHome = () => {
clearResult();
router.push("/categories");
setStatus("not-started"); // ✅ reset exam flow
clearResult(); // ✅ clear stored results
router.replace("/categories"); // ✅ prevent re-entry
};
const views = getResultViews(result);