From 4791a776d4e26ef0ad6c613a1aa260e52ef698a1 Mon Sep 17 00:00:00 2001 From: Reto Aebersold Date: Thu, 9 Nov 2023 10:01:36 +0100 Subject: [PATCH] feat: file upload composable --- client/src/composables.ts | 28 +++++++++++ .../assignment/AttachmentSection.vue | 40 ++++++---------- .../pages/onboarding/uk/AccountProfile.vue | 46 ++++++++----------- client/src/services/files.ts | 14 +++++- 4 files changed, 76 insertions(+), 52 deletions(-) diff --git a/client/src/composables.ts b/client/src/composables.ts index 28924f1f..af4fda5b 100644 --- a/client/src/composables.ts +++ b/client/src/composables.ts @@ -6,6 +6,7 @@ import { circleFlatLearningContents, circleFlatLearningUnits, } from "@/services/circle"; +import { presignUpload, uploadFile } from "@/services/files"; import { useCompletionStore } from "@/stores/completion"; import { useCourseSessionsStore } from "@/stores/courseSessions"; import { useDashboardStore } from "@/stores/dashboard"; @@ -435,3 +436,30 @@ export function useCourseStatistics() { return { courseSessionName, circleMeta }; } + +export function useFileUpload() { + const error = ref(false); + const loading = ref(false); + const fileInfo = ref({} as { id: string; name: string; url: string }); + + async function upload(e: Event) { + const { files } = e.target as HTMLInputElement; + if (!files?.length) return; + + try { + error.value = false; + loading.value = true; + const file = files[0]; + const presignData = await presignUpload(file); + await uploadFile(presignData.pre_sign, file); + fileInfo.value = presignData.file_info; + } catch (e) { + console.error(e); + error.value = true; + } finally { + loading.value = false; + } + } + + return { upload, error, loading, fileInfo }; +} diff --git a/client/src/pages/learningPath/learningContentPage/assignment/AttachmentSection.vue b/client/src/pages/learningPath/learningContentPage/assignment/AttachmentSection.vue index 5ecd92a8..62738a83 100644 --- a/client/src/pages/learningPath/learningContentPage/assignment/AttachmentSection.vue +++ b/client/src/pages/learningPath/learningContentPage/assignment/AttachmentSection.vue @@ -1,7 +1,7 @@