organization.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, onBeforeUnmount, onMounted, 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: EventSource | null = null
  46. const sseOpened = () => {
  47. return eventSource !== null && eventSource.readyState === EventSource.OPEN
  48. }
  49. const submit = async () => {
  50. const response = await DataPersister.request(
  51. '/api/export/cmf-licence/organization',
  52. HTTP_METHOD.POST,
  53. { type: QUERY_TYPE.DEFAULT, data: { format: 'pdf', async: sseOpened() }, withCredentials: true } as DataPersisterArgs
  54. )
  55. if (sseOpened()) {
  56. pending.value = true
  57. } else {
  58. console.warn('File downloaded synchronously')
  59. fileUrl.value = JSON.parse(response).url
  60. }
  61. }
  62. const unsubscribe = async () => {
  63. if (eventSource === null || eventSource.readyState === EventSource.CLOSED) {
  64. return
  65. }
  66. console.log('SSE - Close subscription')
  67. eventSource.close()
  68. }
  69. const subscribe = async () => {
  70. if (sseOpened()) {
  71. console.error('SSE - Already subscribed to the event source')
  72. return
  73. }
  74. if (process.server) {
  75. console.error('SSE - Cannot subscribe on server side')
  76. return
  77. }
  78. const url = new URL($config.baseUrl_mercure)
  79. url.searchParams.append('topic', "access/" + store.state.profile.access.id)
  80. eventSource = new EventSourcePolyfill(
  81. url.toString(),
  82. { withCredentials: true }
  83. );
  84. eventSource.onerror = (event) => {
  85. console.error('SSE - An error happened : ' + JSON.stringify(event))
  86. eventSource?.close()
  87. }
  88. eventSource.onopen = () => {
  89. console.log('SSE - Listening for events...')
  90. }
  91. eventSource.onmessage = event => {
  92. const data = JSON.parse(event.data)
  93. fileUrl.value = data.url
  94. pending.value = false
  95. }
  96. }
  97. // onMounted(() => {
  98. // subscribe()
  99. // })
  100. //
  101. // onBeforeUnmount(() => {
  102. // unsubscribe()
  103. // })
  104. //
  105. // if (process.browser) {
  106. // window.addEventListener('beforeunload', unsubscribe)
  107. // }
  108. return {
  109. submit,
  110. pending,
  111. fileUrl
  112. }
  113. }
  114. })
  115. </script>