-
Notifications
You must be signed in to change notification settings - Fork 74
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
Add progress bar and score percentage as discussed in #102 #104
Add progress bar and score percentage as discussed in #102 #104
Conversation
Codecov Report
@@ Coverage Diff @@
## master #104 +/- ##
==========================================
- Coverage 89.26% 88.33% -0.93%
==========================================
Files 28 30 +2
Lines 149 180 +31
Branches 13 17 +4
==========================================
+ Hits 133 159 +26
- Misses 15 20 +5
Partials 1 1 |
src/components/Question/Question.js
Outdated
// return a new score object | ||
// NOTE: possible bug; if we declare `newScore` by initializing to | ||
// `score`'s properties ( newScore = { books: ..., } etc. ) | ||
// vs. initializing it as it is below (line 80) |
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.
(Lines 75-79) when implementing the updated score object, I ran into this weird bug(?). My goal was to remove newScore
altogether and just contain the entire "newScore" object as an argument to updateScore()
(line 92) without an additional variable declaration. However, this (lines 75 - 79) happens.
The app behaves as expected with the changes as they are now, but would just like to know why this happens and if I can indeed minify the code a little as I described.
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 not sure I totally understand your comment, but setting newScore = score
isn't creating a new object. It is pointing newScore
at score
. So what you are really doing is mutating score
. When you do newScore.books = score.books.map. . .
, it is the same thing as score.books = score.books.map . . .
This would be a good use case for the spread operator.
EDIT: Ok, I figured out the issue. In App.js
, we aren't sending this.state.score
to the Sidebar. We are sending score
. So we can only update it if we mutate it. We need to change it to this.state.score
and it works.
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.
Here is a way to update the score immutably:
const newScore = {...score, books: score.books.map((book, index) => {
if (index === bookId) {
return {
...book,
correct: book.correct + scoreDiff.correct,
incorrect: book.incorrect + scoreDiff.incorrect,
chapters: book.chapters.map((chapter, index) => {
if (index === chapterId) {
return {
...chapter,
questions: chapter.questions.map((question, index) => {
if (questionIndex === index) {
return {
answered: true || question.answered,
correct: isCorrect,
};
}
return question
})
}
}
return chapter
})
}
}
return book
})}
In order to use the spread operator we need to add:
plugins: ['transform-object-rest-spread'],
to the babelrc
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.
Just thinking out loud...I feel like the best way to structure score
would be to not store book.correct
or book.incorrect
. Also, for each question, instead of {answered, correct}
, we just have {status: "correct" }
, {status: "incorrect" }
, or {status: "unanswered" }
Then we should just use functions to calculate everything we need to get out of the data structure. This doesn't have to be a part of this PR...just a thought I had.
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.
Dude, this looks awesome! Couple comments about using the spread operator and fixing a bug in App.js, but nothing major
src/components/Question/Question.js
Outdated
// return a new score object | ||
// NOTE: possible bug; if we declare `newScore` by initializing to | ||
// `score`'s properties ( newScore = { books: ..., } etc. ) | ||
// vs. initializing it as it is below (line 80) |
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 not sure I totally understand your comment, but setting newScore = score
isn't creating a new object. It is pointing newScore
at score
. So what you are really doing is mutating score
. When you do newScore.books = score.books.map. . .
, it is the same thing as score.books = score.books.map . . .
This would be a good use case for the spread operator.
EDIT: Ok, I figured out the issue. In App.js
, we aren't sending this.state.score
to the Sidebar. We are sending score
. So we can only update it if we mutate it. We need to change it to this.state.score
and it works.
src/components/Question/Question.js
Outdated
// return a new score object | ||
// NOTE: possible bug; if we declare `newScore` by initializing to | ||
// `score`'s properties ( newScore = { books: ..., } etc. ) | ||
// vs. initializing it as it is below (line 80) |
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.
Here is a way to update the score immutably:
const newScore = {...score, books: score.books.map((book, index) => {
if (index === bookId) {
return {
...book,
correct: book.correct + scoreDiff.correct,
incorrect: book.incorrect + scoreDiff.incorrect,
chapters: book.chapters.map((chapter, index) => {
if (index === chapterId) {
return {
...chapter,
questions: chapter.questions.map((question, index) => {
if (questionIndex === index) {
return {
answered: true || question.answered,
correct: isCorrect,
};
}
return question
})
}
}
return chapter
})
}
}
return book
})}
In order to use the spread operator we need to add:
plugins: ['transform-object-rest-spread'],
to the babelrc
src/components/Question/Question.js
Outdated
// return a new score object | ||
// NOTE: possible bug; if we declare `newScore` by initializing to | ||
// `score`'s properties ( newScore = { books: ..., } etc. ) | ||
// vs. initializing it as it is below (line 80) |
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.
Just thinking out loud...I feel like the best way to structure score
would be to not store book.correct
or book.incorrect
. Also, for each question, instead of {answered, correct}
, we just have {status: "correct" }
, {status: "incorrect" }
, or {status: "unanswered" }
Then we should just use functions to calculate everything we need to get out of the data structure. This doesn't have to be a part of this PR...just a thought I had.
I think you need to stage your changes to .babelrc.js, package.json, and package-lock.json |
Hmmm...I made my own test branch where I added spread operator and added that line to the .babelrc and committed it, and I didn't have a problem. If you're all out of ideas, maybe just bypass the git hook, so that others can take a look and help debug? |
Just pushed the changes while bypassing the linter problem. Still trying to figure out what's causing this. |
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 believe the Sidebar book scores are showing (undefined / undefined). Probably the best way to fix this is to just have a helper function and use it in Sidebar.js to calculate the score for each book.
just pulled this branch to look at the issues, but it seems to work for me. Added a test for spread operator and that too seems fine. |
I think we're good. I'm going to re-add support for book scores in |
Just pushed the changes. Cleaned up the P.S. I'm liking the new design 😄 |
Looks good! Only thing is that the book sidebar isn't updating for me. EDIT: Looks like |
Noted, and found a few more bugs as well. Will have a resolution soon. |
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.
Looks good to me! Thanks for being open to feedback! 🙂
Small issue:
This is what the progress bar looks like right now. There's not anything behaviorally wrong with it, but I just noticed that when zoomed out, the red segment appears to be slightly lower than the green segment (it's not, if you check the page source). I'm not sure if I should bother trying to fix this as it's mostly a design issue right now, but if anyone has any suggestions I'm all ears!