organization.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 && fileUrl === 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. :href="fileUrl">
  27. {{ $t('download') }}
  28. </v-btn>
  29. </div>
  30. </v-form>
  31. </div>
  32. </template>
  33. <script lang="ts">
  34. import {defineComponent, onMounted, onUnmounted, Ref, ref, useContext} from "@nuxtjs/composition-api";
  35. import {HTTP_METHOD, QUERY_TYPE} from "~/types/enums";
  36. import DataPersister from "~/services/data/dataPersister";
  37. import {DataPersisterArgs} from "~/types/interfaces";
  38. import { EventSourcePolyfill } from "event-source-polyfill";
  39. export default defineComponent({
  40. name: 'OrganizationCmfLicence',
  41. setup() {
  42. const { $config, store } = useContext()
  43. let pending: Ref<boolean> = ref(false)
  44. let fileUrl: Ref<string | null> = ref(null)
  45. let eventSource: Ref<EventSource | null> = ref(null)
  46. let async: boolean = true
  47. const submit = async () => {
  48. const response = await DataPersister.request(
  49. '/api/export/cmf-licence/organization',
  50. HTTP_METHOD.POST,
  51. { type: QUERY_TYPE.DEFAULT, data: { format: 'pdf', async: async }, withCredentials: true } as DataPersisterArgs
  52. )
  53. if (async) {
  54. pending.value = true
  55. } else {
  56. console.warn('File is downloaded synchronously')
  57. fileUrl.value = JSON.parse(response).url
  58. }
  59. }
  60. const unsubscribe = async () => {
  61. if (eventSource.value !== null) {
  62. eventSource.value.onerror = null
  63. eventSource.value.close()
  64. eventSource.value = null
  65. }
  66. }
  67. const subscribe = async () => {
  68. await unsubscribe()
  69. const url = new URL($config.baseUrl_mercure)
  70. url.searchParams.append('topic', "access/" + store.state.profile.access.id)
  71. eventSource.value = new EventSourcePolyfill(url.toString(), { withCredentials: true });
  72. eventSource.value.onerror = event => {
  73. async = false
  74. console.error('Error while subscribing to the EventSource, fallback to sync mode : ' + JSON.stringify(event))
  75. unsubscribe()
  76. }
  77. eventSource.value.onopen = event => {
  78. console.log('Listening for events...')
  79. }
  80. eventSource.value.onmessage = event => {
  81. const data = JSON.parse(event.data)
  82. fileUrl.value = data.url
  83. pending.value = false
  84. }
  85. }
  86. onMounted(() => {
  87. subscribe()
  88. })
  89. onUnmounted(() => {
  90. unsubscribe()
  91. })
  92. return {
  93. eventSource,
  94. submit,
  95. subscribe,
  96. pending,
  97. fileUrl
  98. }
  99. }
  100. })
  101. </script>