organization.vue 3.3 KB

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