Skip to content

Instantly share code, notes, and snippets.

@simontreny
Last active August 30, 2019 14:07
Show Gist options
  • Save simontreny/169905ecfad29a18dea863610a63d87c to your computer and use it in GitHub Desktop.
Save simontreny/169905ecfad29a18dea863610a63d87c to your computer and use it in GitHub Desktop.
const initialState = {
visibilityFilter: VisibilityFilters.SHOW_ALL,
todos: []
}
function todos(state = [], action) {
switch (action.type) {
case ADD_TODO:
return [
...state,
{
text: action.text,
completed: false
}
]
case TOGGLE_TODO:
return state.map((todo, index) => {
if (index === action.index) {
return Object.assign({}, todo, {
completed: !todo.completed
})
}
return todo
})
default:
return state
}
}
function todoApp(state = initialState, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return Object.assign({}, state, {
visibilityFilter: action.filter
})
case ADD_TODO:
return Object.assign({}, state, {
todos: todos(state.todos, action)
})
case TOGGLE_TODO:
return Object.assign({}, state, {
todos: todos(state.todos, action)
})
default:
return state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment