vikunja-frontend/src/store/modules/kanban.js

352 lines
8.5 KiB
JavaScript
Raw Normal View History

2021-07-25 16:02:49 +02:00
import cloneDeep from 'lodash/cloneDeep'
2021-09-07 15:42:41 +02:00
import {findById, findIndexById} from '@/helpers/find'
import BucketService from '../../services/bucket'
2020-05-09 22:08:18 +02:00
import {setLoading} from '../helper'
import TaskCollectionService from '@/services/taskCollection'
const TASKS_PER_BUCKET = 25
2021-09-07 15:42:41 +02:00
function getTaskPosition(state, task) {
const bucketIndex = findIndexById(state.buckets, task.bucketId)
if (!bucketIndex) {
throw('Bucket not found')
}
const bucket = state.buckets[bucketIndex]
const taskIndex = findIndexById(bucket.tasks, task.id)
if (!bucketIndex) {
throw('Task not found')
}
return {
bucketIndex,
taskIndex,
}
}
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)
}
/**
* 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
state: () => ({
buckets: [],
listId: 0,
bucketLoading: {},
taskPagesPerBucket: {},
allTasksLoadedForBucket: {},
}),
2021-09-07 15:42:41 +02:00
mutations: {
setListId(state, listId) {
state.listId = parseInt(listId)
},
2021-09-07 15:42:41 +02:00
setBuckets(state, buckets) {
state.buckets = buckets
buckets.forEach(b => {
state.taskPagesPerBucket[b.id] = 1
state.allTasksLoadedForBucket[b.id] = false
})
},
2021-09-07 15:42:41 +02:00
addBucket(state, bucket) {
state.buckets.push(bucket)
},
2021-09-07 15:42:41 +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)
},
2021-09-07 15:42:41 +02:00
setBucketById(state, bucket) {
2021-09-07 15:42:41 +02:00
const bucketIndex = findIndexById(state.buckets, bucket.id)
state.buckets[bucketIndex] = bucket
},
2021-09-07 15:42:41 +02:00
setBucketByIndex(state, {bucketIndex, bucket}) {
state.buckets[bucketIndex] = bucket
},
2021-09-07 15:42:41 +02:00
setTaskInBucketByIndex(state, {bucketIndex, taskIndex, task}) {
const bucket = state.buckets[bucketIndex]
bucket.tasks[taskIndex] = task
state.buckets[bucketIndex] = bucket
},
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,
}
},
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
}
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)
}
state.buckets[b] = bucket
found = true
return
}
}
}
for (const b in state.buckets) {
if (state.buckets[b].id === task.bucketId) {
findAndUpdate(b)
if (found) {
return
}
}
}
for (const b in state.buckets) {
findAndUpdate(b)
if (found) {
return
}
}
},
2021-09-07 15:42:41 +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
},
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,
tasks,
],
}
state.buckets[bucketIndex] = newBucket
},
2021-09-07 15:42:41 +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-09-07 15:42:41 +02:00
const { bucketIndex, taskIndex } = getTaskPosition(state, task)
state.buckets[bucketIndex].tasks.splice(taskIndex, 1)
},
2021-09-07 15:42:41 +02:00
setBucketLoading(state, {bucketId, loading}) {
state.bucketLoading[bucketId] = loading
},
2021-09-07 15:42:41 +02:00
setTasksLoadedForBucketPage(state, {bucketId, page}) {
state.taskPagesPerBucket[bucketId] = page
},
2021-09-07 15:42:41 +02:00
setAllTasksLoadedForBucket(state, bucketId) {
state.allTasksLoadedForBucket[bucketId] = true
},
},
2021-09-07 15:42:41 +02:00
getters: {
2021-09-07 15:42:41 +02:00
getBucketById(state) {
return (bucketId) => findById(state.buckets, bucketId)
},
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,
}
},
},
2021-09-07 15:42:41 +02:00
actions: {
2020-12-22 12:49:34 +01:00
loadBucketsForList(ctx, {listId, params}) {
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', [])
params.per_page = TASKS_PER_BUCKET
const bucketService = new BucketService()
2020-12-22 12:49:34 +01:00
return bucketService.getAll({listId: listId}, params)
.then(r => {
ctx.commit('setBuckets', r)
ctx.commit('setListId', listId)
return Promise.resolve(r)
})
.catch(e => {
return Promise.reject(e)
})
2020-05-09 22:08:18 +02:00
.finally(() => {
cancel()
})
},
2021-09-07 15:42:41 +02: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
if (isLoading) {
return Promise.resolve()
}
2021-09-07 15:42:41 +02:00
const page = (ctx.state.taskPagesPerBucket[bucketIndex] ?? 1) + 1
2021-09-07 15:42:41 +02:00
const alreadyLoaded = ctx.state.allTasksLoadedForBucket[bucketIndex] ?? 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 = 'kanban_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 = TASKS_PER_BUCKET
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})
})
},
createBucket(ctx, bucket) {
const cancel = setLoading(ctx, 'kanban')
2020-05-09 22:08:18 +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()
})
},
deleteBucket(ctx, {bucket, params}) {
const cancel = setLoading(ctx, 'kanban')
2020-05-09 22:08:18 +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
ctx.dispatch('loadBucketsForList', {listId: bucket.listId, params: params})
return Promise.resolve(r)
})
.catch(e => {
return Promise.reject(e)
})
2020-05-09 22:08:18 +02:00
.finally(() => {
cancel()
})
},
updateBucket(ctx, bucket) {
const cancel = setLoading(ctx, 'kanban')
2020-05-09 22:08:18 +02:00
const bucketService = new BucketService()
return bucketService.update(bucket)
.then(r => {
2021-09-07 15:42:41 +02:00
const bi = findById(ctx.state.buckets, 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()
})
},
},
}