organization.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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="!pending && file === null"
  17. @click="submit"
  18. >
  19. {{ $t('generate') }}
  20. </v-btn>
  21. <v-btn
  22. v-else
  23. color="primary"
  24. :loading="pending"
  25. :disabled="pending"
  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 type {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 {useDownloadFile} from "~/composables/utils/useDownloadFile";
  43. const { em } = useEntityManager()
  44. const { getRef } = useEntityFetch()
  45. const sseStore = useSseStore()
  46. const async = () => { return sseStore.connected }
  47. const fileId = ref(null)
  48. // TODO: il y a quelque chose à creuser ici, cette ref est nécessaire pour garder le lien avec le store,
  49. // car on ne peut pas directement passer par le fetch de useEntityFetch ici (puisqu'on a pas encore l'id
  50. // au chargement). Il faudrait voir si on ne pourrait pas intégrer ça au fetch, ou faire un fetchOrNull, qui
  51. // combine fetch et getRef?
  52. const file: ComputedRef<File | null> = getRef(File, fileId)
  53. // Submitting export request
  54. const submitting: Ref<boolean> = ref(false)
  55. // File is downloading
  56. const downloading: Ref<boolean> = ref(false)
  57. const pending: ComputedRef<boolean> = computed(() => {
  58. return submitting.value || (file.value !== null && file.value.status === 'PENDING') || downloading.value
  59. })
  60. const submit = async () => {
  61. submitting.value = true
  62. const exportRequest = em.newInstance(LicenceCmfOrganizationER)
  63. exportRequest.format = 'pdf'
  64. if (async()) {
  65. exportRequest.async = true
  66. } else {
  67. console.error('SSE unavailable - File will be downloaded synchronously')
  68. }
  69. try {
  70. // Send the export request and get the receipt
  71. const receipt = await em.persist(LicenceCmfOrganizationER, exportRequest)
  72. if (receipt.fileId === null) {
  73. throw new Error("Missing file's id, abort")
  74. }
  75. fileId.value = receipt.fileId
  76. // Fetch the newly created file from API. If export is async, it will be a record about a pending file,
  77. // SSE will update its status to ready when it'll be.
  78. await em.fetch(File, receipt.fileId)
  79. } finally {
  80. submitting.value = false
  81. }
  82. }
  83. const download = async () => {
  84. downloading.value = true
  85. if (file.value === null) {
  86. console.error("File is not defined yet, impossible to download")
  87. return
  88. }
  89. if (file.value.status === 'PENDING') {
  90. console.error("File is still pending, impossible to download")
  91. return
  92. }
  93. try {
  94. await useDownloadFile(file.value)
  95. } finally {
  96. downloading.value = false
  97. }
  98. }
  99. </script>