Task module

This commit is contained in:
Marek Lenczewski
2026-04-12 10:06:17 +02:00
parent efe0cfe361
commit 27b34eb90f
39 changed files with 2454 additions and 41 deletions

View File

@@ -0,0 +1,77 @@
import { defineStore } from 'pinia'
import { taskApi } from '../services/api'
export const useTasksStore = defineStore('tasks', {
state: () => ({
tasks: [],
currentTasks: [],
availableStatuses: [],
loading: false,
error: null,
}),
actions: {
async fetchAll() {
this.loading = true
this.error = null
try {
this.tasks = await taskApi.list()
} catch (e) {
this.error = e.message
} finally {
this.loading = false
}
},
async fetchCurrent() {
this.loading = true
this.error = null
try {
this.currentTasks = await taskApi.list('current')
} catch (e) {
this.error = e.message
} finally {
this.loading = false
}
},
async get(id) {
return taskApi.get(id)
},
async fetchStatuses() {
if (this.availableStatuses.length > 0) return
this.availableStatuses = await taskApi.statuses()
},
async create(data) {
const task = await taskApi.create(data)
this.tasks.push(task)
return task
},
async update(id, data) {
const updated = await taskApi.update(id, data)
this.replaceLocal(updated)
return updated
},
async remove(id) {
await taskApi.remove(id)
this.tasks = this.tasks.filter((t) => t.id !== id)
this.currentTasks = this.currentTasks.filter((t) => t.id !== id)
},
async toggle(id) {
const updated = await taskApi.toggle(id)
this.replaceLocal(updated)
return updated
},
replaceLocal(task) {
const replace = (list) => list.map((t) => (t.id === task.id ? task : t))
this.tasks = replace(this.tasks)
this.currentTasks = replace(this.currentTasks)
},
},
})