fixing bug with pureFunction clear

This commit is contained in:
Ken
2019-02-26 14:22:03 -08:00
parent 540e7fb638
commit ad065d4b4a
9 changed files with 26 additions and 19 deletions

View File

@@ -1,29 +1,36 @@
import { Store, FilterTypes } from '../store';
import { Store } from '../store';
export function addTodo(state: Store['todos'], id: string, label: string): Store['todos'] {
return { ...state, [id]: { label, completed: false } };
}
export function remove(state: Store['todos'], id: string) {
// Clone the Todos
const newTodos = { ...state };
// Delete an item in the object by the key
delete newTodos[id];
return newTodos;
}
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;
}
export function clear(state: Store['todos']) {
// Clone the todos
const newTodos = { ...state };
Object.keys(state.todos).forEach(key => {
if (state.todos[key].completed) {
// 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];
}
});