-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strongly type all Quiz data types [refactor] #11992
Changes from 3 commits
c4f51bb
da539e3
e5d5f17
757c4bd
73a047f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import { useEffect, useMemo, useRef, useState } from "react" | ||
import { useEffect, useMemo, useState } from "react" | ||
import { shuffle } from "lodash" | ||
import { useTranslation } from "next-i18next" | ||
|
||
import { | ||
import type { | ||
AnswerChoice, | ||
Question, | ||
Quiz, | ||
QuizKey, | ||
RawQuestion, | ||
RawQuiz, | ||
} from "@/lib/interfaces" | ||
} from "@/lib/types" | ||
|
||
import allQuizzesData from "@/data/quizzes" | ||
import questionBank from "@/data/quizzes/questionBank" | ||
|
@@ -28,11 +29,9 @@ export const useQuizWidget = ({ | |
const { t } = useTranslation() | ||
|
||
const [quizData, setQuizData] = useState<Quiz | null>(null) | ||
const [nextQuiz, setNextQuiz] = useState<string | undefined>(undefined) | ||
const [userQuizProgress, setUserQuizProgress] = useState<Array<AnswerChoice>>( | ||
[] | ||
) | ||
const [showAnswer, setShowAnswer] = useState<boolean>(false) | ||
const [nextQuiz, setNextQuiz] = useState<QuizKey | undefined>(undefined) | ||
const [userQuizProgress, setUserQuizProgress] = useState<AnswerChoice[]>([]) | ||
const [showAnswer, setShowAnswer] = useState(false) | ||
const [currentQuestionAnswerChoice, setCurrentQuestionAnswerChoice] = | ||
useState<AnswerChoice | null>(null) | ||
|
||
|
@@ -46,18 +45,9 @@ export const useQuizWidget = ({ | |
setUserQuizProgress([]) | ||
setShowAnswer(false) | ||
|
||
const currentQuizKey = | ||
quizKey || | ||
Object.keys(allQuizzesData).filter((quizUri) => | ||
window?.location.href.includes(quizUri) | ||
)[0] || | ||
null | ||
|
||
if (!currentQuizKey) return | ||
|
||
// Get quiz data from key, shuffle, then truncate if necessary | ||
const rawQuiz: RawQuiz = allQuizzesData[currentQuizKey] | ||
const questions: Array<Question> = rawQuiz.questions.map((id) => { | ||
const rawQuiz: RawQuiz = allQuizzesData[quizKey] | ||
const questions: Question[] = rawQuiz.questions.map((id) => { | ||
const rawQuestion: RawQuestion = questionBank[id] | ||
return { id, ...rawQuestion } | ||
}) | ||
|
@@ -99,10 +89,7 @@ export const useQuizWidget = ({ | |
const quizScore = Math.floor(ratioCorrect * 100) | ||
const isPassingScore = quizScore > PASSING_QUIZ_SCORE | ||
|
||
const showConfetti = useMemo<boolean>( | ||
() => !!quizData && showResults && isPassingScore, | ||
[quizData, showResults, isPassingScore] | ||
) | ||
const showConfetti = !!quizData && showResults && isPassingScore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense 👍🏼 |
||
|
||
useEffect(() => { | ||
if (!showResults) return | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,13 @@ | ||
import { CompletedQuizzes, UserStats } from "@/lib/types" | ||
|
||
import allQuizzesData from "@/data/quizzes" | ||
|
||
import { USER_STATS_KEY } from "@/lib/constants" | ||
|
||
import { useLocalStorage } from "@/hooks/useLocalStorage" | ||
|
||
/** | ||
* Contains each quiz id as key and <boolean, number> to indicate if its completed and the highest score in that quiz | ||
* | ||
* Initialize all quizzes as not completed | ||
*/ | ||
const INITIAL_COMPLETED_QUIZZES: CompletedQuizzes = Object.keys( | ||
allQuizzesData | ||
).reduce((object, key) => ({ ...object, [key]: [false, 0] }), {}) | ||
|
||
export const INITIAL_USER_STATS: UserStats = { | ||
score: 0, | ||
average: [], | ||
completed: INITIAL_COMPLETED_QUIZZES, | ||
completed: {} as CompletedQuizzes, | ||
} | ||
|
||
export const useLocalQuizData = () => { | ||
const data = useLocalStorage(USER_STATS_KEY, INITIAL_USER_STATS) | ||
|
||
return data | ||
} | ||
export const useLocalQuizData = () => useLocalStorage<UserStats>(USER_STATS_KEY, INITIAL_USER_STATS) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
// Import data types | ||
import type { QuestionBank } from "@/lib/interfaces" | ||
import type { QuestionBank } from "@/lib/types" | ||
|
||
// Declare hash map of question bank | ||
const questionBank: QuestionBank = { | ||
const questionBank = { | ||
// What is Ethereum? | ||
a001: { | ||
prompt: "a001-prompt", | ||
|
@@ -887,6 +887,6 @@ const questionBank: QuestionBank = { | |
], | ||
correctAnswerId: "h005-b", | ||
}, | ||
} | ||
} as const satisfies QuestionBank | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a benefit doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried without, but had to add back The other place |
||
|
||
export default questionBank |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a search and it seems that we always pass a
quizKey
, this default is never used. I'm ok with removing it if not used, but just curious about its original purpose, do you know it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm honestly not sure... but that was my same reasoning... there was no circumstance where the fallback would be used since a valid key is required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking a little here, it may have simply been a product of not having proper typing, so any
string
could be used which may-or-may-not be available. This shouldn't be an issue now.