Use kanbanPosition or position, whatever makes sense

This commit is contained in:
kolaente 2021-07-27 17:57:42 +02:00
parent 39ef4b48f2
commit 7434640525
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
6 changed files with 55 additions and 19 deletions

View File

@ -36,14 +36,17 @@ export default {
const list = this.$store.getters['lists/findListByExactname'](parsedTask.list)
listId = list === null ? null : list.id
}
if (listId === null) {
listId = lId !== 0 ? lId : this.$route.params.listId
if (lId !== 0) {
listId = lId
}
if (typeof this.$route.params.listId !== 'undefined') {
listId = parseInt(this.$route.params.listId)
}
if (typeof listId === 'undefined' || listId === 0) {
if (typeof listId === 'undefined' || listId === null) {
return Promise.reject('NO_LIST')
}
// Separate closure because we need to wait for the results of the user search if users were entered in the
// task create request. Because _that_ happens in a promise, we'll need something to call when it resolves.
const createTask = () => {

View File

@ -0,0 +1,18 @@
import {calculateTaskPosition} from './calculateTaskPosition'
it('should calculate the task position', () => {
const result = calculateTaskPosition(10, 100)
expect(result).toBe(55)
})
it('should return 0 if no position was provided', () => {
const result = calculateTaskPosition(null, null)
expect(result).toBe(0)
})
it('should calculate the task position for the first task', () => {
const result = calculateTaskPosition(null, 100)
expect(result).toBe(50)
})
it('should calculate the task position for the last task', () => {
const result = calculateTaskPosition(10, null)
expect(result).toBe(65546)
})

View File

@ -0,0 +1,21 @@
export const calculateTaskPosition = (positionBefore: number | null, positionAfter: number | null): number => {
console.log('calculate', positionBefore, positionAfter)
if (positionBefore === null && positionAfter === null) {
return 0
}
// If there is no task before, our task is the first task in which case we let it have half of the position of the task after it
if (positionBefore === null && positionAfter !== null) {
return positionAfter / 2
}
// If there is no task after it, we just add 2^16 to the last position to have enough room in the future
if (positionBefore !== null && positionAfter === null) {
return positionBefore + Math.pow(2, 16)
}
// If we have both a task before and after it, we acually calculate the position
// @ts-ignore - can never be null but TS does not seem to understand that
return positionBefore + (positionAfter - positionBefore) / 2
}

View File

@ -104,6 +104,9 @@ export default class TaskModel extends AbstractModel {
index: 0,
isFavorite: false,
subscription: null,
position: 0,
kanbanPosition: 0,
createdBy: UserModel,
created: null,

View File

@ -11,7 +11,7 @@ const tasksPerBucket = 25
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)
state.buckets[bi].tasks.sort((a, b) => a.kanbanPosition > b.kanbanPosition ? 1 : -1)
}
/**
@ -208,7 +208,7 @@ export default {
const params = cloneDeep(ps)
params.sort_by = 'position'
params.sort_by = 'kanban_position'
params.order_by = 'asc'
let hasBucketFilter = false

View File

@ -295,6 +295,7 @@ import Dropdown from '@/components/misc/dropdown.vue'
import {playPop} from '@/helpers/playPop'
import createTask from '@/components/tasks/mixins/createTask'
import {getCollapsedBucketState, saveCollapsedBucketState} from '@/helpers/saveCollapsedBucketState'
import {calculateTaskPosition} from '../../../helpers/calculateTaskPosition'
export default {
name: 'Kanban',
@ -447,19 +448,9 @@ export default {
this.$set(this.taskUpdating, task.id, true)
this.oneTaskUpdating = true
// If there is no task before, our task is the first task in which case we let it have half of the position of the task after it
if (taskBefore === null && taskAfter !== null) {
task.position = taskAfter.position / 2
}
// If there is no task after it, we just add 2^16 to the last position
if (taskBefore !== null && taskAfter === null) {
task.position = taskBefore.position + Math.pow(2, 16)
}
// If we have both a task before and after it, we acually calculate the position
if (taskAfter !== null && taskBefore !== null) {
task.position = taskBefore.position + (taskAfter.position - taskBefore.position) / 2
}
task.kanbanPosition = calculateTaskPosition(taskBefore !== null ? taskBefore.kanbanPosition : null, taskAfter !== null ? taskAfter.kanbanPosition : null)
console.log(task.kanbanPosition)
task.bucketId = bucketId
this.$store.dispatch('tasks/update', task)