Skip to content

Instantly share code, notes, and snippets.

@drex44
Created September 3, 2018 08:14
Show Gist options
  • Save drex44/0f5637563122a9905b8f3888a2580f61 to your computer and use it in GitHub Desktop.
Save drex44/0f5637563122a9905b8f3888a2580f61 to your computer and use it in GitHub Desktop.
import ACTIONS from "./action";
import _ from "lodash";
const defaultState = {
items: []
};
const todoReducer = (state = defaultState, action) => {
switch (action.type) {
case ACTIONS.Types.CREATE_ITEM: {
console.log(action);
let item = action.payload;
let newItem = { id: state.items.length + 1, description: item };
let newState = _.cloneDeep(state);
newState.items.push(newItem);
return newState;
}
case ACTIONS.Types.DELETE_ITEM: {
let newState = _.cloneDeep(state);
let index = _.findIndex(newState.items, { id: action.payload });
newState.items.splice(index, 1);
return newState;
}
default:
return state;
}
};
export default todoReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment