Skip to content

Commit 0fc257c

Browse files
committed
fix: handling saved results in local storage
initialize "completed" to empty object, and add score fallback if not yet taken
1 parent d048d70 commit 0fc257c

File tree

2 files changed

+4
-11
lines changed

2 files changed

+4
-11
lines changed

src/components/Quiz/QuizWidget/useQuizWidget.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ export const useQuizWidget = ({
108108
if (!showResults) return
109109

110110
updateUserStats((prevStats) => {
111-
const lastScore = prevStats.completed[quizKey][1]
111+
const { completed } = prevStats
112+
const hasResultsSaved = !!completed[quizKey]
113+
const lastScore = hasResultsSaved ? prevStats.completed[quizKey][1] : 0
112114

113115
if (numberOfCorrectAnswers < lastScore) return prevStats
114116

src/components/Quiz/useLocalQuizData.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,10 @@ import { USER_STATS_KEY } from "@/lib/constants"
66

77
import { useLocalStorage } from "@/hooks/useLocalStorage"
88

9-
/**
10-
* Contains each quiz id as key and <boolean, number> to indicate if its completed and the highest score in that quiz
11-
*
12-
* Initialize all quizzes as not completed
13-
*/
14-
const INITIAL_COMPLETED_QUIZZES: CompletedQuizzes = Object.keys(
15-
allQuizzesData
16-
).reduce((object, key) => ({ ...object, [key]: [false, 0] }), {})
17-
189
export const INITIAL_USER_STATS: UserStats = {
1910
score: 0,
2011
average: [],
21-
completed: INITIAL_COMPLETED_QUIZZES,
12+
completed: {},
2213
}
2314

2415
export const useLocalQuizData = () => {

0 commit comments

Comments
 (0)