Skip to content

Instantly share code, notes, and snippets.

@wordyallen
Created April 7, 2016 23:17
Show Gist options
  • Save wordyallen/5086dee2bed9491b096501002b73d84a to your computer and use it in GitHub Desktop.
Save wordyallen/5086dee2bed9491b096501002b73d84a to your computer and use it in GitHub Desktop.
import {List, Map} from 'immutable';
function setState(state, newState) {
return state.merge(newState);
}
function vote(state, entry) {
const currentPair = state.getIn(['vote', 'pair']);
if (currentPair && currentPair.includes(entry)) {
return state.set('hasVoted', entry);
} else {
return state;
}
}
function resetVote(state) {
const hasVoted = state.get('hasVoted');
const currentPair = state.getIn(['vote', 'pair'], List());
if (hasVoted && !currentPair.includes(hasVoted)) {
return state.remove('hasVoted');
} else {
return state;
}
}
export default function(state = Map(), action) {
switch (action.type) {
case 'SET_STATE':
return resetVote(setState(state, action.state));
case 'VOTE':
return vote(state, action.entry);
}
return state;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment