buckets and some more

This commit is contained in:
WofWca 2023-03-23 17:19:23 +04:00
parent f4890f00d7
commit 90ef2b13a9
5 changed files with 68 additions and 8 deletions

View File

@ -1,5 +1,5 @@
import type { IBucket } from '@/modelTypes/IBucket'
import { getTasksOfBucket } from './tasks'
import { getAllTasks, getTasksOfBucket } from './tasks'
import { defaultPositionIfZero } from './utils/calculateDefaultPosition'
// TODO_OFFLINE there is a lot of duplication between `localBackend` parts.
@ -110,10 +110,16 @@ export function getDefaultBucket(projectId: number): IBucket {
*/
export function createBucket(bucket: IBucket): IBucket {
const allBuckets = getAllBucketsWithoutTasks()
const maxPosition = allBuckets.reduce((currMax, b) => {
return b.position > currMax
? b.position
: currMax
}, -Infinity)
const newBucketFullData = {
...bucket,
id: Math.round(Math.random() * 1000000000000),
position: defaultPositionIfZero(bucket.position),
// position: defaultPositionIfZero(bucket.position),
position: maxPosition * 2,
}
const newBucketFullDataToStore = {
...newBucketFullData,
@ -126,13 +132,51 @@ export function createBucket(bucket: IBucket): IBucket {
}
export function updateBucket(newBucketData: IBucket) {
const allBuckets = getAllBuckets()
const allBuckets = getAllBucketsWithoutTasks()
// TODO_OFFLINE looks like the real backend also filters by prjectId, but
// since in localBackend all bucket `id`s are unique even between projects,
// it's not necessary
const targetBucketInd = allBuckets.findIndex(t => t.id === newBucketData.id)
const targetBucketInd = allBuckets.findIndex(b => b.id === newBucketData.id)
if (targetBucketInd < 0) {
console.warn('Tried to update a bucket, but it does not exist')
return
}
// TODO_OFFLINE remove tasks.
allBuckets.splice(targetBucketInd, 1, newBucketData)
localStorage.setItem('buckets', JSON.stringify(allBuckets))
return newBucketData
}
export function deleteBucket({ id }: { id: number }) {
const allBuckets = getAllBucketsWithoutTasks()
if (allBuckets.length <= 1) {
// Prevent removing the last bucket.
// https://kolaente.dev/vikunja/api/src/commit/6aadaaaffc1fff4a94e35e8fa3f6eab397cbc3ce/pkg/models/kanban.go#L325-L335
return
}
const targetBucketInd = allBuckets.findIndex(b => b.id === id)
if (targetBucketInd < 0) {
console.warn('Tried to delete a bucket, but it does not exist')
return
}
const projectId = allBuckets[targetBucketInd].projectId
allBuckets.splice(targetBucketInd, 1)
localStorage.setItem('buckets', JSON.stringify(allBuckets))
// Move all the tasks from this bucket to the default one.
// https://kolaente.dev/vikunja/api/src/commit/6aadaaaffc1fff4a94e35e8fa3f6eab397cbc3ce/pkg/models/kanban.go#L349-L353
const deletedBuckedId = id
const defaultBucketId = getDefaultBucket(projectId).id
const allTasks = getAllTasks()
allTasks.forEach(t => {
if (t.bucketId === deletedBuckedId) {
t.bucketId = defaultBucketId
}
})
localStorage.setItem('tasks', JSON.stringify(allTasks))
// TODO_OFFLINE idk what it's supposed to return
return true
}

View File

@ -76,16 +76,27 @@ export function getTask(taskId: number) {
}
export function updateTask(newTaskData: ITask) {
// TODO_OFFLINE a lot of stuff is not implemented. For example, marking a task "done"
// when it is moved to the "done" bucket.
// https://kolaente.dev/vikunja/api/src/commit/6aadaaaffc1fff4a94e35e8fa3f6eab397cbc3ce/pkg/models/tasks.go#L1008
const allTasks = getAllTasks()
const targetTaskInd = allTasks.findIndex(t => t.id === newTaskData.id)
if (targetTaskInd < 0) {
console.warn('Tried to update a task, but it does not exist')
return
}
allTasks.splice(targetTaskInd, 1, newTaskData)
localStorage.setItem('tasks', JSON.stringify(allTasks))
return newTaskData
}
export function deleteTask(taskId: number) {
export function deleteTask({ id }: { id: number }) {
const allTasks = getAllTasks()
const targetTaskInd = allTasks.findIndex(t => t.id === taskId)
const targetTaskInd = allTasks.findIndex(t => t.id === id)
if (targetTaskInd < 0) {
console.warn('Tried to delete a task, but it does not exist')
return
}
allTasks.splice(targetTaskInd, 1)
localStorage.setItem('tasks', JSON.stringify(allTasks))

View File

@ -1,4 +1,8 @@
// https://kolaente.dev/vikunja/api/src/commit/066c26f83e1e066bdc9d80b4642db1df0d6a77eb/pkg/models/tasks.go#L874-L880
// TODO_OFFLINE this is wrong. For example, for buckets. it is possible to create several buckets
// with position: 65535, which would mess up positioning. Looks like we actually need to consider
// position. How about store `lastUsedGlobalId` in `localStorage` and simply increment it each
// time an entity is created?
export function defaultPositionIfZero(/* entityID: number, */ position: number): number {
if (position === 0) {
return /* entityID * */ 2**16

View File

@ -2,7 +2,7 @@ import AbstractService from './abstractService'
import BucketModel from '../models/bucket'
import TaskService from '@/services/task'
import type { IBucket } from '@/modelTypes/IBucket'
import { createBucket, getAllBucketsOfProject, updateBucket } from '@/localBackend/buckets'
import { createBucket, deleteBucket, getAllBucketsOfProject, updateBucket } from '@/localBackend/buckets'
export default class BucketService extends AbstractService<IBucket> {
constructor() {
@ -17,6 +17,7 @@ export default class BucketService extends AbstractService<IBucket> {
_getAll = ({ projectId }: { projectId: number }) => getAllBucketsOfProject(projectId)
_create = (bucket: IBucket) => createBucket(bucket)
_update = (bucket: IBucket) => updateBucket(bucket)
_delete = (bucket: IBucket) => deleteBucket(bucket)
modelFactory(data: Partial<IBucket>) {
return new BucketModel(data)

View File

@ -31,7 +31,7 @@ export default class TaskService extends AbstractService<ITask> {
_get = (model: ITask) => getTask(model.id)
_create = (taskData: ITask) => createTask(taskData)
_update = (model: ITask) => updateTask(model)
_delete = (model: ITask) => deleteTask(model.id)
_delete = (model: ITask) => deleteTask(model)
modelFactory(data) {
return new TaskModel(data)