2021-09-11 17:53:03 +02:00
|
|
|
import cloneDeep from 'lodash.clonedeep'
|
|
|
|
|
2021-10-07 12:20:52 +02:00
|
|
|
import {findById, findIndexById} from '@/helpers/utils'
|
2021-09-11 17:53:03 +02:00
|
|
|
import {i18n} from '@/i18n'
|
|
|
|
import {success} from '@/message'
|
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
import BucketService from '../../services/bucket'
|
2020-05-09 22:08:18 +02:00
|
|
|
import {setLoading} from '../helper'
|
2021-03-10 11:59:29 +01:00
|
|
|
import TaskCollectionService from '@/services/taskCollection'
|
|
|
|
|
2021-09-08 11:59:46 +02:00
|
|
|
const TASKS_PER_BUCKET = 25
|
2020-05-09 19:00:54 +02:00
|
|
|
|
2021-03-24 21:16:56 +01:00
|
|
|
const addTaskToBucketAndSort = (state, task) => {
|
2021-09-07 15:42:41 +02:00
|
|
|
const bucketIndex = findIndexById(state.buckets, task.bucketId)
|
|
|
|
state.buckets[bucketIndex].tasks.push(task)
|
|
|
|
state.buckets[bucketIndex].tasks.sort((a, b) => a.kanbanPosition > b.kanbanPosition ? 1 : -1)
|
2021-03-24 21:16:56 +01:00
|
|
|
}
|
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
/**
|
|
|
|
* This store is intended to hold the currently active kanban view.
|
|
|
|
* It should hold only the current buckets.
|
|
|
|
*/
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
state: () => ({
|
|
|
|
buckets: [],
|
2020-05-21 11:35:09 +02:00
|
|
|
listId: 0,
|
2021-03-10 11:59:29 +01:00
|
|
|
bucketLoading: {},
|
|
|
|
taskPagesPerBucket: {},
|
|
|
|
allTasksLoadedForBucket: {},
|
2020-05-09 19:00:54 +02:00
|
|
|
}),
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
mutations: {
|
2020-05-21 11:35:09 +02:00
|
|
|
setListId(state, listId) {
|
2020-07-24 18:47:33 +02:00
|
|
|
state.listId = parseInt(listId)
|
2020-05-21 11:35:09 +02:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
setBuckets(state, buckets) {
|
|
|
|
state.buckets = buckets
|
2021-03-10 11:59:29 +01:00
|
|
|
buckets.forEach(b => {
|
2021-08-19 21:35:38 +02:00
|
|
|
state.taskPagesPerBucket[b.id] = 1
|
|
|
|
state.allTasksLoadedForBucket[b.id] = false
|
2021-03-10 11:59:29 +01:00
|
|
|
})
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
addBucket(state, bucket) {
|
|
|
|
state.buckets.push(bucket)
|
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
removeBucket(state, bucket) {
|
2021-09-07 15:42:41 +02:00
|
|
|
const bucketIndex = findIndexById(state.buckets, bucket.id)
|
|
|
|
state.buckets.splice(bucketIndex, 1)
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
setBucketById(state, bucket) {
|
2021-09-07 15:42:41 +02:00
|
|
|
const bucketIndex = findIndexById(state.buckets, bucket.id)
|
|
|
|
state.buckets[bucketIndex] = bucket
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
setBucketByIndex(state, {bucketIndex, bucket}) {
|
2021-08-19 21:35:38 +02:00
|
|
|
state.buckets[bucketIndex] = bucket
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
setTaskInBucketByIndex(state, {bucketIndex, taskIndex, task}) {
|
|
|
|
const bucket = state.buckets[bucketIndex]
|
|
|
|
bucket.tasks[taskIndex] = task
|
2021-08-19 21:35:38 +02:00
|
|
|
state.buckets[bucketIndex] = bucket
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
|
|
|
setTasksInBucketByBucketId(state, {bucketId, tasks}) {
|
|
|
|
const bucketIndex = findIndexById(state.buckets, bucketId)
|
|
|
|
state.buckets[bucketIndex] = {
|
|
|
|
...state.buckets[bucketIndex],
|
|
|
|
tasks,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
setTaskInBucket(state, task) {
|
|
|
|
// If this gets invoked without any tasks actually loaded, we can save the hassle of finding the task
|
|
|
|
if (state.buckets.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-24 21:16:56 +01:00
|
|
|
let found = false
|
|
|
|
|
|
|
|
const findAndUpdate = b => {
|
|
|
|
for (const t in state.buckets[b].tasks) {
|
|
|
|
if (state.buckets[b].tasks[t].id === task.id) {
|
|
|
|
const bucket = state.buckets[b]
|
|
|
|
bucket.tasks[t] = task
|
|
|
|
|
|
|
|
if (bucket.id !== task.bucketId) {
|
|
|
|
bucket.tasks.splice(t, 1)
|
|
|
|
addTaskToBucketAndSort(state, task)
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:35:38 +02:00
|
|
|
state.buckets[b] = bucket
|
|
|
|
|
2021-03-24 21:16:56 +01:00
|
|
|
found = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
for (const b in state.buckets) {
|
|
|
|
if (state.buckets[b].id === task.bucketId) {
|
2021-03-24 21:16:56 +01:00
|
|
|
findAndUpdate(b)
|
|
|
|
if (found) {
|
|
|
|
return
|
2020-05-09 19:00:54 +02:00
|
|
|
}
|
2021-03-24 21:16:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const b in state.buckets) {
|
|
|
|
findAndUpdate(b)
|
|
|
|
if (found) {
|
2020-05-09 19:00:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
addTaskToBucket(state, task) {
|
2021-09-07 15:42:41 +02:00
|
|
|
const bucketIndex = findIndexById(state.buckets, task.bucketId)
|
|
|
|
const oldBucket = state.buckets[bucketIndex]
|
|
|
|
const newBucket = {
|
|
|
|
...oldBucket,
|
|
|
|
tasks: [
|
|
|
|
...oldBucket.tasks,
|
|
|
|
task,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
state.buckets[bucketIndex] = newBucket
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
2021-03-10 11:59:29 +01:00
|
|
|
|
2021-09-07 15:42:41 +02:00
|
|
|
addTasksToBucket(state, {tasks, bucketId}) {
|
|
|
|
const bucketIndex = findIndexById(state.buckets, bucketId)
|
|
|
|
const oldBucket = state.buckets[bucketIndex]
|
|
|
|
const newBucket = {
|
|
|
|
...oldBucket,
|
|
|
|
tasks: [
|
|
|
|
...oldBucket.tasks,
|
2021-10-11 15:14:27 +02:00
|
|
|
...tasks,
|
2021-09-07 15:42:41 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
state.buckets[bucketIndex] = newBucket
|
2021-03-10 11:59:29 +01:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-11 17:24:51 +02:00
|
|
|
removeTaskInBucket(state, task) {
|
|
|
|
// If this gets invoked without any tasks actually loaded, we can save the hassle of finding the task
|
|
|
|
if (state.buckets.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-11 17:50:53 +02:00
|
|
|
for (const b in state.buckets) {
|
|
|
|
if (state.buckets[b].id === task.bucketId) {
|
|
|
|
for (const t in state.buckets[b].tasks) {
|
|
|
|
if (state.buckets[b].tasks[t].id === task.id) {
|
|
|
|
const bucket = state.buckets[b]
|
|
|
|
bucket.tasks.splice(t, 1)
|
|
|
|
state.buckets[b] = bucket
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-09-05 22:35:52 +02:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2021-03-10 11:59:29 +01:00
|
|
|
setBucketLoading(state, {bucketId, loading}) {
|
2021-08-19 21:35:38 +02:00
|
|
|
state.bucketLoading[bucketId] = loading
|
2021-03-10 11:59:29 +01:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2021-03-10 11:59:29 +01:00
|
|
|
setTasksLoadedForBucketPage(state, {bucketId, page}) {
|
2021-08-19 21:35:38 +02:00
|
|
|
state.taskPagesPerBucket[bucketId] = page
|
2021-03-10 11:59:29 +01:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2021-03-10 11:59:29 +01:00
|
|
|
setAllTasksLoadedForBucket(state, bucketId) {
|
2021-08-19 21:35:38 +02:00
|
|
|
state.allTasksLoadedForBucket[bucketId] = true
|
2021-03-10 11:59:29 +01:00
|
|
|
},
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
getters: {
|
2021-09-07 15:42:41 +02:00
|
|
|
getBucketById(state) {
|
|
|
|
return (bucketId) => findById(state.buckets, bucketId)
|
|
|
|
},
|
2021-09-11 17:53:03 +02:00
|
|
|
|
|
|
|
getTaskById(state) {
|
|
|
|
return (id) => {
|
|
|
|
let taskIndex
|
|
|
|
const bucketIndex = state.buckets.findIndex(({ tasks }) => {
|
|
|
|
taskIndex = findIndexById(tasks, id)
|
2021-10-07 12:52:42 +02:00
|
|
|
return taskIndex !== -1
|
2021-09-11 17:53:03 +02:00
|
|
|
})
|
2021-10-07 12:52:42 +02:00
|
|
|
|
2021-09-11 17:53:03 +02:00
|
|
|
return {
|
2021-10-07 12:52:42 +02:00
|
|
|
bucketIndex: bucketIndex !== -1 ? bucketIndex : null,
|
|
|
|
taskIndex: taskIndex !== -1 ? taskIndex : null,
|
|
|
|
task: state.buckets[bucketIndex]?.tasks?.[taskIndex] || null,
|
2020-05-09 19:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
actions: {
|
2020-12-22 12:49:34 +01:00
|
|
|
loadBucketsForList(ctx, {listId, params}) {
|
2021-01-09 15:24:06 +01:00
|
|
|
const cancel = setLoading(ctx, 'kanban')
|
2020-05-09 22:08:18 +02:00
|
|
|
|
|
|
|
// Clear everything to prevent having old buckets in the list if loading the buckets from this list takes a few moments
|
|
|
|
ctx.commit('setBuckets', [])
|
|
|
|
|
2021-09-08 11:59:46 +02:00
|
|
|
params.per_page = TASKS_PER_BUCKET
|
2021-03-10 11:59:29 +01:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
const bucketService = new BucketService()
|
2020-12-22 12:49:34 +01:00
|
|
|
return bucketService.getAll({listId: listId}, params)
|
2020-05-09 19:00:54 +02:00
|
|
|
.then(r => {
|
|
|
|
ctx.commit('setBuckets', r)
|
2020-05-21 11:35:09 +02:00
|
|
|
ctx.commit('setListId', listId)
|
2020-10-24 17:23:13 +02:00
|
|
|
return Promise.resolve(r)
|
2020-05-09 19:00:54 +02:00
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2020-05-09 22:08:18 +02:00
|
|
|
.finally(() => {
|
|
|
|
cancel()
|
|
|
|
})
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
2021-09-07 15:42:41 +02:00
|
|
|
|
2021-03-10 11:59:29 +01:00
|
|
|
loadNextTasksForBucket(ctx, {listId, ps = {}, bucketId}) {
|
2021-09-07 15:42:41 +02:00
|
|
|
const bucketIndex = findIndexById(ctx.state.buckets, bucketId)
|
|
|
|
|
|
|
|
const isLoading = ctx.state.bucketLoading[bucketIndex] ?? false
|
2021-03-10 11:59:29 +01:00
|
|
|
if (isLoading) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
2021-09-07 15:42:41 +02:00
|
|
|
const page = (ctx.state.taskPagesPerBucket[bucketIndex] ?? 1) + 1
|
2021-03-10 11:59:29 +01:00
|
|
|
|
2021-09-07 15:42:41 +02:00
|
|
|
const alreadyLoaded = ctx.state.allTasksLoadedForBucket[bucketIndex] ?? false
|
2021-03-10 11:59:29 +01:00
|
|
|
if (alreadyLoaded) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
const cancel = setLoading(ctx, 'kanban')
|
|
|
|
ctx.commit('setBucketLoading', {bucketId: bucketId, loading: true})
|
|
|
|
|
2021-10-06 22:25:06 +02:00
|
|
|
const params = JSON.parse(JSON.stringify(ps))
|
2021-03-10 11:59:29 +01:00
|
|
|
|
2021-07-28 21:56:29 +02:00
|
|
|
params.sort_by = 'kanban_position'
|
2021-03-10 11:59:29 +01:00
|
|
|
params.order_by = 'asc'
|
|
|
|
|
2021-09-11 17:53:03 +02:00
|
|
|
// const hasBucketFilter = Object.entries(params.filter_by).some(([key, value]) => {
|
|
|
|
// const condition = value === 'bucket_id'
|
|
|
|
// if (condition) {
|
|
|
|
// if (value !== bucketId) {
|
|
|
|
// params.filter_value[key] = bucketId
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return condition
|
|
|
|
// })
|
2021-03-10 11:59:29 +01:00
|
|
|
let hasBucketFilter = false
|
|
|
|
for (const f in params.filter_by) {
|
|
|
|
if (params.filter_by[f] === 'bucket_id') {
|
|
|
|
hasBucketFilter = true
|
|
|
|
if (params.filter_value[f] !== bucketId) {
|
|
|
|
params.filter_value[f] = bucketId
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasBucketFilter) {
|
|
|
|
params.filter_by = [...(params.filter_by ?? []), 'bucket_id']
|
|
|
|
params.filter_value = [...(params.filter_value ?? []), bucketId]
|
|
|
|
params.filter_comparator = [...(params.filter_comparator ?? []), 'equals']
|
|
|
|
}
|
|
|
|
|
2021-09-08 11:59:46 +02:00
|
|
|
params.per_page = TASKS_PER_BUCKET
|
2021-03-10 11:59:29 +01:00
|
|
|
|
|
|
|
const taskService = new TaskCollectionService()
|
|
|
|
return taskService.getAll({listId: listId}, params, page)
|
|
|
|
.then(r => {
|
|
|
|
ctx.commit('addTasksToBucket', {tasks: r, bucketId: bucketId})
|
|
|
|
ctx.commit('setTasksLoadedForBucketPage', {bucketId, page})
|
|
|
|
if (taskService.totalPages <= page) {
|
|
|
|
ctx.commit('setAllTasksLoadedForBucket', bucketId)
|
|
|
|
}
|
|
|
|
return Promise.resolve(r)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
cancel()
|
|
|
|
ctx.commit('setBucketLoading', {bucketId: bucketId, loading: false})
|
|
|
|
})
|
|
|
|
},
|
2021-09-11 17:53:03 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
createBucket(ctx, bucket) {
|
2021-01-09 15:24:06 +01:00
|
|
|
const cancel = setLoading(ctx, 'kanban')
|
2020-05-09 22:08:18 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
const bucketService = new BucketService()
|
|
|
|
return bucketService.create(bucket)
|
|
|
|
.then(r => {
|
|
|
|
ctx.commit('addBucket', r)
|
|
|
|
return Promise.resolve(r)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2020-05-09 22:08:18 +02:00
|
|
|
.finally(() => {
|
|
|
|
cancel()
|
|
|
|
})
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
2021-09-11 17:53:03 +02:00
|
|
|
|
2021-03-10 11:59:29 +01:00
|
|
|
deleteBucket(ctx, {bucket, params}) {
|
2021-01-09 15:24:06 +01:00
|
|
|
const cancel = setLoading(ctx, 'kanban')
|
2020-05-09 22:08:18 +02:00
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
const bucketService = new BucketService()
|
|
|
|
return bucketService.delete(bucket)
|
|
|
|
.then(r => {
|
|
|
|
ctx.commit('removeBucket', bucket)
|
|
|
|
// We reload all buckets because tasks are being moved from the deleted bucket
|
2021-03-10 11:59:29 +01:00
|
|
|
ctx.dispatch('loadBucketsForList', {listId: bucket.listId, params: params})
|
2020-05-09 19:00:54 +02:00
|
|
|
return Promise.resolve(r)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2020-05-09 22:08:18 +02:00
|
|
|
.finally(() => {
|
|
|
|
cancel()
|
|
|
|
})
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
2021-09-11 17:53:03 +02:00
|
|
|
|
|
|
|
updateBucket(ctx, updatedBucketData) {
|
2021-01-09 15:24:06 +01:00
|
|
|
const cancel = setLoading(ctx, 'kanban')
|
2020-05-09 22:08:18 +02:00
|
|
|
|
2021-09-11 17:53:03 +02:00
|
|
|
const bucketIndex = findIndexById(ctx.state.buckets, updatedBucketData.id)
|
|
|
|
const oldBucket = cloneDeep(ctx.state.buckets[bucketIndex])
|
|
|
|
|
|
|
|
const bucket = ctx.state.buckets[bucketIndex]
|
|
|
|
|
|
|
|
const requestData = {
|
|
|
|
id: updatedBucketData.id,
|
|
|
|
listId: updatedBucketData.listId || oldBucket.listId,
|
|
|
|
title: oldBucket.title, // can't be empty in request
|
|
|
|
// ...bucket,
|
|
|
|
...updatedBucketData,
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatedBucket = {
|
|
|
|
...bucket,
|
|
|
|
...requestData,
|
|
|
|
}
|
|
|
|
ctx.commit('setBucketByIndex', {bucketIndex, bucket: updatedBucket})
|
|
|
|
|
2021-10-11 17:50:53 +02:00
|
|
|
const bucketService = new BucketService()
|
2021-09-11 17:53:03 +02:00
|
|
|
return bucketService.update(updatedBucket)
|
2020-05-09 19:00:54 +02:00
|
|
|
.then(r => {
|
2021-10-11 17:50:53 +02:00
|
|
|
ctx.commit('setBucketByIndex', {bucketIndex, bucket: r})
|
2021-09-11 17:53:03 +02:00
|
|
|
Promise.resolve(r)
|
2020-05-09 19:00:54 +02:00
|
|
|
})
|
|
|
|
.catch(e => {
|
2021-09-11 17:53:03 +02:00
|
|
|
// restore original state
|
|
|
|
ctx.commit('setBucketByIndex', {bucketIndex, bucket: oldBucket})
|
|
|
|
|
2020-05-09 19:00:54 +02:00
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-09-11 17:53:03 +02:00
|
|
|
.finally(() => cancel())
|
|
|
|
},
|
|
|
|
|
|
|
|
updateBucketTitle(ctx, { id, title }) {
|
|
|
|
const bucket = findById(ctx.state.buckets, id)
|
|
|
|
|
|
|
|
if (bucket.title === title) {
|
|
|
|
// bucket title has not changed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatedBucketData = {
|
|
|
|
id,
|
|
|
|
title,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.dispatch('updateBucket', updatedBucketData).then(() => {
|
|
|
|
success({message: i18n.global.t('list.kanban.bucketTitleSavedSuccess')})
|
|
|
|
})
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|