Last active
August 30, 2019 14:07
-
-
Save simontreny/169905ecfad29a18dea863610a63d87c 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
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