Files
frontend-bootcamp/step2-05/demo/src/reducers/pureFunctions.ts
2019-02-26 14:22:03 -08:00

40 lines
941 B
TypeScript

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 };
// 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;
}