feat: tasks store with composition api
All checks were successful
continuous-integration/drone/pr Build is passing
All checks were successful
continuous-integration/drone/pr Build is passing
This commit is contained in:
parent
ea1c7f1a7e
commit
2d3dc6ae8d
|
@ -1,3 +1,4 @@
|
||||||
|
import {computed, ref} from 'vue'
|
||||||
import {defineStore, acceptHMRUpdate} from 'pinia'
|
import {defineStore, acceptHMRUpdate} from 'pinia'
|
||||||
import router from '@/router'
|
import router from '@/router'
|
||||||
import {formatISO} from 'date-fns'
|
import {formatISO} from 'date-fns'
|
||||||
|
@ -78,48 +79,48 @@ async function findAssignees(parsedTaskAssignees: string[]) {
|
||||||
return validatedUsers.filter((item) => Boolean(item))
|
return validatedUsers.filter((item) => Boolean(item))
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TaskState {
|
export const useTaskStore = defineStore('task', () => {
|
||||||
tasks: { [id: ITask['id']]: ITask }
|
const baseStore = useBaseStore()
|
||||||
isLoading: boolean,
|
const kanbanStore = useKanbanStore()
|
||||||
}
|
const attachmentStore = useAttachmentStore()
|
||||||
|
const labelStore = useLabelStore()
|
||||||
|
const listStore = useListStore()
|
||||||
|
|
||||||
export const useTaskStore = defineStore('task', {
|
const tasks = ref<{ [id: ITask['id']]: ITask }>({}) // TODO: or is this ITask[]
|
||||||
state: () : TaskState => ({
|
const isLoading = ref(false)
|
||||||
tasks: {},
|
|
||||||
isLoading: false,
|
const hasTasks = computed(() => Object.keys(tasks.value).length > 0)
|
||||||
}),
|
|
||||||
getters: {
|
function setIsLoading(newIsLoading: boolean) {
|
||||||
hasTasks(state) {
|
isLoading.value = newIsLoading
|
||||||
return Object.keys(state.tasks).length > 0
|
}
|
||||||
},
|
|
||||||
},
|
function setTasks(newTasks: ITask[]) {
|
||||||
actions: {
|
newTasks.forEach(task => {
|
||||||
setTasks(tasks: ITask[]) {
|
tasks.value[task.id] = task
|
||||||
tasks.forEach(task => {
|
|
||||||
this.tasks[task.id] = task
|
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
|
|
||||||
async loadTasks(params) {
|
async function loadTasks(params) {
|
||||||
const taskService = new TaskService()
|
const taskService = new TaskService()
|
||||||
|
|
||||||
const cancel = setModuleLoading(this)
|
const cancel = setModuleLoading(this, setIsLoading)
|
||||||
try {
|
try {
|
||||||
this.tasks = await taskService.getAll({}, params)
|
tasks.value = await taskService.getAll({}, params)
|
||||||
useBaseStore().setHasTasks(this.tasks.length > 0)
|
baseStore.setHasTasks(tasks.value.length > 0)
|
||||||
return this.tasks
|
return tasks.value
|
||||||
} finally {
|
} finally {
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
async update(task: ITask) {
|
async function update(task: ITask) {
|
||||||
const cancel = setModuleLoading(this)
|
const cancel = setModuleLoading(this, setIsLoading)
|
||||||
|
|
||||||
const taskService = new TaskService()
|
const taskService = new TaskService()
|
||||||
try {
|
try {
|
||||||
const updatedTask = await taskService.update(task)
|
const updatedTask = await taskService.update(task)
|
||||||
useKanbanStore().setTaskInBucket(updatedTask)
|
kanbanStore.setTaskInBucket(updatedTask)
|
||||||
if (task.done) {
|
if (task.done) {
|
||||||
playPop()
|
playPop()
|
||||||
}
|
}
|
||||||
|
@ -127,25 +128,24 @@ export const useTaskStore = defineStore('task', {
|
||||||
} finally {
|
} finally {
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
async delete(task: ITask) {
|
async function deleteTask(task: ITask) {
|
||||||
const taskService = new TaskService()
|
const taskService = new TaskService()
|
||||||
const response = await taskService.delete(task)
|
const response = await taskService.delete(task)
|
||||||
useKanbanStore().removeTaskInBucket(task)
|
kanbanStore.removeTaskInBucket(task)
|
||||||
return response
|
return response
|
||||||
},
|
}
|
||||||
|
|
||||||
// Adds a task attachment in store.
|
// Adds a task attachment in store.
|
||||||
// This is an action to be able to commit other mutations
|
// This is an action to be able to commit other mutations
|
||||||
addTaskAttachment({
|
function addTaskAttachment({
|
||||||
taskId,
|
taskId,
|
||||||
attachment,
|
attachment,
|
||||||
}: {
|
}: {
|
||||||
taskId: ITask['id']
|
taskId: ITask['id']
|
||||||
attachment: IAttachment
|
attachment: IAttachment
|
||||||
}) {
|
}) {
|
||||||
const kanbanStore = useKanbanStore()
|
|
||||||
const t = kanbanStore.getTaskById(taskId)
|
const t = kanbanStore.getTaskById(taskId)
|
||||||
if (t.task !== null) {
|
if (t.task !== null) {
|
||||||
const attachments = [
|
const attachments = [
|
||||||
|
@ -162,21 +162,19 @@ export const useTaskStore = defineStore('task', {
|
||||||
}
|
}
|
||||||
kanbanStore.setTaskInBucketByIndex(newTask)
|
kanbanStore.setTaskInBucketByIndex(newTask)
|
||||||
}
|
}
|
||||||
const attachmentStore = useAttachmentStore()
|
|
||||||
attachmentStore.add(attachment)
|
attachmentStore.add(attachment)
|
||||||
},
|
}
|
||||||
|
|
||||||
async addAssignee({
|
async function addAssignee({
|
||||||
user,
|
user,
|
||||||
taskId,
|
taskId,
|
||||||
}: {
|
}: {
|
||||||
user: IUser,
|
user: IUser,
|
||||||
taskId: ITask['id']
|
taskId: ITask['id']
|
||||||
}) {
|
}) {
|
||||||
const cancel = setModuleLoading(this)
|
const cancel = setModuleLoading(this, setIsLoading)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const kanbanStore = useKanbanStore()
|
|
||||||
const taskAssigneeService = new TaskAssigneeService()
|
const taskAssigneeService = new TaskAssigneeService()
|
||||||
const r = await taskAssigneeService.create(new TaskAssigneeModel({
|
const r = await taskAssigneeService.create(new TaskAssigneeModel({
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
|
@ -206,16 +204,15 @@ export const useTaskStore = defineStore('task', {
|
||||||
} finally {
|
} finally {
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
async removeAssignee({
|
async function removeAssignee({
|
||||||
user,
|
user,
|
||||||
taskId,
|
taskId,
|
||||||
}: {
|
}: {
|
||||||
user: IUser,
|
user: IUser,
|
||||||
taskId: ITask['id']
|
taskId: ITask['id']
|
||||||
}) {
|
}) {
|
||||||
const kanbanStore = useKanbanStore()
|
|
||||||
const taskAssigneeService = new TaskAssigneeService()
|
const taskAssigneeService = new TaskAssigneeService()
|
||||||
const response = await taskAssigneeService.delete(new TaskAssigneeModel({
|
const response = await taskAssigneeService.delete(new TaskAssigneeModel({
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
|
@ -241,16 +238,15 @@ export const useTaskStore = defineStore('task', {
|
||||||
})
|
})
|
||||||
return response
|
return response
|
||||||
|
|
||||||
},
|
}
|
||||||
|
|
||||||
async addLabel({
|
async function addLabel({
|
||||||
label,
|
label,
|
||||||
taskId,
|
taskId,
|
||||||
} : {
|
} : {
|
||||||
label: ILabel,
|
label: ILabel,
|
||||||
taskId: ITask['id']
|
taskId: ITask['id']
|
||||||
}) {
|
}) {
|
||||||
const kanbanStore = useKanbanStore()
|
|
||||||
const labelTaskService = new LabelTaskService()
|
const labelTaskService = new LabelTaskService()
|
||||||
const r = await labelTaskService.create(new LabelTaskModel({
|
const r = await labelTaskService.create(new LabelTaskModel({
|
||||||
taskId,
|
taskId,
|
||||||
|
@ -277,13 +273,12 @@ export const useTaskStore = defineStore('task', {
|
||||||
})
|
})
|
||||||
|
|
||||||
return r
|
return r
|
||||||
},
|
}
|
||||||
|
|
||||||
async removeLabel(
|
async function removeLabel(
|
||||||
{label, taskId}:
|
{label, taskId}:
|
||||||
{label: ILabel, taskId: ITask['id']},
|
{label: ILabel, taskId: ITask['id']},
|
||||||
) {
|
) {
|
||||||
const kanbanStore = useKanbanStore()
|
|
||||||
const labelTaskService = new LabelTaskService()
|
const labelTaskService = new LabelTaskService()
|
||||||
const response = await labelTaskService.delete(new LabelTaskModel({
|
const response = await labelTaskService.delete(new LabelTaskModel({
|
||||||
taskId, labelId:
|
taskId, labelId:
|
||||||
|
@ -310,10 +305,10 @@ export const useTaskStore = defineStore('task', {
|
||||||
})
|
})
|
||||||
|
|
||||||
return response
|
return response
|
||||||
},
|
}
|
||||||
|
|
||||||
// Do everything that is involved in finding, creating and adding the label to the task
|
// Do everything that is involved in finding, creating and adding the label to the task
|
||||||
async addLabelsToTask(
|
async function addLabelsToTask(
|
||||||
{ task, parsedLabels }:
|
{ task, parsedLabels }:
|
||||||
{ task: ITask, parsedLabels: string[] },
|
{ task: ITask, parsedLabels: string[] },
|
||||||
) {
|
) {
|
||||||
|
@ -321,8 +316,6 @@ export const useTaskStore = defineStore('task', {
|
||||||
return task
|
return task
|
||||||
}
|
}
|
||||||
|
|
||||||
const labelStore = useLabelStore()
|
|
||||||
|
|
||||||
const labelAddsToWaitFor = parsedLabels.map(async labelTitle => {
|
const labelAddsToWaitFor = parsedLabels.map(async labelTitle => {
|
||||||
let label = validateLabel(Object.values(labelStore.labels), labelTitle)
|
let label = validateLabel(Object.values(labelStore.labels), labelTitle)
|
||||||
if (typeof label === 'undefined') {
|
if (typeof label === 'undefined') {
|
||||||
|
@ -337,9 +330,9 @@ export const useTaskStore = defineStore('task', {
|
||||||
// This waits until all labels are created and added to the task
|
// This waits until all labels are created and added to the task
|
||||||
await Promise.all(labelAddsToWaitFor)
|
await Promise.all(labelAddsToWaitFor)
|
||||||
return task
|
return task
|
||||||
},
|
}
|
||||||
|
|
||||||
findListId(
|
function findListId(
|
||||||
{ list: listName, listId }:
|
{ list: listName, listId }:
|
||||||
{ list: string, listId: IList['id'] }) {
|
{ list: string, listId: IList['id'] }) {
|
||||||
let foundListId = null
|
let foundListId = null
|
||||||
|
@ -347,7 +340,6 @@ export const useTaskStore = defineStore('task', {
|
||||||
// Uses the following ways to get the list id of the new task:
|
// Uses the following ways to get the list id of the new task:
|
||||||
// 1. If specified in quick add magic, look in store if it exists and use it if it does
|
// 1. If specified in quick add magic, look in store if it exists and use it if it does
|
||||||
if (listName !== null) {
|
if (listName !== null) {
|
||||||
const listStore = useListStore()
|
|
||||||
const list = listStore.findListByExactname(listName)
|
const list = listStore.findListByExactname(listName)
|
||||||
foundListId = list === null ? null : list.id
|
foundListId = list === null ? null : list.id
|
||||||
}
|
}
|
||||||
|
@ -368,9 +360,9 @@ export const useTaskStore = defineStore('task', {
|
||||||
}
|
}
|
||||||
|
|
||||||
return foundListId
|
return foundListId
|
||||||
},
|
}
|
||||||
|
|
||||||
async createNewTask({
|
async function createNewTask({
|
||||||
title,
|
title,
|
||||||
bucketId,
|
bucketId,
|
||||||
listId,
|
listId,
|
||||||
|
@ -378,10 +370,10 @@ export const useTaskStore = defineStore('task', {
|
||||||
} :
|
} :
|
||||||
Partial<ITask>,
|
Partial<ITask>,
|
||||||
) {
|
) {
|
||||||
const cancel = setModuleLoading(this)
|
const cancel = setModuleLoading(this, setIsLoading)
|
||||||
const parsedTask = parseTaskText(title, getQuickAddMagicMode())
|
const parsedTask = parseTaskText(title, getQuickAddMagicMode())
|
||||||
|
|
||||||
const foundListId = await this.findListId({
|
const foundListId = await findListId({
|
||||||
list: parsedTask.list,
|
list: parsedTask.list,
|
||||||
listId: listId || 0,
|
listId: listId || 0,
|
||||||
})
|
})
|
||||||
|
@ -410,7 +402,7 @@ export const useTaskStore = defineStore('task', {
|
||||||
const taskService = new TaskService()
|
const taskService = new TaskService()
|
||||||
try {
|
try {
|
||||||
const createdTask = await taskService.create(task)
|
const createdTask = await taskService.create(task)
|
||||||
const result = await this.addLabelsToTask({
|
const result = await addLabelsToTask({
|
||||||
task: createdTask,
|
task: createdTask,
|
||||||
parsedLabels: parsedTask.labels,
|
parsedLabels: parsedTask.labels,
|
||||||
})
|
})
|
||||||
|
@ -418,15 +410,34 @@ export const useTaskStore = defineStore('task', {
|
||||||
} finally {
|
} finally {
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
async setCoverImage(task: ITask, attachment: IAttachment | null) {
|
async function setCoverImage(task: ITask, attachment: IAttachment | null) {
|
||||||
return this.update({
|
return update({
|
||||||
...task,
|
...task,
|
||||||
coverImageAttachmentId: attachment ? attachment.id : 0,
|
coverImageAttachmentId: attachment ? attachment.id : 0,
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
},
|
|
||||||
|
return {
|
||||||
|
tasks,
|
||||||
|
isLoading,
|
||||||
|
|
||||||
|
hasTasks,
|
||||||
|
|
||||||
|
setTasks,
|
||||||
|
loadTasks,
|
||||||
|
update,
|
||||||
|
delete: deleteTask, // since delete is a reserved word we have to alias here
|
||||||
|
addTaskAttachment,
|
||||||
|
addAssignee,
|
||||||
|
removeAssignee,
|
||||||
|
addLabel,
|
||||||
|
removeLabel,
|
||||||
|
addLabelsToTask,
|
||||||
|
createNewTask,
|
||||||
|
setCoverImage,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// support hot reloading
|
// support hot reloading
|
||||||
|
|
Reference in New Issue
Block a user