redux-course-2 icon indicating copy to clipboard operation
redux-course-2 copied to clipboard

Incorrect reduce func implementation for determine current user's vote in Poll component

Open ghost opened this issue 4 years ago • 1 comments

Hey @tylermcginnis! In the Poll state video you're writing mapStateToProps for Poll component, which contains code to figure out answer to the poll of currently authenticated user:

const vote = getVoteKeys().reduce((vote, key) => {
    if (vote !== null) {
      return vote[0]
    }

    return poll[key].includes(authedUser)
      ? key
      : vote
  }, null)

There's error here and it won't work for case when user responded with option d. Because it's last element in input array for reduce and you're just returning key. You could update it to:

const vote = getVoteKeys().reduce((vote, key) => {
    if (vote !== null) {
      return vote
    }

    return poll[key].includes(authedUser)
      ? key[0]  // < --- I've moved indexer to take first letter here
      : vote
  }, null)

to make it work correctly.

On the published version (in published branch) there is the same problem with slightly different code.

ghost avatar Apr 25 '20 18:04 ghost

Thanks! I'll get a fix out soon.

tylermcginnis avatar May 04 '20 00:05 tylermcginnis