2020-05-09 19:00:54 +02:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
|
|
|
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'
|
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,
|
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
|
|
|
|
},
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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[t] = task
|
|
|
|
Vue.set(state.buckets, b, bucket)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTaskToBucket(state, task) {
|
|
|
|
const bi = filterObject(state.buckets, b => b.id === task.bucketId)
|
|
|
|
state.buckets[bi].tasks.push(task)
|
|
|
|
},
|
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
|
|
|
},
|
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', [])
|
|
|
|
|
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
|
|
|
},
|
|
|
|
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
|
|
|
},
|
|
|
|
deleteBucket(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.delete(bucket)
|
|
|
|
.then(r => {
|
|
|
|
ctx.commit('removeBucket', bucket)
|
|
|
|
// We reload all buckets because tasks are being moved from the deleted bucket
|
2020-12-28 23:42:09 +01:00
|
|
|
ctx.dispatch('loadBucketsForList', {listId: bucket.listId})
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|