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'] { export function addTodo(state: Store['todos'], id: string, label: string): Store['todos'] {
return { ...state, [id]: { label, completed: false } }; return { ...state, [id]: { label, completed: false } };
} }
export function remove(state: Store['todos'], id: string) { export function remove(state: Store['todos'], id: string) {
// Clone the Todos
const newTodos = { ...state }; const newTodos = { ...state };
// Delete an item in the object by the key
delete newTodos[id]; delete newTodos[id];
return newTodos; return newTodos;
} }
export function complete(state: Store['todos'], id: string) { export function complete(state: Store['todos'], id: string) {
// Clone the todos
const newTodos = { ...state }; const newTodos = { ...state };
// Manipulate the completed flag
newTodos[id].completed = !newTodos[id].completed; newTodos[id].completed = !newTodos[id].completed;
return newTodos; return newTodos;
} }
export function clear(state: Store['todos']) { export function clear(state: Store['todos']) {
// Clone the todos
const newTodos = { ...state }; const newTodos = { ...state };
Object.keys(state.todos).forEach(key => { // Delete all todos based on the completed flag, looping over the keys of the todos
if (state.todos[key].completed) { Object.keys(state).forEach(key => {
if (state[key].completed) {
delete newTodos[key]; delete newTodos[key];
} }
}); });

View File

@@ -22,8 +22,8 @@ export function complete(state: Store['todos'], id: string) {
export function clear(state: Store['todos']) { export function clear(state: Store['todos']) {
const newTodos = { ...state }; const newTodos = { ...state };
Object.keys(state.todos).forEach(key => { Object.keys(state).forEach(key => {
if (state.todos[key].completed) { if (state[key].completed) {
delete newTodos[key]; delete newTodos[key];
} }
}); });

View File

@@ -22,8 +22,8 @@ export function complete(state: Store['todos'], id: string) {
export function clear(state: Store['todos']) { export function clear(state: Store['todos']) {
const newTodos = { ...state }; const newTodos = { ...state };
Object.keys(state.todos).forEach(key => { Object.keys(state).forEach(key => {
if (state.todos[key].completed) { if (state[key].completed) {
delete newTodos[key]; delete newTodos[key];
} }
}); });

View File

@@ -22,8 +22,8 @@ export function complete(state: Store['todos'], id: string) {
export function clear(state: Store['todos']) { export function clear(state: Store['todos']) {
const newTodos = { ...state }; const newTodos = { ...state };
Object.keys(state.todos).forEach(key => { Object.keys(state).forEach(key => {
if (state.todos[key].completed) { if (state[key].completed) {
delete newTodos[key]; delete newTodos[key];
} }
}); });

View File

@@ -22,8 +22,8 @@ export function complete(state: Store['todos'], id: string) {
export function clear(state: Store['todos']) { export function clear(state: Store['todos']) {
const newTodos = { ...state }; const newTodos = { ...state };
Object.keys(state.todos).forEach(key => { Object.keys(state).forEach(key => {
if (state.todos[key].completed) { if (state[key].completed) {
delete newTodos[key]; delete newTodos[key];
} }
}); });

View File

@@ -22,8 +22,8 @@ export function complete(state: Store['todos'], id: string) {
export function clear(state: Store['todos']) { export function clear(state: Store['todos']) {
const newTodos = { ...state }; const newTodos = { ...state };
Object.keys(state.todos).forEach(key => { Object.keys(state).forEach(key => {
if (state.todos[key].completed) { if (state[key].completed) {
delete newTodos[key]; delete newTodos[key];
} }
}); });

View File

@@ -34,8 +34,8 @@ export function complete(state: Store['todos'], id: string) {
export function clear(state: Store['todos']) { export function clear(state: Store['todos']) {
const newTodos = { ...state }; const newTodos = { ...state };
Object.keys(state.todos).forEach(key => { Object.keys(state).forEach(key => {
if (state.todos[key].completed) { if (state[key].completed) {
delete newTodos[key]; delete newTodos[key];
} }
}); });

View File

@@ -26,8 +26,8 @@ export function complete(state: Store['todos'], id: string) {
export function clear(state: Store['todos']) { export function clear(state: Store['todos']) {
const newTodos = { ...state }; const newTodos = { ...state };
Object.keys(state.todos).forEach(key => { Object.keys(state).forEach(key => {
if (state.todos[key].completed) { if (state[key].completed) {
delete newTodos[key]; delete newTodos[key];
} }
}); });

View File

@@ -26,8 +26,8 @@ export function complete(state: Store['todos'], id: string) {
export function clear(state: Store['todos']) { export function clear(state: Store['todos']) {
const newTodos = { ...state }; const newTodos = { ...state };
Object.keys(state.todos).forEach(key => { Object.keys(state).forEach(key => {
if (state.todos[key].completed) { if (state[key].completed) {
delete newTodos[key]; delete newTodos[key];
} }
}); });