Skip to content

Commit 579b344

Browse files
authored
Merge pull request #5 from or2ooo/develop
Fix test results navigation issue
2 parents bea9d80 + 8be6d12 commit 579b344

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

src/pages/Results.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@ import { questions } from '../data/questions';
44
import { topics } from '../data/topics';
55
import { motion, AnimatePresence } from 'framer-motion';
66
import { useEffect, useState } from 'react';
7+
import { useStore } from '../store/useStore';
78

89
export function Results() {
910
const location = useLocation();
1011
const navigate = useNavigate();
11-
const result = location.state?.result;
12+
const { userProfile } = useStore();
13+
14+
// Try to get result from navigation state first, then fallback to latest test result
15+
const navigationResult = location.state?.result;
16+
const latestTestResult = userProfile.testResults.length > 0 ? userProfile.testResults[0] : null;
17+
18+
// Use navigation result if available, otherwise use latest test
19+
const result = navigationResult || latestTestResult;
20+
1221
const [showDetailedReview, setShowDetailedReview] = useState(false);
1322
const [expandedQuestions, setExpandedQuestions] = useState<Set<number>>(new Set());
1423

1524
useEffect(() => {
25+
// Only redirect if there's no result at all (no navigation state AND no test history)
1626
if (!result) {
1727
navigate('/practice');
1828
}

src/pages/Test.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,10 @@ export function Test() {
5757
const handleSubmit = () => {
5858
const result = finishTest();
5959
if (result) {
60-
// Calculate score
61-
let correct = 0;
62-
currentTest.questions.forEach((qId, index) => {
63-
const question = allQuestions.find(q => q.id === qId);
64-
if (question && currentTest.answers[index] === question.correctAnswer) {
65-
correct++;
66-
}
67-
});
68-
const score = Math.round((correct / currentTest.questions.length) * 100);
69-
70-
// Navigate to results
71-
navigate('/results', { state: { result: { ...result, score } } });
60+
// Small delay to ensure state is saved before navigation
61+
setTimeout(() => {
62+
navigate('/results', { state: { result } });
63+
}, 0);
7264
}
7365
};
7466

src/store/useStore.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { create } from 'zustand';
22
import { persist } from 'zustand/middleware';
33
import type { UserProfile, TestResult, StudyProgress } from '../types';
4+
import { questions } from '../data/questions';
45

56
interface AppState {
67
// User Profile
@@ -161,12 +162,22 @@ export const useStore = create<AppState>()(
161162
const state = get();
162163
if (!state.currentTest || !state.currentTest.startTime) return null;
163164

165+
// Calculate score here
166+
let correct = 0;
167+
state.currentTest!.questions.forEach((qId, index) => {
168+
const question = questions.find(q => q.id === qId);
169+
if (question && state.currentTest!.answers[index] === question.correctAnswer) {
170+
correct++;
171+
}
172+
});
173+
const score = Math.round((correct / state.currentTest!.questions.length) * 100);
174+
164175
const result: TestResult = {
165176
id: Date.now().toString(),
166177
date: new Date(),
167178
questions: state.currentTest.questions,
168179
answers: state.currentTest.answers,
169-
score: 0, // Will be calculated by the component
180+
score,
170181
timeSpent: Date.now() - state.currentTest.startTime.getTime(),
171182
type: state.currentTest.type
172183
};

0 commit comments

Comments
 (0)