Skip to content

Instantly share code, notes, and snippets.

@edorivai
Created June 29, 2017 14:45
Show Gist options
  • Save edorivai/727bc329c51d0d50200a926ce6cfe765 to your computer and use it in GitHub Desktop.
Save edorivai/727bc329c51d0d50200a926ce6cfe765 to your computer and use it in GitHub Desktop.
function reducer(state, action) {
switch (action.type) {
case UPDATE_VOTES:
const updateVotesList = state.polls.map((poll, ind) => {
if (ind === action.question) {
return poll.answers.map((ans, index) =>
index === action.index
? { ...ans, votes: ans.votes + action.votes }
: ans;
);
} else {
return { polls: { ...state.polls } };
}
});
return {
...state,
polls: updateVotesList,
};
default:
return state;
}
@edorivai
Copy link
Author

case UPDATE_VOTES:
      return state.polls.map((poll, ind) => (
        ind !== action.question
          ? poll
          : {
            ...poll,
            answers: poll.answers.map((ans, index) => (
              index !== action.index
                ? ans
                : { ...ans, votes: ans.votes + action.votes }
            )
          }
      );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment