|
|
@@ -34,40 +34,79 @@
|
|
|
</template>
|
|
|
|
|
|
<script lang="ts">
|
|
|
-import {defineComponent, Ref, ref, useContext} from "@nuxtjs/composition-api";
|
|
|
+import {defineComponent, onMounted, onUnmounted, Ref, ref, useContext} from "@nuxtjs/composition-api";
|
|
|
import {HTTP_METHOD, QUERY_TYPE} from "~/types/enums";
|
|
|
import DataPersister from "~/services/data/dataPersister";
|
|
|
import {DataPersisterArgs} from "~/types/interfaces";
|
|
|
+import { EventSourcePolyfill } from "event-source-polyfill";
|
|
|
|
|
|
export default defineComponent({
|
|
|
name: 'OrganizationCmfLicence',
|
|
|
setup() {
|
|
|
- const { $config } = useContext()
|
|
|
+ const { $config, store } = useContext()
|
|
|
+
|
|
|
let pending: Ref<boolean> = ref(false)
|
|
|
let fileUrl: Ref<string | null> = ref(null)
|
|
|
+ let eventSource: Ref<EventSource | null> = ref(null)
|
|
|
+ let async: boolean = true
|
|
|
|
|
|
const submit = async () => {
|
|
|
+ const response = await DataPersister.request(
|
|
|
+ '/api/export/cmf-licence/organization',
|
|
|
+ HTTP_METHOD.POST,
|
|
|
+ { type: QUERY_TYPE.DEFAULT, data: { format: 'pdf', async: async }, withCredentials: true } as DataPersisterArgs
|
|
|
+ )
|
|
|
+ if (async) {
|
|
|
+ pending.value = true
|
|
|
+ } else {
|
|
|
+ console.warn('File is downloaded synchronously')
|
|
|
+ fileUrl.value = JSON.parse(response).url
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const unsubscribe = async () => {
|
|
|
+ if (eventSource.value !== null) {
|
|
|
+ eventSource.value.onerror = null
|
|
|
+ eventSource.value.close()
|
|
|
+ eventSource.value = null
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const subscribe = async () => {
|
|
|
+ await unsubscribe()
|
|
|
+
|
|
|
const url = new URL($config.baseUrl_mercure)
|
|
|
- url.searchParams.append('topic', "files")
|
|
|
- // const eventSource = new EventSource(url.toString(), { withCredentials: true });
|
|
|
- const eventSource = new EventSource(url.toString());
|
|
|
- eventSource.onmessage = event => {
|
|
|
- // Will be called every time an update is published by the server
|
|
|
+ url.searchParams.append('topic', "access/" + store.state.profile.access.id)
|
|
|
+
|
|
|
+ eventSource.value = new EventSourcePolyfill(url.toString(), { withCredentials: true });
|
|
|
+
|
|
|
+ eventSource.value.onerror = event => {
|
|
|
+ async = false
|
|
|
+ console.error('Error while subscribing to the EventSource, fallback to sync mode : ' + JSON.stringify(event))
|
|
|
+ unsubscribe()
|
|
|
+ }
|
|
|
+ eventSource.value.onopen = event => {
|
|
|
+ console.log('Listening for events...')
|
|
|
+ }
|
|
|
+ eventSource.value.onmessage = event => {
|
|
|
const data = JSON.parse(event.data)
|
|
|
fileUrl.value = data.url
|
|
|
pending.value = false
|
|
|
}
|
|
|
-
|
|
|
- await DataPersister.request(
|
|
|
- '/api/export/cmf-licence/organization',
|
|
|
- HTTP_METHOD.POST,
|
|
|
- { type: QUERY_TYPE.DEFAULT, data: { format: 'pdf', async: true } } as DataPersisterArgs
|
|
|
- )
|
|
|
- pending.value = true
|
|
|
}
|
|
|
|
|
|
+ onMounted(() => {
|
|
|
+ subscribe()
|
|
|
+ })
|
|
|
+
|
|
|
+ onUnmounted(() => {
|
|
|
+ unsubscribe()
|
|
|
+ })
|
|
|
+
|
|
|
return {
|
|
|
+ eventSource,
|
|
|
submit,
|
|
|
+ subscribe,
|
|
|
pending,
|
|
|
fileUrl
|
|
|
}
|