|
|
@@ -14,7 +14,7 @@
|
|
|
>
|
|
|
<div class="ma-12">
|
|
|
<v-btn
|
|
|
- v-if="!filePending && file === null"
|
|
|
+ v-if="!pending && file === null"
|
|
|
@click="submit"
|
|
|
>
|
|
|
{{ $t('generate') }}
|
|
|
@@ -23,8 +23,8 @@
|
|
|
<v-btn
|
|
|
v-else
|
|
|
color="primary"
|
|
|
- :loading="filePending"
|
|
|
- :disabled="filePending"
|
|
|
+ :loading="pending"
|
|
|
+ :disabled="pending"
|
|
|
target="_blank"
|
|
|
@click="download"
|
|
|
>
|
|
|
@@ -42,71 +42,80 @@ import {useEntityFetch} from "~/composables/data/useEntityFetch";
|
|
|
import {useSseStore} from "~/stores/sse";
|
|
|
import {useEntityManager} from "~/composables/data/useEntityManager";
|
|
|
import LicenceCmfOrganizationER from "~/models/Export/LicenceCmfOrganizationER";
|
|
|
-import {useFileManager} from "~/composables/data/useFileManager";
|
|
|
+import {useDownloadFile} from "~/composables/utils/useDownloadFile";
|
|
|
+import {useRepo} from "pinia-orm";
|
|
|
|
|
|
const { em } = useEntityManager()
|
|
|
-const { fetch } = useEntityFetch()
|
|
|
-const { fileManager } = useFileManager()
|
|
|
+const { getRef } = useEntityFetch()
|
|
|
|
|
|
const sseStore = useSseStore()
|
|
|
const async = () => { return sseStore.connected }
|
|
|
|
|
|
-const file: Ref<File | null> = ref(null)
|
|
|
+const fileId = ref(null)
|
|
|
|
|
|
-const submittingRequest: Ref<boolean> = ref(false)
|
|
|
+// TODO: il y a quelque chose à creuser ici, cette ref est nécessaire pour garder le lien avec le store,
|
|
|
+// car on ne peut pas directement passer par le fetch de useEntityFetch ici (puisqu'on a pas encore l'id
|
|
|
+// au chargement). Il faudrait voir si on ne pourrait pas intégrer ça au fetch, ou faire un fetchOrNull, qui
|
|
|
+// combine fetch et getRef?
|
|
|
+const file: ComputedRef<File | null> = getRef(File, fileId)
|
|
|
|
|
|
-const filePending: ComputedRef<boolean> = computed(() => {
|
|
|
- return submittingRequest.value || (file.value !== null && file.value.status === 'PENDING')
|
|
|
+// Submitting export request
|
|
|
+const submitting: Ref<boolean> = ref(false)
|
|
|
+
|
|
|
+// File is downloading
|
|
|
+const downloading: Ref<boolean> = ref(false)
|
|
|
+
|
|
|
+const pending: ComputedRef<boolean> = computed(() => {
|
|
|
+ return submitting.value || (file.value !== null && file.value.status === 'PENDING') || downloading.value
|
|
|
})
|
|
|
|
|
|
const submit = async () => {
|
|
|
- submittingRequest.value = true
|
|
|
+ submitting.value = true
|
|
|
|
|
|
const exportRequest = em.newInstance(LicenceCmfOrganizationER)
|
|
|
|
|
|
exportRequest.format = 'pdf'
|
|
|
|
|
|
if (async()) {
|
|
|
- exportRequest.async = false
|
|
|
+ exportRequest.async = true
|
|
|
} else {
|
|
|
- console.error('SSE unavailable - File downloaded synchronously')
|
|
|
+ console.error('SSE unavailable - File will be downloaded synchronously')
|
|
|
}
|
|
|
|
|
|
- // Send the export request and get the receipt
|
|
|
- const receipt = await em.persist(LicenceCmfOrganizationER, exportRequest)
|
|
|
- if (receipt.fileId === null) {
|
|
|
- throw new Error("Missing file's id, abort")
|
|
|
- }
|
|
|
+ try {
|
|
|
+ // Send the export request and get the receipt
|
|
|
+ const receipt = await em.persist(LicenceCmfOrganizationER, exportRequest)
|
|
|
+ if (receipt.fileId === null) {
|
|
|
+ throw new Error("Missing file's id, abort")
|
|
|
+ }
|
|
|
+ fileId.value = receipt.fileId
|
|
|
|
|
|
- file.value = await em.fetch(File, receipt.fileId as number, true) as File
|
|
|
+ // Fetch the newly created file from API. If export is async, it will be a record about a pending file,
|
|
|
+ // SSE will update its status to ready when it'll be.
|
|
|
+ await em.fetch(File, receipt.fileId)
|
|
|
|
|
|
- submittingRequest.value = false
|
|
|
+ } finally {
|
|
|
+ submitting.value = false
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
const download = async () => {
|
|
|
+ downloading.value = true
|
|
|
+
|
|
|
if (file.value === null) {
|
|
|
console.error("File is not defined yet, impossible to download")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- if (filePending.value) {
|
|
|
+ if (file.value.status === 'PENDING') {
|
|
|
console.error("File is still pending, impossible to download")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- const response = await fileManager.download(file.value.id as number)
|
|
|
-
|
|
|
- const blob = new Blob([response], { type: response.type })
|
|
|
-
|
|
|
- const url = window.URL.createObjectURL(blob)
|
|
|
-
|
|
|
- const link = document.createElement('a')
|
|
|
- link.href = url
|
|
|
- link.download = file.value.name ?? 'unknown'
|
|
|
- link.target = '_blank'
|
|
|
- link.click()
|
|
|
-
|
|
|
- link.remove()
|
|
|
- window.URL.revokeObjectURL(url)
|
|
|
+ try {
|
|
|
+ await useDownloadFile(file.value)
|
|
|
+ } finally {
|
|
|
+ downloading.value = false
|
|
|
+ }
|
|
|
}
|
|
|
</script>
|