cmf_licence_structure.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 ref="form" lazy-validation>
  11. <div class="ma-12">
  12. <v-btn v-if="!pending && file === null" @click="submit">
  13. {{ $t('generate') }}
  14. </v-btn>
  15. <v-btn
  16. v-else
  17. color="primary"
  18. :loading="pending"
  19. :disabled="pending"
  20. target="_blank"
  21. @click="download"
  22. >
  23. {{ $t('download') }}
  24. </v-btn>
  25. </div>
  26. </v-form>
  27. </div>
  28. </template>
  29. <script setup lang="ts">
  30. import type { ComputedRef, Ref } from 'vue'
  31. import File from '~/models/Core/File'
  32. import { useEntityFetch } from '~/composables/data/useEntityFetch'
  33. import { useSseStore } from '~/stores/sse'
  34. import { useEntityManager } from '~/composables/data/useEntityManager'
  35. import LicenceCmfOrganizationER from '~/models/Export/LicenceCmfOrganizationER'
  36. import { useDownloadFile } from '~/composables/utils/useDownloadFile'
  37. const { em } = useEntityManager()
  38. const { getRef } = useEntityFetch()
  39. const sseStore = useSseStore()
  40. const async = () => {
  41. return sseStore.connected
  42. }
  43. const fileId = ref(null)
  44. // TODO: il y a quelque chose à creuser ici, cette ref est nécessaire pour garder le lien avec le store,
  45. // car on ne peut pas directement passer par le fetch de useEntityFetch ici (puisqu'on a pas encore l'id
  46. // au chargement). Il faudrait voir si on ne pourrait pas intégrer ça au fetch, ou faire un fetchOrNull, qui
  47. // combine fetch et getRef?
  48. const file: ComputedRef<File | null> = getRef(File, fileId)
  49. // Submitting export request
  50. const submitting: Ref<boolean> = ref(false)
  51. // File is downloading
  52. const downloading: Ref<boolean> = ref(false)
  53. const pending: ComputedRef<boolean> = computed(() => {
  54. return (
  55. submitting.value ||
  56. (file.value !== null && file.value.status === 'PENDING') ||
  57. downloading.value
  58. )
  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. // In the case of a SSE dysfonctionnement, program a forced checkup of file status
  80. for (let i = 0; i < 3; i++) {
  81. setTimeout(async () => {
  82. if (file.value && file.value.status === 'PENDING') {
  83. console.warn(
  84. "File's status has not been updated : force a status checkup",
  85. )
  86. await em.fetch(File, receipt.fileId, true)
  87. }
  88. }, i * 4000)
  89. }
  90. } finally {
  91. submitting.value = false
  92. }
  93. }
  94. const download = async () => {
  95. downloading.value = true
  96. if (file.value === null) {
  97. console.error('File is not defined yet, impossible to download')
  98. return
  99. }
  100. if (file.value.status === 'PENDING') {
  101. console.error('File is still pending, impossible to download')
  102. return
  103. }
  104. try {
  105. await useDownloadFile(file.value)
  106. } finally {
  107. downloading.value = false
  108. }
  109. }
  110. </script>