Store tasks in array with key as index

This commit is contained in:
Raimund Schlüßler 2018-11-11 13:33:48 +01:00
parent dc9bd756cc
commit a2646228d4
No known key found for this signature in database
GPG key ID: 036FA7EB1A599178
5 changed files with 52 additions and 8 deletions

View file

@ -128,6 +128,10 @@ export default {
} }
}, },
tasks: function() {
return Object.values(this.calendar.tasks)
},
inputString: function() { inputString: function() {
return t('tasks', 'Add a task to "{calendar}"...', { calendar: this.calendar.displayName }) return t('tasks', 'Add a task to "{calendar}"...', { calendar: this.calendar.displayName })
}, },
@ -141,7 +145,6 @@ export default {
return n('tasks', '%n Completed Task', '%n Completed Tasks', this.completedCount(this.calendarId)) return n('tasks', '%n Completed Task', '%n Completed Tasks', this.completedCount(this.calendarId))
} }, } },
mapGetters({ mapGetters({
tasks: 'getTasksByRoute',
completedCount: 'getCalendarCountCompleted', completedCount: 'getCalendarCountCompleted',
calendar: 'getCalendarByRoute' calendar: 'getCalendarByRoute'
}) })

View file

@ -98,6 +98,16 @@ export default class Task {
return Array.isArray(data) ? data[0] : data return Array.isArray(data) ? data[0] : data
} }
/**
* Return the key
*
* @readonly
* @memberof Task
*/
get key() {
return this.uid + '~' + this.calendar.id
}
/** /**
* Return the url * Return the url
* *

View file

@ -134,7 +134,7 @@ const getters = {
*/ */
getCalendarCountByCollectionId: (state, getters) => (calendarId, collectionId) => { getCalendarCountByCollectionId: (state, getters) => (calendarId, collectionId) => {
var calendar = getters.getCalendarById(calendarId) var calendar = getters.getCalendarById(calendarId)
var count = calendar.tasks.filter(task => { var count = Object.values(calendar.tasks).filter(task => {
return isTaskInList(task, collectionId) && !task.related return isTaskInList(task, collectionId) && !task.related
}).length }).length
return count return count
@ -258,7 +258,15 @@ const mutations = {
appendTasksToCalendar(state, { calendar, tasks }) { appendTasksToCalendar(state, { calendar, tasks }) {
calendar = state.calendars.find(search => search === calendar) calendar = state.calendars.find(search => search === calendar)
Vue.set(calendar, 'tasks', tasks) // convert list into an array and remove duplicate
calendar.tasks = tasks.reduce((list, task) => {
if (list[task.uid]) {
console.debug('Duplicate task overridden', list[task.uid], task)
}
Vue.set(list, task.uid, task)
return list
}, calendar.tasks)
}, },
/** /**
@ -447,7 +455,7 @@ const actions = {
return task return task
}) })
context.commit('appendTasksToCalendar', { calendar, tasks }) context.commit('appendTasksToCalendar', { calendar, tasks })
// context.commit('appendTasks', tasks) context.commit('appendTasks', tasks)
return tasks return tasks
}) })
.catch((error) => { .catch((error) => {

View file

@ -51,7 +51,7 @@ const getters = {
getCollectionCount: (state, getters, rootState) => (collectionId) => { getCollectionCount: (state, getters, rootState) => (collectionId) => {
var count = 0 var count = 0
rootState.calendars.calendars.forEach(calendar => { rootState.calendars.calendars.forEach(calendar => {
count += calendar.tasks.filter(task => { count += Object.values(calendar.tasks).filter(task => {
return isTaskInList(task, collectionId) && !task.related return isTaskInList(task, collectionId) && !task.related
}).length }).length
}) })

View file

@ -22,9 +22,14 @@
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import Task from '../models/task'
Vue.use(Vuex) Vue.use(Vuex)
const state = {
tasks: {}
}
const getters = { const getters = {
/** /**
* Returns all tasks corresponding to the calendar * Returns all tasks corresponding to the calendar
@ -83,14 +88,14 @@ const getters = {
if (!calendar) { if (!calendar) {
return null return null
} }
return calendar.tasks.find(task => { return Object.values(calendar.tasks).find(task => {
return task.uri === rootState.route.params.taskId return task.uri === rootState.route.params.taskId
}) })
} }
// Else, we have to search all calendars // Else, we have to search all calendars
var task var task
for (let calendar of rootState.calendars.calendars) { for (let calendar of rootState.calendars.calendars) {
task = calendar.tasks.find(task => { task = Object.values(calendar.tasks).find(task => {
return task.uri === rootState.route.params.taskId return task.uri === rootState.route.params.taskId
}) })
if (task) return task if (task) return task
@ -99,6 +104,24 @@ const getters = {
} }
const mutations = { const mutations = {
/**
* Store tasks into state
*
* @param {Object} state Default state
* @param {Array<Task>} tasks Tasks
*/
appendTasks(state, tasks = []) {
state.tasks = tasks.reduce(function(list, task) {
if (task instanceof Task) {
Vue.set(list, task.key, task)
} else {
console.error('Wrong task object', task)
}
return list
}, state.tasks)
},
/** /**
* Deletes a task * Deletes a task
* *
@ -186,4 +209,4 @@ const actions = {
} }
} }
export default { getters, mutations, actions } export default { state, getters, mutations, actions }