Skip to content

Conversation

@huyenltnguyen
Copy link
Member

Checklist:

Updates useQuiz to allow customizing the passing grade.

Ref: https://github.com/freeCodeCamp/fCC10/issues/2#issuecomment-2391462203.

validationMessages,
onSuccess,
onFailure,
passingGrade = 100,
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 on the fence. I'm not sure if we should have a default value here, or just have it a required prop and ask clients to provide the value.

(#356 already contains a breaking change, so it's not a big deal if we have another here.)

Copy link
Contributor

Choose a reason for hiding this comment

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

I slightly prefer not defaulting, but either is fine. The main argument for not defaulting is that I don't think that's set in stone, so we may end up having to change it.

@huyenltnguyen huyenltnguyen marked this pull request as ready for review October 16, 2024 09:18
@huyenltnguyen huyenltnguyen requested a review from a team October 16, 2024 09:18
const [questions, setQuestions] =
useState<Question<AnswerT>[]>(initialQuestions);
const [correctAnswerCount, setCorrectAnswerCount] = useState(0);
const [grade, setGrade] = useState(0);
Copy link
Member Author

Choose a reason for hiding this comment

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

@ojeytonwilliams I'd like to pick your brain about this 😄

Do you think the initial value of grade and correctAnswerCount should be null instead of 0? Being null would mean that the quiz hasn't been validated, and 0 would mean the quiz has been validated and the value is 0.

That's a little more work for us to handle typing, but client would then be able to conditionally render the grade and correctAnswerCount values using null check, rather than having to pair them with a hasSubmitted boolean.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good question! I think null (or perhaps undefined) makes sense, mostly because it doesn't seem right to say that someone has a grade or number of correct answers until they have been graded.

One thing to consider is a return type like this:

{ validated: true, grade: number, correctAnswerCount: number, ... } | { validated: false, grade: never, correctAnswerCount: never,  ... }

That way the client can, in principle, just check the value of validated, rather than having to account for both grade and correctAnswerCount being nullable. I say "in principle", because I'm not 100% sure how the code would look in practice, but I think it should work.

Comment on lines 25 to 27
type ValidationData =
| { validated: true; grade: number; correctAnswerCount: number }
| { validated: false; grade: undefined; correctAnswerCount: undefined };
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 went with this because:

  • I don't think I can return type never.

    Screenshot 2024-10-17 at 05 04 17
  • And leaving grade and correctAnswerCount out of the return type:

    type ValidationData =
      | { validated: true; grade: number; correctAnswerCount: number }
      | { validated: false };

    Would cause an error in the consumption (in the useQuiz storybook file):

    Screenshot 2024-10-17 at 05 06 44

Copy link
Contributor

@ojeytonwilliams ojeytonwilliams left a comment

Choose a reason for hiding this comment

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

I got the type right this time 😰

huyenltnguyen and others added 2 commits October 17, 2024 02:49
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
Copy link
Contributor

@ojeytonwilliams ojeytonwilliams left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@huyenltnguyen huyenltnguyen merged commit beb8a08 into freeCodeCamp:main Oct 18, 2024
8 checks passed
@huyenltnguyen huyenltnguyen deleted the feat/passing-grade branch October 18, 2024 09:21
@huyenltnguyen huyenltnguyen mentioned this pull request Oct 18, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants