2021-09-24 20:16:37 +02:00
|
|
|
import AttachmentModel from '@/models/attachment'
|
|
|
|
import FileModel from '@/models/file'
|
|
|
|
|
|
|
|
import AttachmentService from '@/services/attachment'
|
|
|
|
import { store } from '@/store'
|
|
|
|
|
|
|
|
export function uploadFile(taskId: number, file: FileModel, onSuccess: () => Function) {
|
|
|
|
const attachmentService = new AttachmentService()
|
|
|
|
const files = [file]
|
|
|
|
|
|
|
|
return uploadFiles(attachmentService, taskId, files, onSuccess)
|
|
|
|
}
|
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
export async function uploadFiles(attachmentService: AttachmentService, taskId: number, files: FileModel[], onSuccess : Function = () => {}) {
|
2021-09-24 20:16:37 +02:00
|
|
|
const attachmentModel = new AttachmentModel({taskId})
|
2021-10-11 19:37:20 +02:00
|
|
|
const response = await attachmentService.create(attachmentModel, files)
|
|
|
|
console.debug(`Uploaded attachments for task ${taskId}, response was`, response)
|
|
|
|
|
|
|
|
response.success?.map((attachment: AttachmentModel) => {
|
|
|
|
store.dispatch('tasks/addTaskAttachment', {
|
|
|
|
taskId,
|
|
|
|
attachment,
|
2021-09-24 20:16:37 +02:00
|
|
|
})
|
2021-10-11 19:37:20 +02:00
|
|
|
onSuccess(generateAttachmentUrl(taskId, attachment.id))
|
|
|
|
})
|
|
|
|
|
|
|
|
if (response.errors !== null) {
|
|
|
|
throw Error(response.errors)
|
|
|
|
}
|
2021-09-24 20:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function generateAttachmentUrl(taskId: number, attachmentId: number) : any {
|
|
|
|
return `${window.API_URL}/tasks/${taskId}/attachments/${attachmentId}`
|
|
|
|
}
|