2022-09-06 09:36:01 +00:00
|
|
|
import AttachmentModel from '@/models/attachment'
|
|
|
|
import type {IAttachment} from '@/modelTypes/IAttachment'
|
2021-09-24 18:16:37 +00:00
|
|
|
|
|
|
|
import AttachmentService from '@/services/attachment'
|
2022-09-23 10:55:53 +00:00
|
|
|
import {useTaskStore} from '@/stores/tasks'
|
2021-09-24 18:16:37 +00:00
|
|
|
|
2022-10-04 10:48:23 +00:00
|
|
|
export function uploadFile(taskId: number, file: File, onSuccess?: (url: string) => void) {
|
2021-09-24 18:16:37 +00:00
|
|
|
const attachmentService = new AttachmentService()
|
|
|
|
const files = [file]
|
|
|
|
|
|
|
|
return uploadFiles(attachmentService, taskId, files, onSuccess)
|
|
|
|
}
|
|
|
|
|
2022-07-20 22:42:36 +00:00
|
|
|
export async function uploadFiles(
|
|
|
|
attachmentService: AttachmentService,
|
|
|
|
taskId: number,
|
2022-09-22 15:31:13 +00:00
|
|
|
files: File[] | FileList,
|
2022-10-04 10:48:23 +00:00
|
|
|
onSuccess?: (attachmentUrl: string) => void,
|
2022-07-20 22:42:36 +00:00
|
|
|
) {
|
2021-09-24 18:16:37 +00:00
|
|
|
const attachmentModel = new AttachmentModel({taskId})
|
2021-10-11 17:37:20 +00:00
|
|
|
const response = await attachmentService.create(attachmentModel, files)
|
|
|
|
console.debug(`Uploaded attachments for task ${taskId}, response was`, response)
|
|
|
|
|
2022-07-20 22:42:36 +00:00
|
|
|
response.success?.map((attachment: IAttachment) => {
|
2022-09-23 10:55:53 +00:00
|
|
|
useTaskStore().addTaskAttachment({
|
2021-10-11 17:37:20 +00:00
|
|
|
taskId,
|
|
|
|
attachment,
|
2021-09-24 18:16:37 +00:00
|
|
|
})
|
2022-10-04 10:48:23 +00:00
|
|
|
onSuccess?.(generateAttachmentUrl(taskId, attachment.id))
|
2021-10-11 17:37:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if (response.errors !== null) {
|
|
|
|
throw Error(response.errors)
|
|
|
|
}
|
2021-09-24 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 19:15:35 +00:00
|
|
|
export function generateAttachmentUrl(taskId: number, attachmentId: number) {
|
2021-09-24 18:16:37 +00:00
|
|
|
return `${window.API_URL}/tasks/${taskId}/attachments/${attachmentId}`
|
|
|
|
}
|