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"
|
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"
|
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)
|
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
|
# Create Question records from the JSON
|
||||||
q_ids = []
|
q_ids = []
|
||||||
for raw_q in raw_questions:
|
for raw_q in raw_questions:
|
||||||
@@ -475,7 +478,7 @@ def seed_local_database():
|
|||||||
correct_answer=raw_q.get("correct_answer", {"value": "A"}),
|
correct_answer=raw_q.get("correct_answer", {"value": "A"}),
|
||||||
solution_text=raw_q.get("solution_text", "Refer to standard solution."),
|
solution_text=raw_q.get("solution_text", "Refer to standard solution."),
|
||||||
language=models.MediumEnum.ENGLISH,
|
language=models.MediumEnum.ENGLISH,
|
||||||
exam=models.ExamEnum.JEE_MAIN,
|
exam=dps_pattern,
|
||||||
created_by=admin_id,
|
created_by=admin_id,
|
||||||
)
|
)
|
||||||
dps_session.add(q)
|
dps_session.add(q)
|
||||||
@@ -499,7 +502,7 @@ def seed_local_database():
|
|||||||
id=dps_id,
|
id=dps_id,
|
||||||
title=sheet_title,
|
title=sheet_title,
|
||||||
type="DPS",
|
type="DPS",
|
||||||
exam_pattern=models.ExamEnum.JEE_MAIN,
|
exam_pattern=dps_pattern,
|
||||||
sections={"Practice": {"questions": q_ids, "time_limit": 30}},
|
sections={"Practice": {"questions": q_ids, "time_limit": 30}},
|
||||||
total_time=30,
|
total_time=30,
|
||||||
difficulty="Medium",
|
difficulty="Medium",
|
||||||
@@ -519,3 +522,4 @@ def seed_local_database():
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
seed_local_database()
|
seed_local_database()
|
||||||
|
|
||||||
|
|||||||
@@ -151,11 +151,11 @@ export default function CBTExamPage({ params }: { params: Promise<{ examId: stri
|
|||||||
};
|
};
|
||||||
}).filter(Boolean) as any[];
|
}).filter(Boolean) as any[];
|
||||||
|
|
||||||
initExam(examId, fetchedQuestions, testData.total_time * 60);
|
initExam(examId, fetchedQuestions, testData.total_time * 60, userId);
|
||||||
setMounted(true);
|
setMounted(true);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Failed to load exam from network, falling back to dummy data:", 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);
|
setMounted(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ export interface ExamAttemptPayload {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface CBTState {
|
interface CBTState {
|
||||||
|
userId: string | null
|
||||||
examId: string | null
|
examId: string | null
|
||||||
attemptId: string | null
|
attemptId: string | null
|
||||||
status: 'NOT_STARTED' | 'IN_PROGRESS' | 'SUBMITTED' | 'TIMED_OUT'
|
status: 'NOT_STARTED' | 'IN_PROGRESS' | 'SUBMITTED' | 'TIMED_OUT'
|
||||||
@@ -56,7 +57,7 @@ interface CBTState {
|
|||||||
currentQuestionIndex: number
|
currentQuestionIndex: number
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
initExam: (examId: string, questions: any[], duration: number) => void
|
initExam: (examId: string, questions: any[], duration: number, userId: string) => void
|
||||||
startExam: () => void
|
startExam: () => void
|
||||||
selectAnswer: (questionId: string, answer: string) => void
|
selectAnswer: (questionId: string, answer: string) => void
|
||||||
clearResponse: (questionId: string) => void
|
clearResponse: (questionId: string) => void
|
||||||
@@ -72,6 +73,7 @@ interface CBTState {
|
|||||||
export const useCBTStore = create<CBTState>()(
|
export const useCBTStore = create<CBTState>()(
|
||||||
persist(
|
persist(
|
||||||
(set, get) => ({
|
(set, get) => ({
|
||||||
|
userId: null,
|
||||||
examId: null,
|
examId: null,
|
||||||
attemptId: null,
|
attemptId: null,
|
||||||
status: 'NOT_STARTED',
|
status: 'NOT_STARTED',
|
||||||
@@ -81,12 +83,13 @@ export const useCBTStore = create<CBTState>()(
|
|||||||
answers: {},
|
answers: {},
|
||||||
currentQuestionIndex: 0,
|
currentQuestionIndex: 0,
|
||||||
|
|
||||||
initExam: (examId, questions, duration) => {
|
initExam: (examId, questions, duration, userId) => {
|
||||||
const currentExamId = get().examId;
|
const currentExamId = get().examId;
|
||||||
|
const currentUserId = get().userId;
|
||||||
const currentQuestions = get().questions;
|
const currentQuestions = get().questions;
|
||||||
|
|
||||||
// Force reset if it's a new exam or the number of questions has changed
|
// Force reset if it's a new exam, new user, or the number of questions has changed
|
||||||
const needsReset = currentExamId !== examId || currentQuestions.length !== questions.length;
|
const needsReset = currentExamId !== examId || currentUserId !== userId || currentQuestions.length !== questions.length;
|
||||||
|
|
||||||
if (needsReset) {
|
if (needsReset) {
|
||||||
const initialAnswers: Record<string, AnswerState> = {};
|
const initialAnswers: Record<string, AnswerState> = {};
|
||||||
@@ -100,6 +103,7 @@ export const useCBTStore = create<CBTState>()(
|
|||||||
})
|
})
|
||||||
|
|
||||||
set({
|
set({
|
||||||
|
userId,
|
||||||
examId,
|
examId,
|
||||||
attemptId: null,
|
attemptId: null,
|
||||||
status: 'NOT_STARTED',
|
status: 'NOT_STARTED',
|
||||||
@@ -271,6 +275,7 @@ export const useCBTStore = create<CBTState>()(
|
|||||||
|
|
||||||
resetExamState: () => {
|
resetExamState: () => {
|
||||||
set({
|
set({
|
||||||
|
userId: null,
|
||||||
examId: null,
|
examId: null,
|
||||||
attemptId: null,
|
attemptId: null,
|
||||||
status: 'NOT_STARTED',
|
status: 'NOT_STARTED',
|
||||||
@@ -286,6 +291,7 @@ export const useCBTStore = create<CBTState>()(
|
|||||||
name: 'cbt-storage', // unique name
|
name: 'cbt-storage', // unique name
|
||||||
storage: createJSONStorage(() => idbStorage), // use custom IndexedDB storage
|
storage: createJSONStorage(() => idbStorage), // use custom IndexedDB storage
|
||||||
partialize: (state) => ({
|
partialize: (state) => ({
|
||||||
|
userId: state.userId,
|
||||||
examId: state.examId,
|
examId: state.examId,
|
||||||
status: state.status,
|
status: state.status,
|
||||||
startTime: state.startTime,
|
startTime: state.startTime,
|
||||||
|
|||||||
Reference in New Issue
Block a user