Skip to content

Instantly share code, notes, and snippets.

@esbb48
Created March 16, 2017 10:00
Show Gist options
  • Select an option

  • Save esbb48/245bb13cae82a80fb600e93da5a3ea9c to your computer and use it in GitHub Desktop.

Select an option

Save esbb48/245bb13cae82a80fb600e93da5a3ea9c to your computer and use it in GitHub Desktop.
export const addTodo = text => ({ type: types.ADD_TODO, text })
export const deleteTodo = id => ({ type: types.DELETE_TODO, id })
export const editTodo = (id, text) => ({ type: types.EDIT_TODO, id, text })
export const ADD_TODO = 'ADD_TODO'
export const DELETE_TODO = 'DELETE_TODO'
export const EDIT_TODO = 'EDIT_TODO'
export default function todos(state = initialState, action) {
switch (action.type) {
case ADD_TODO:
return [
{
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
completed: false,
text: action.text
},
...state
]
case DELETE_TODO:
return state.filter(todo =>
todo.id !== action.id
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment