Fix CBT session isolation and backend seed mock data exam pattern
This commit is contained in:
@@ -457,9 +457,12 @@ def seed_local_database():
|
||||
|
||||
subject = raw_questions[0].get("subject", "General") if raw_questions else "General"
|
||||
topic = raw_questions[0].get("topic", "Chapter 4") if raw_questions else "Chapter 4"
|
||||
sheet_title = f"{subject} — {topic} DPS"
|
||||
sheet_title = f"{subject} DPS"
|
||||
sheet_date = date.today() - timedelta(days=i)
|
||||
|
||||
is_neet = any(s in subject.lower() for s in ["botany", "zoology", "bio", "biology"])
|
||||
dps_pattern = models.ExamEnum.NEET if is_neet else models.ExamEnum.JEE_MAIN
|
||||
|
||||
# Create Question records from the JSON
|
||||
q_ids = []
|
||||
for raw_q in raw_questions:
|
||||
@@ -475,7 +478,7 @@ def seed_local_database():
|
||||
correct_answer=raw_q.get("correct_answer", {"value": "A"}),
|
||||
solution_text=raw_q.get("solution_text", "Refer to standard solution."),
|
||||
language=models.MediumEnum.ENGLISH,
|
||||
exam=models.ExamEnum.JEE_MAIN,
|
||||
exam=dps_pattern,
|
||||
created_by=admin_id,
|
||||
)
|
||||
dps_session.add(q)
|
||||
@@ -499,7 +502,7 @@ def seed_local_database():
|
||||
id=dps_id,
|
||||
title=sheet_title,
|
||||
type="DPS",
|
||||
exam_pattern=models.ExamEnum.JEE_MAIN,
|
||||
exam_pattern=dps_pattern,
|
||||
sections={"Practice": {"questions": q_ids, "time_limit": 30}},
|
||||
total_time=30,
|
||||
difficulty="Medium",
|
||||
@@ -519,3 +522,4 @@ def seed_local_database():
|
||||
|
||||
if __name__ == "__main__":
|
||||
seed_local_database()
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user