mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
40 lines
941 B
TypeScript
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;
|
|
}
|