diff --git a/step2-05/demo/src/reducers/pureFunctions.ts b/step2-05/demo/src/reducers/pureFunctions.ts index 4e6f599..40b899f 100644 --- a/step2-05/demo/src/reducers/pureFunctions.ts +++ b/step2-05/demo/src/reducers/pureFunctions.ts @@ -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]; } }); diff --git a/step2-06/demo/src/reducers/pureFunctions.ts b/step2-06/demo/src/reducers/pureFunctions.ts index 69e492a..792e217 100644 --- a/step2-06/demo/src/reducers/pureFunctions.ts +++ b/step2-06/demo/src/reducers/pureFunctions.ts @@ -22,8 +22,8 @@ export function complete(state: Store['todos'], id: string) { export function clear(state: Store['todos']) { const newTodos = { ...state }; - Object.keys(state.todos).forEach(key => { - if (state.todos[key].completed) { + Object.keys(state).forEach(key => { + if (state[key].completed) { delete newTodos[key]; } }); diff --git a/step2-06/exercise/src/reducers/pureFunctions.ts b/step2-06/exercise/src/reducers/pureFunctions.ts index 69e492a..792e217 100644 --- a/step2-06/exercise/src/reducers/pureFunctions.ts +++ b/step2-06/exercise/src/reducers/pureFunctions.ts @@ -22,8 +22,8 @@ export function complete(state: Store['todos'], id: string) { export function clear(state: Store['todos']) { const newTodos = { ...state }; - Object.keys(state.todos).forEach(key => { - if (state.todos[key].completed) { + Object.keys(state).forEach(key => { + if (state[key].completed) { delete newTodos[key]; } }); diff --git a/step2-07/demo/src/reducers/pureFunctions.ts b/step2-07/demo/src/reducers/pureFunctions.ts index 69e492a..792e217 100644 --- a/step2-07/demo/src/reducers/pureFunctions.ts +++ b/step2-07/demo/src/reducers/pureFunctions.ts @@ -22,8 +22,8 @@ export function complete(state: Store['todos'], id: string) { export function clear(state: Store['todos']) { const newTodos = { ...state }; - Object.keys(state.todos).forEach(key => { - if (state.todos[key].completed) { + Object.keys(state).forEach(key => { + if (state[key].completed) { delete newTodos[key]; } }); diff --git a/step2-07/exercise/src/reducers/pureFunctions.ts b/step2-07/exercise/src/reducers/pureFunctions.ts index 69e492a..792e217 100644 --- a/step2-07/exercise/src/reducers/pureFunctions.ts +++ b/step2-07/exercise/src/reducers/pureFunctions.ts @@ -22,8 +22,8 @@ export function complete(state: Store['todos'], id: string) { export function clear(state: Store['todos']) { const newTodos = { ...state }; - Object.keys(state.todos).forEach(key => { - if (state.todos[key].completed) { + Object.keys(state).forEach(key => { + if (state[key].completed) { delete newTodos[key]; } }); diff --git a/step2-08/demo/src/reducers/pureFunctions.ts b/step2-08/demo/src/reducers/pureFunctions.ts index 69e492a..792e217 100644 --- a/step2-08/demo/src/reducers/pureFunctions.ts +++ b/step2-08/demo/src/reducers/pureFunctions.ts @@ -22,8 +22,8 @@ export function complete(state: Store['todos'], id: string) { export function clear(state: Store['todos']) { const newTodos = { ...state }; - Object.keys(state.todos).forEach(key => { - if (state.todos[key].completed) { + Object.keys(state).forEach(key => { + if (state[key].completed) { delete newTodos[key]; } }); diff --git a/step2-08/exercise/src/reducers/pureFunctions.ts b/step2-08/exercise/src/reducers/pureFunctions.ts index 1f34b0c..c8bfb58 100644 --- a/step2-08/exercise/src/reducers/pureFunctions.ts +++ b/step2-08/exercise/src/reducers/pureFunctions.ts @@ -34,8 +34,8 @@ export function complete(state: Store['todos'], id: string) { export function clear(state: Store['todos']) { const newTodos = { ...state }; - Object.keys(state.todos).forEach(key => { - if (state.todos[key].completed) { + Object.keys(state).forEach(key => { + if (state[key].completed) { delete newTodos[key]; } }); diff --git a/step2-09/demo/src/reducers/pureFunctions.ts b/step2-09/demo/src/reducers/pureFunctions.ts index dc53164..1b49648 100644 --- a/step2-09/demo/src/reducers/pureFunctions.ts +++ b/step2-09/demo/src/reducers/pureFunctions.ts @@ -26,8 +26,8 @@ export function complete(state: Store['todos'], id: string) { export function clear(state: Store['todos']) { const newTodos = { ...state }; - Object.keys(state.todos).forEach(key => { - if (state.todos[key].completed) { + Object.keys(state).forEach(key => { + if (state[key].completed) { delete newTodos[key]; } }); diff --git a/step2-09/exercise/src/reducers/pureFunctions.ts b/step2-09/exercise/src/reducers/pureFunctions.ts index dc53164..1b49648 100644 --- a/step2-09/exercise/src/reducers/pureFunctions.ts +++ b/step2-09/exercise/src/reducers/pureFunctions.ts @@ -26,8 +26,8 @@ export function complete(state: Store['todos'], id: string) { export function clear(state: Store['todos']) { const newTodos = { ...state }; - Object.keys(state.todos).forEach(key => { - if (state.todos[key].completed) { + Object.keys(state).forEach(key => { + if (state[key].completed) { delete newTodos[key]; } });