2020-05-09 19:00:54 +02:00
|
|
|
import Vue from 'vue'
|
2021-03-10 11:59:29 +01:00
|
|
|
import {cloneDeep} from 'lodash'
|
2020-05-09 19:00:54 +02:00
|
|
|
|
|
|
|
import BucketService from '../../services/bucket'
|
2020-09-05 22:35:52 +02:00
|
|
|
import {filterObject} from '@/helpers/filterObject'
|
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'
|
|
|
|
|
|
|
|
const tasksPerBucket = 25
|
2020-05-09 19:00:54 +02:00
|
|
|
|
2021-03-24 21:16:56 +01:00
|
|
|
const addTaskToBucketAndSort = (state, task) => {
|
|
|
|
const bi = filterObject(state.buckets, b => b.id === task.bucketId)
|
|
|
|
state.buckets[bi].tasks.push(task)
|
|
|
|
state.buckets[bi].tasks.sort((a, b) => a.position > b.position ? 1 : -1)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
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
|
|
|
}),
|
|
|
|
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
|
|
|
},
|
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 => {
|
|
|
|
Vue.set(state.taskPagesPerBucket, b.id, 1)
|
|
|
|
Vue.set(state.allTasksLoadedForBucket, b.id, false)
|
|
|
|
})
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
|
|
|
addBucket(state, bucket) {
|
|
|
|
state.buckets.push(bucket)
|
|
|
|
},
|
|
|
|
removeBucket(state, bucket) {
|
|
|
|
for (const b in state.buckets) {
|
|
|
|
if (state.buckets[b].id === bucket.id) {
|
|
|
|
state.buckets.splice(b, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setBucketById(state, bucket) {
|
|
|
|
for (const b in state.buckets) {
|
|
|
|
if (state.buckets[b].id === bucket.id) {
|
|
|
|
Vue.set(state.buckets, b, bucket)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setBucketByIndex(state, {bucketIndex, bucket}) {
|
|
|
|
Vue.set(state.buckets, bucketIndex, bucket)
|
|
|
|
},
|
|
|
|
setTaskInBucketByIndex(state, {bucketIndex, taskIndex, task}) {
|
|
|
|
const bucket = state.buckets[bucketIndex]
|
|
|
|
bucket.tasks[taskIndex] = task
|
|
|
|
Vue.set(state.buckets, bucketIndex, bucket)
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
Vue.set(state.buckets, b, bucket)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTaskToBucket(state, task) {
|
|
|
|
const bi = filterObject(state.buckets, b => b.id === task.bucketId)
|
|
|
|
state.buckets[bi].tasks.push(task)
|
|
|
|
},
|
2021-03-10 11:59:29 +01:00
|
|
|
addTasksToBucket(state, {tasks, bucketId}) {
|
|
|
|
const bi = filterObject(state.buckets, b => b.id === bucketId)
|
|
|
|
|
|
|
|
tasks.forEach(t => {
|
|
|
|
state.buckets[bi].tasks.push(t)
|
|
|
|
})
|
|
|
|
},
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
Vue.set(state.buckets, b, bucket)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-09-05 22:35:52 +02:00
|
|
|
},
|
2021-03-10 11:59:29 +01:00
|
|
|
setBucketLoading(state, {bucketId, loading}) {
|
|
|
|
Vue.set(state.bucketLoading, bucketId, loading)
|
|
|
|
},
|
|
|
|
setTasksLoadedForBucketPage(state, {bucketId, page}) {
|
|
|
|
Vue.set(state.taskPagesPerBucket, bucketId, page)
|
|
|
|
},
|
|
|
|
setAllTasksLoadedForBucket(state, bucketId) {
|
|
|
|
Vue.set(state.allTasksLoadedForBucket, bucketId, true)
|
|
|
|
},
|
2020-05-09 19:00:54 +02:00
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
getTaskById: state => id => {
|
|
|
|
for (const b in state.buckets) {
|
|
|
|
for (const t in state.buckets[b].tasks) {
|
|
|
|
if (state.buckets[b].tasks[t].id === id) {
|
|
|
|
return {
|
|
|
|
bucketIndex: b,
|
|
|
|
taskIndex: t,
|
|
|
|
task: state.buckets[b].tasks[t],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
bucketIndex: null,
|
|
|
|
taskIndex: null,
|
|
|
|
task: null,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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-03-10 11:59:29 +01:00
|
|
|
params.per_page = tasksPerBucket
|
|
|
|
|
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-03-10 11:59:29 +01:00
|
|
|
loadNextTasksForBucket(ctx, {listId, ps = {}, bucketId}) {
|
|
|
|
const isLoading = ctx.state.bucketLoading[bucketId] ?? false
|
|
|
|
if (isLoading) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
const page = (ctx.state.taskPagesPerBucket[bucketId] ?? 1) + 1
|
|
|
|
|
|
|
|
const alreadyLoaded = ctx.state.allTasksLoadedForBucket[bucketId] ?? false
|
|
|
|
if (alreadyLoaded) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
const cancel = setLoading(ctx, 'kanban')
|
|
|
|
ctx.commit('setBucketLoading', {bucketId: bucketId, loading: true})
|
|
|
|
|
|
|
|
const params = cloneDeep(ps)
|
|
|
|
|
|
|
|
params.sort_by = 'position'
|
|
|
|
params.order_by = 'asc'
|
|
|
|
|
|
|
|
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']
|
|
|
|
}
|
|
|
|
|
|
|
|
params.per_page = tasksPerBucket
|
|
|
|
|
|
|
|
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})
|
|
|
|
})
|
|
|
|
},
|
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-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
|
|
|
},
|
|
|
|
updateBucket(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.update(bucket)
|
|
|
|
.then(r => {
|
|
|
|
const bi = filterObject(ctx.state.buckets, b => b.id === r.id)
|
|
|
|
const bucket = r
|
|
|
|
bucket.tasks = ctx.state.buckets[bi].tasks
|
|
|
|
ctx.commit('setBucketByIndex', {bucketIndex: bi, bucket})
|
|
|
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|