Skip to content
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

Merged
merged 5 commits into from
Feb 1, 2024
Merged

Strongly type all Quiz data types [refactor] #11992

merged 5 commits into from
Feb 1, 2024

Conversation

wackerow
Copy link
Member

Description

  • Refactor all Quiz-related interfaces to types; migrate from interfaces.ts to types.ts
  • Creates QuizKey, QuestionKey, and AnswerKey types
  • Rolls these new types out throughout Quiz components
  • Some related changes to get these components up-to-date with latest site conventions

QuizKey, QuestionKey, and AnswerKey types: Implemented using satisfies and keyof type pattern to both quiz and question banks. Allows us to create these types based on the strings chosen in each data mapping (keyof typeof pattern), while maintaining type-checking for child object structure (using satisfies)

Copy link

netlify bot commented Jan 19, 2024

Deploy Preview for ethereumorg ready!

Name Link
🔨 Latest commit 73a047f
🔍 Latest deploy log https://app.netlify.com/sites/ethereumorg/deploys/65bbb1f54d7d99000804ef4b
😎 Deploy Preview https://deploy-preview-11992--ethereumorg.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added content 🖋️ This involves copy additions or edits review needed 👀 tooling 🔧 Changes related to tooling of the project labels Jan 19, 2024
Copy link
Member

@pettinarip pettinarip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @wackerow good refactor. Left a couple of questions, thanks!


export type QuestionBank = Record<string, RawQuestion>
export type QuestionKey = keyof typeof allQuestionData
export type AnswerKey = typeof allQuestionData[QuestionKey]["answers"][number]["id"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty cool

@@ -887,6 +887,6 @@ const questionBank: QuestionBank = {
],
correctAnswerId: "h005-b",
},
}
} as const satisfies QuestionBank
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a benefit doing as const satisfies instead of just satisfies?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried without, but had to add back as const (in this commit) after it wasn't casting the typeof as literals to perform keyof on... instead it was just simplified to string (IIRC)...

The other place satisfies is used didn't give me that problem, so I kept as const off of that one.

quizKey ||
Object.keys(allQuizzesData).filter((quizUri) =>
window?.location.href.includes(quizUri)
)[0] ||
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author

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.

const showConfetti = useMemo<boolean>(
() => !!quizData && showResults && isPassingScore,
[quizData, showResults, isPassingScore]
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👍🏼

@pettinarip pettinarip merged commit f3c9824 into dev Feb 1, 2024
3 checks passed
@pettinarip pettinarip deleted the quiz-types branch February 1, 2024 15:00
This was referenced Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content 🖋️ This involves copy additions or edits tooling 🔧 Changes related to tooling of the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants