some exercise for 2.6
This commit is contained in:
Родитель
0e24c8ca9c
Коммит
619f17f2b8
|
@ -21,5 +21,5 @@ describe('TodoApp reducers', () => {
|
||||||
*/
|
*/
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: test remove, complete and clear
|
// TODO: add a test for remove()
|
||||||
});
|
});
|
||||||
|
|
|
@ -19,20 +19,21 @@ export function remove(state: Store['todos'], id: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function complete(state: Store['todos'], id: string) {
|
export function complete(state: Store['todos'], id: string) {
|
||||||
// Write code:
|
// Clone the todo, overriding
|
||||||
// - to clone the state[id] object into new todo object, using the spread syntax
|
const newTodo = { ...state[id], completed: !state[id].completed };
|
||||||
// - in the spread syntax, also override the value of the completed key like this: {...foo, [id]: !foo[id].completed}
|
return { ...state, [id]: newTodo };
|
||||||
// - modify new state and set the id key to the value of the new item object
|
|
||||||
|
|
||||||
return state;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clear(state: Store['todos']) {
|
export function clear(state: Store['todos']) {
|
||||||
// Write code:
|
// Clone the todos
|
||||||
// - to clone the state object into new state object
|
const newTodos = { ...state };
|
||||||
// - loop through the keys of the new state object
|
|
||||||
// - remove those items inside that new state if the item is completed using the "delete" keyword
|
|
||||||
// - return the new state
|
|
||||||
|
|
||||||
return state;
|
// Delete all todos based on the completed flag, looping over the keys of the todos
|
||||||
|
Object.keys(state).forEach(key => {
|
||||||
|
if (state[key].completed) {
|
||||||
|
delete newTodos[key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return newTodos;
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче