12.2. Add Sorting Getter to Vuex Store

This commit is contained in:
Fredrick W Warren 2021-02-10 15:49:53 -08:00
parent 6bde7fce56
commit 96f167549d

View File

@ -63,11 +63,29 @@ const actions = {
} }
const getters = { const getters = {
tasksFiltered: (state) => { tasksSorted: (state) => {
const tasksSorted = {}
const keysOrdered = Object.keys(state.tasks)
keysOrdered.sort((a, b) => {
const taskAProp = state.tasks[a].name.toLowerCase()
const taskBProp = state.tasks[b].name.toLowerCase()
if (taskAProp > taskBProp) return 1
else if (taskAProp < taskBProp) return -1
else return 0
})
keysOrdered.forEach((key) => {
tasksSorted[key] = state.tasks[key]
})
return tasksSorted
},
tasksFiltered: (state, getters) => {
const tasksSorted = getters.tasksSorted
const tasksFiltered = {} const tasksFiltered = {}
if (state.search) { if (state.search) {
Object.keys(state.tasks).forEach(function (key) { Object.keys(tasksSorted).forEach(function (key) {
const task = state.tasks[key], const task = tasksSorted[key],
taskNameLowerCase = task.name.toLowerCase(), taskNameLowerCase = task.name.toLowerCase(),
searchLowerCase = state.search.toLowerCase() searchLowerCase = state.search.toLowerCase()
if (taskNameLowerCase.includes(searchLowerCase)) { if (taskNameLowerCase.includes(searchLowerCase)) {
@ -76,7 +94,7 @@ const getters = {
}) })
return tasksFiltered return tasksFiltered
} }
return state.tasks return tasksSorted
}, },
tasksTodo: (state, getters) => { tasksTodo: (state, getters) => {
const tasksFiltered = getters.tasksFiltered const tasksFiltered = getters.tasksFiltered