Created
September 8, 2020 16:38
-
-
Save perjo927/6b48df2c488d7452d09e7debe55ad13b to your computer and use it in GitHub Desktop.
Business Logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { CONSTS } from "../redux/actions/index.js"; | |
const { ALL, DONE, IN_PROGRESS } = CONSTS.visibilityFilters; | |
export const makeRegrettable = (store, actions) => ({ | |
undo() { | |
return store.dispatch(actions.undo()); | |
}, | |
redo() { | |
return store.dispatch(actions.redo()); | |
}, | |
}); | |
export const makeTodoModifiers = (store, actions) => ({ | |
addTodo(todo) { | |
return store.dispatch(actions.addTodo(todo)); | |
}, | |
toggleTodo(id) { | |
return store.dispatch(actions.toggleTodo(id)); | |
}, | |
deleteTodo(id) { | |
return store.dispatch(actions.deleteTodo(id)); | |
}, | |
}); | |
export const makeVisibilityChanger = (store, actions) => ({ | |
changeVisibility(filter) { | |
return store.dispatch(actions.setVisibility(filter)); | |
}, | |
}); | |
export const getStoreMethods = (store, actions) => ({ | |
...makeTodoModifiers(store, actions), | |
...makeVisibilityChanger(store, actions), | |
...makeRegrettable(store, actions), | |
}); | |
export const getTodos = (store) => { | |
const { todos } = store.getState(); | |
return todos?.present || []; | |
}; | |
export const hasPastTodos = (store) => { | |
const { todos } = store.getState(); | |
if (!todos) { | |
return false; | |
} | |
return todos.past.length > 0; | |
}; | |
export const hasFutureTodos = (store) => { | |
const { todos } = store.getState(); | |
if (!todos) { | |
return false; | |
} | |
return todos.future.length > 0; | |
}; | |
export const hasTodos = (todos) => todos.length > 0; | |
export const getVisibility = (store) => { | |
const { visibility } = store.getState(); | |
return visibility; | |
}; | |
export const filterAll = (todo) => todo; | |
export const filterDone = (todo) => todo.done; | |
export const filterInProgress = (todo) => !todo.done; | |
const filterFunctionMap = new Map([ | |
[ALL, filterAll], | |
[DONE, filterDone], | |
[IN_PROGRESS, filterInProgress], | |
]); | |
export const filterTodos = (visibility, todos) => { | |
const filterFunction = filterFunctionMap.get(visibility) || filterAll; | |
return todos.filter(filterFunction); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment