Fixing the complete reducer to actually create new instance of a todo item

This commit is contained in:
Ken
2019-02-28 09:23:27 -08:00
parent 5e5717240c
commit 2105301c83
8 changed files with 51 additions and 31 deletions

View File

@@ -15,13 +15,9 @@ export function remove(state: Store['todos'], id: string) {
}
export function complete(state: Store['todos'], id: string) {
// Clone the todos
const newTodos = { ...state };
// Manipulate the completed flag
newTodos[id].completed = !newTodos[id].completed;
return newTodos;
// Clone the todo, overriding
const newTodo = { ...state[id], completed: !state[id].completed };
return { ...state, [id]: newTodo };
}
export function clear(state: Store['todos']) {

View File

@@ -20,8 +20,8 @@ export function remove(state: Store['todos'], id: string) {
export function complete(state: Store['todos'], id: string) {
// Write code:
// - to clone the state object into new state object
// - create a clone of the state[id] into a new item object
// - to clone the state[id] object into new todo object, using the spread syntax
// - in the spread syntax, also override the value of the completed key like this: {...foo, [id]: !foo[id].completed}
// - modify new state and set the id key to the value of the new item object
return state;