Skip to content

Instantly share code, notes, and snippets.

@relaxedtomato
Last active June 28, 2016 01:58
Show Gist options
  • Save relaxedtomato/ef95004b5349d59b1d3f2cec28eab091 to your computer and use it in GitHub Desktop.
Save relaxedtomato/ef95004b5349d59b1d3f2cec28eab091 to your computer and use it in GitHub Desktop.
const initialState = {
  visibilityFilter: VisibilityFilters.SHOW_ALL,
  todos: []
}

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: [
          ...state.todos,
          {
            text: action.text,
            completed: false
          }
        ]
      })
    case TOGGLE_TODO:
      return Object.assign({}, state, {
        todos: state.todos.map((todo, index) => {
          if(index === action.index) {
            return Object.assign({}, todo, {
              completed: !todo.completed
            })
          }
          return todo
        })
      })
    default:
      return state
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment