Created
September 3, 2018 08:14
-
-
Save drex44/0f5637563122a9905b8f3888a2580f61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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