Fix UI issues, timer logic, theme switcher, view solutions, and markdown rendering

This commit is contained in:
2026-07-09 16:08:12 +05:30
parent bc444ea737
commit b80d73ec26
18 changed files with 1662 additions and 41 deletions

View File

@@ -47,7 +47,8 @@ export interface ExamAttemptPayload {
interface CBTState {
examId: string | null
status: 'IN_PROGRESS' | 'SUBMITTED' | 'TIMED_OUT'
attemptId: string | null
status: 'NOT_STARTED' | 'IN_PROGRESS' | 'SUBMITTED' | 'TIMED_OUT'
startTime: number | null
totalDuration: number // in seconds
questions: any[]
@@ -56,6 +57,7 @@ interface CBTState {
// Actions
initExam: (examId: string, questions: any[], duration: number) => void
startExam: () => void
selectAnswer: (questionId: string, answer: string) => void
clearResponse: (questionId: string) => void
markForReview: (questionId: string) => void
@@ -63,7 +65,7 @@ interface CBTState {
submitExam: (reason?: 'SUBMITTED' | 'TIMED_OUT') => void
addTimeSpent: (questionId: string, seconds: number) => void
getAttemptPayload: (userId: string) => ExamAttemptPayload | null
submitAttemptToServer: (userId: string) => Promise<void>
submitAttemptToServer: (userId: string) => Promise<string | null>
resetExamState: () => void
}
@@ -71,7 +73,8 @@ export const useCBTStore = create<CBTState>()(
persist(
(set, get) => ({
examId: null,
status: 'IN_PROGRESS',
attemptId: null,
status: 'NOT_STARTED',
startTime: null,
totalDuration: 0,
questions: [],
@@ -98,8 +101,9 @@ export const useCBTStore = create<CBTState>()(
set({
examId,
status: 'IN_PROGRESS',
startTime: Date.now(), // Server-synced timestamp ideally
attemptId: null,
status: 'NOT_STARTED',
startTime: null,
totalDuration: duration,
questions,
answers: initialAnswers,
@@ -111,6 +115,10 @@ export const useCBTStore = create<CBTState>()(
}
},
startExam: () => {
set({ status: 'IN_PROGRESS', startTime: Date.now() })
},
selectAnswer: (questionId, answer) => set((state) => {
const current = state.answers[questionId]
if (!current) return state;
@@ -237,7 +245,7 @@ export const useCBTStore = create<CBTState>()(
submitAttemptToServer: async (userId: string) => {
const payload = get().getAttemptPayload(userId);
if (!payload) return;
if (!payload) return null;
try {
const res = await fetch(`/api/attempts/submit`, {
@@ -248,18 +256,24 @@ export const useCBTStore = create<CBTState>()(
if (!res.ok) {
console.error("Failed to submit attempt to server", await res.text());
return null;
} else {
console.log("Successfully submitted attempt to server");
const data = await res.json();
console.log("Successfully submitted attempt to server", data.id);
set({ attemptId: data.id });
return data.id;
}
} catch (error) {
console.error("Network error during submission:", error);
return null;
}
},
resetExamState: () => {
set({
examId: null,
status: 'IN_PROGRESS',
attemptId: null,
status: 'NOT_STARTED',
startTime: null,
totalDuration: 0,
questions: [],