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

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