fix(api): exam logic yet to be fixed

This commit is contained in:
shafin-r
2025-08-31 17:45:08 +06:00
parent b112a8fdac
commit 65e3338859
3 changed files with 16 additions and 17 deletions

View File

@ -16,7 +16,7 @@ export default function ExamPage() {
const test_id = searchParams.get("test_id") || ""; const test_id = searchParams.get("test_id") || "";
const type = searchParams.get("type") || ""; const type = searchParams.get("type") || "";
const { isOpen, close, open } = useModal(); // const { isOpen, close, open } = useModal();
const { timeRemaining, setInitialTime, stopTimer } = useTimer(); const { timeRemaining, setInitialTime, stopTimer } = useTimer();
const { test, answers, startExam, setAnswer, submitExam, cancelExam } = const { test, answers, startExam, setAnswer, submitExam, cancelExam } =
useExam(); useExam();
@ -36,7 +36,7 @@ export default function ExamPage() {
if (window.confirm("Are you sure you want to quit the exam?")) { if (window.confirm("Are you sure you want to quit the exam?")) {
stopTimer(); stopTimer();
cancelExam(); cancelExam();
router.push("/unit"); router.push(`/categories/${type}s`);
} }
}, [stopTimer, cancelExam, router]); }, [stopTimer, cancelExam, router]);
@ -142,12 +142,6 @@ export default function ExamPage() {
> >
Submit Submit
</button> </button>
<button
onClick={showExitDialog}
className="flex-1 bg-gray-200 text-gray-900 p-6 font-bold text-lg hover:bg-gray-300 transition-colors"
>
Cancel
</button>
</div> </div>
</div> </div>
</BackgroundWrapper> </BackgroundWrapper>

View File

@ -51,11 +51,11 @@ const Header = ({
<div className={styles.profile}> <div className={styles.profile}>
<Avatar className="bg-gray-200 w-10 h-10"> <Avatar className="bg-gray-200 w-10 h-10">
<AvatarFallback className=" text-lg"> <AvatarFallback className=" text-lg">
{user?.username ? user.username.charAt(0).toUpperCase() : ""} {user?.username ? user.username.charAt(0).toUpperCase() : "U"}
</AvatarFallback> </AvatarFallback>
</Avatar> </Avatar>
<span className={styles.text}> <span className={styles.text}>
Hello, {user?.username ? user.username.split(" ")[0] : ""} Hello, {user?.username ? user.username.split(" ")[0] : "User"}
</span> </span>
</div> </div>
)} )}

View File

@ -23,7 +23,7 @@ export const TimerProvider: React.FC<{ children: React.ReactNode }> = ({
const [timeRemaining, setTimeRemaining] = useState<number>(0); const [timeRemaining, setTimeRemaining] = useState<number>(0);
const timerRef = useRef<NodeJS.Timeout | null>(null); const timerRef = useRef<NodeJS.Timeout | null>(null);
// countdown effect // Effect: run interval whenever timeRemaining is set > 0
useEffect(() => { useEffect(() => {
if (timeRemaining > 0 && !timerRef.current) { if (timeRemaining > 0 && !timerRef.current) {
timerRef.current = setInterval(() => { timerRef.current = setInterval(() => {
@ -39,16 +39,18 @@ export const TimerProvider: React.FC<{ children: React.ReactNode }> = ({
} }
return () => { return () => {
if (timerRef.current) { if (timerRef.current && timeRemaining <= 0) {
clearInterval(timerRef.current); clearInterval(timerRef.current);
timerRef.current = null; timerRef.current = null;
} }
}; };
}, [timeRemaining]); }, [timeRemaining]); // 👈 depend on timeRemaining
const resetTimer = (duration: number) => { const resetTimer = (duration: number) => {
if (timerRef.current) clearInterval(timerRef.current); if (timerRef.current) {
timerRef.current = null; clearInterval(timerRef.current);
timerRef.current = null;
}
setTimeRemaining(duration); setTimeRemaining(duration);
}; };
@ -57,11 +59,14 @@ export const TimerProvider: React.FC<{ children: React.ReactNode }> = ({
clearInterval(timerRef.current); clearInterval(timerRef.current);
timerRef.current = null; timerRef.current = null;
} }
setTimeRemaining(0);
}; };
const setInitialTime = (duration: number) => { const setInitialTime = (duration: number) => {
if (timerRef.current) clearInterval(timerRef.current); if (timerRef.current) {
timerRef.current = null; clearInterval(timerRef.current);
timerRef.current = null;
}
setTimeRemaining(duration); setTimeRemaining(duration);
}; };