From a2646228d4d4d5e0ab13f5466680c887f7bb5f61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raimund=20Schl=C3=BC=C3=9Fler?= Date: Sun, 11 Nov 2018 13:33:48 +0100 Subject: [PATCH] Store tasks in array with key as index --- src/components/TheCollections/Calendar.vue | 5 +++- src/models/task.js | 10 ++++++++ src/store/calendars.js | 14 ++++++++--- src/store/collections.js | 2 +- src/store/tasks.js | 29 +++++++++++++++++++--- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/components/TheCollections/Calendar.vue b/src/components/TheCollections/Calendar.vue index 239d3a87..1b96adf2 100644 --- a/src/components/TheCollections/Calendar.vue +++ b/src/components/TheCollections/Calendar.vue @@ -128,6 +128,10 @@ export default { } }, + tasks: function() { + return Object.values(this.calendar.tasks) + }, + inputString: function() { 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)) } }, mapGetters({ - tasks: 'getTasksByRoute', completedCount: 'getCalendarCountCompleted', calendar: 'getCalendarByRoute' }) diff --git a/src/models/task.js b/src/models/task.js index 336862ed..232643a8 100644 --- a/src/models/task.js +++ b/src/models/task.js @@ -98,6 +98,16 @@ export default class Task { return Array.isArray(data) ? data[0] : data } + /** + * Return the key + * + * @readonly + * @memberof Task + */ + get key() { + return this.uid + '~' + this.calendar.id + } + /** * Return the url * diff --git a/src/store/calendars.js b/src/store/calendars.js index f8037ab7..3f4afe17 100644 --- a/src/store/calendars.js +++ b/src/store/calendars.js @@ -134,7 +134,7 @@ const getters = { */ getCalendarCountByCollectionId: (state, getters) => (calendarId, collectionId) => { 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 }).length return count @@ -258,7 +258,15 @@ const mutations = { appendTasksToCalendar(state, { calendar, tasks }) { 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 }) context.commit('appendTasksToCalendar', { calendar, tasks }) - // context.commit('appendTasks', tasks) + context.commit('appendTasks', tasks) return tasks }) .catch((error) => { diff --git a/src/store/collections.js b/src/store/collections.js index 23b2367c..47db8efa 100644 --- a/src/store/collections.js +++ b/src/store/collections.js @@ -51,7 +51,7 @@ const getters = { getCollectionCount: (state, getters, rootState) => (collectionId) => { var count = 0 rootState.calendars.calendars.forEach(calendar => { - count += calendar.tasks.filter(task => { + count += Object.values(calendar.tasks).filter(task => { return isTaskInList(task, collectionId) && !task.related }).length }) diff --git a/src/store/tasks.js b/src/store/tasks.js index 00705b1b..a4073700 100644 --- a/src/store/tasks.js +++ b/src/store/tasks.js @@ -22,9 +22,14 @@ import Vue from 'vue' import Vuex from 'vuex' +import Task from '../models/task' Vue.use(Vuex) +const state = { + tasks: {} +} + const getters = { /** * Returns all tasks corresponding to the calendar @@ -83,14 +88,14 @@ const getters = { if (!calendar) { return null } - return calendar.tasks.find(task => { + return Object.values(calendar.tasks).find(task => { return task.uri === rootState.route.params.taskId }) } // Else, we have to search all calendars var task 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 }) if (task) return task @@ -99,6 +104,24 @@ const getters = { } const mutations = { + + /** + * Store tasks into state + * + * @param {Object} state Default state + * @param {Array} 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 * @@ -186,4 +209,4 @@ const actions = { } } -export default { getters, mutations, actions } +export default { state, getters, mutations, actions }