Fix CBT session isolation and backend seed mock data exam pattern

This commit is contained in:
2026-09-21 12:15:13 +05:30
parent 63ab119f7c
commit 857ac320f1
3 changed files with 19 additions and 9 deletions

View File

@@ -151,11 +151,11 @@ export default function CBTExamPage({ params }: { params: Promise<{ examId: stri
};
}).filter(Boolean) as any[];
initExam(examId, fetchedQuestions, testData.total_time * 60);
initExam(examId, fetchedQuestions, testData.total_time * 60, userId);
setMounted(true);
} catch (err) {
console.error("Failed to load exam from network, falling back to dummy data:", err);
initExam(examId, DUMMY_QUESTIONS, 10800);
initExam(examId, DUMMY_QUESTIONS, 10800, userId);
setMounted(true);
}
}

View File

@@ -46,6 +46,7 @@ export interface ExamAttemptPayload {
}
interface CBTState {
userId: string | null
examId: string | null
attemptId: string | null
status: 'NOT_STARTED' | 'IN_PROGRESS' | 'SUBMITTED' | 'TIMED_OUT'
@@ -56,7 +57,7 @@ interface CBTState {
currentQuestionIndex: number
// Actions
initExam: (examId: string, questions: any[], duration: number) => void
initExam: (examId: string, questions: any[], duration: number, userId: string) => void
startExam: () => void
selectAnswer: (questionId: string, answer: string) => void
clearResponse: (questionId: string) => void
@@ -72,6 +73,7 @@ interface CBTState {
export const useCBTStore = create<CBTState>()(
persist(
(set, get) => ({
userId: null,
examId: null,
attemptId: null,
status: 'NOT_STARTED',
@@ -81,12 +83,13 @@ export const useCBTStore = create<CBTState>()(
answers: {},
currentQuestionIndex: 0,
initExam: (examId, questions, duration) => {
initExam: (examId, questions, duration, userId) => {
const currentExamId = get().examId;
const currentUserId = get().userId;
const currentQuestions = get().questions;
// Force reset if it's a new exam or the number of questions has changed
const needsReset = currentExamId !== examId || currentQuestions.length !== questions.length;
// Force reset if it's a new exam, new user, or the number of questions has changed
const needsReset = currentExamId !== examId || currentUserId !== userId || currentQuestions.length !== questions.length;
if (needsReset) {
const initialAnswers: Record<string, AnswerState> = {};
@@ -100,6 +103,7 @@ export const useCBTStore = create<CBTState>()(
})
set({
userId,
examId,
attemptId: null,
status: 'NOT_STARTED',
@@ -271,6 +275,7 @@ export const useCBTStore = create<CBTState>()(
resetExamState: () => {
set({
userId: null,
examId: null,
attemptId: null,
status: 'NOT_STARTED',
@@ -286,6 +291,7 @@ export const useCBTStore = create<CBTState>()(
name: 'cbt-storage', // unique name
storage: createJSONStorage(() => idbStorage), // use custom IndexedDB storage
partialize: (state) => ({
userId: state.userId,
examId: state.examId,
status: state.status,
startTime: state.startTime,