File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,15 +4,25 @@ import { questions } from '../data/questions';
44import { topics } from '../data/topics' ;
55import { motion , AnimatePresence } from 'framer-motion' ;
66import { useEffect , useState } from 'react' ;
7+ import { useStore } from '../store/useStore' ;
78
89export 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 }
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11import { create } from 'zustand' ;
22import { persist } from 'zustand/middleware' ;
33import type { UserProfile , TestResult , StudyProgress } from '../types' ;
4+ import { questions } from '../data/questions' ;
45
56interface 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 } ;
You can’t perform that action at this time.
0 commit comments