Skip to content

Instantly share code, notes, and snippets.

@gaearon
Created January 15, 2016 19:49
Show Gist options
  • Save gaearon/763ec0e055278deae253 to your computer and use it in GitHub Desktop.
Save gaearon/763ec0e055278deae253 to your computer and use it in GitHub Desktop.
function postComments(state = [], action) {
switch(action.type) {
case 'LOAD_COMMENTS' :
return action.comments;
case 'ADD_COMMENT':
return [...state, {
user : { username: action.author },
text : action.comment
}];
case 'REMOVE_COMMENT':
return [
...state.slice(0, action.i),
...state.slice(action.i + 1)
];
default:
return state;
}
}
function comments(state = {}, action) {
if (typeof action.postId !== 'undefined') {
return {
...state,
[action.postId]: postComments(state[action.postId], action)
};
}
return state;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment