organization.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <div class="d-flex flex-column align-center">
  3. <h2 class="ma-4">{{ $t('cmf_structure_licence')}}</h2>
  4. <a
  5. href="https://www.cmf-musique.org/services/tarifs-preferentiels/"
  6. target="_blank"
  7. >
  8. {{ $t('cmf_licence_details_url')}}
  9. </a>
  10. <v-form
  11. ref="form"
  12. lazy-validation
  13. >
  14. <div class="ma-12">
  15. <v-btn
  16. v-if="!filePending && file === null"
  17. @click="submit"
  18. >
  19. {{ $t('generate') }}
  20. </v-btn>
  21. <v-btn
  22. v-else
  23. color="primary"
  24. :loading="filePending"
  25. :disabled="filePending"
  26. target="_blank"
  27. @click="download"
  28. >
  29. {{ $t('download') }}
  30. </v-btn>
  31. </div>
  32. </v-form>
  33. </div>
  34. </template>
  35. <script setup lang="ts">
  36. import File from "~/models/Core/File"
  37. import {ComputedRef, Ref} from "vue";
  38. import {useEntityFetch} from "~/composables/data/useEntityFetch";
  39. import {useSseStore} from "~/stores/sse";
  40. import {useEntityManager} from "~/composables/data/useEntityManager";
  41. import LicenceCmfOrganizationER from "~/models/Export/LicenceCmfOrganizationER";
  42. import {useFileManager} from "~/composables/data/useFileManager";
  43. const { em } = useEntityManager()
  44. const { fetch } = useEntityFetch()
  45. const { fileManager } = useFileManager()
  46. const sseStore = useSseStore()
  47. const async = () => { return sseStore.connected }
  48. const file: Ref<File | null> = ref(null)
  49. const submittingRequest: Ref<boolean> = ref(false)
  50. const filePending: ComputedRef<boolean> = computed(() => {
  51. return submittingRequest.value || (file.value !== null && file.value.status === 'PENDING')
  52. })
  53. const submit = async () => {
  54. submittingRequest.value = true
  55. const exportRequest = em.newInstance(LicenceCmfOrganizationER)
  56. exportRequest.format = 'pdf'
  57. if (async()) {
  58. exportRequest.async = false
  59. } else {
  60. console.error('SSE unavailable - File downloaded synchronously')
  61. }
  62. // Send the export request and get the receipt
  63. const receipt = await em.persist(LicenceCmfOrganizationER, exportRequest)
  64. if (receipt.fileId === null) {
  65. throw new Error("Missing file's id, abort")
  66. }
  67. file.value = await em.fetch(File, receipt.fileId as number, true) as File
  68. submittingRequest.value = false
  69. }
  70. const download = async () => {
  71. if (file.value === null) {
  72. console.error("File is not defined yet, impossible to download")
  73. return
  74. }
  75. if (filePending.value) {
  76. console.error("File is still pending, impossible to download")
  77. return
  78. }
  79. const response = await fileManager.download(file.value.id as number)
  80. const blob = new Blob([response], { type: response.type })
  81. const url = window.URL.createObjectURL(blob)
  82. const link = document.createElement('a')
  83. link.href = url
  84. link.download = file.value.name ?? 'unknown'
  85. link.target = '_blank'
  86. link.click()
  87. link.remove()
  88. window.URL.revokeObjectURL(url)
  89. }
  90. </script>