From 46f006e87c1d35aaec3e8d0cee89d5e5609b896c Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Mon, 25 Jul 2022 19:10:36 +0800 Subject: [PATCH] wip(gui-v2): socket logic & add base url & header to $fetch --- .../components/dlg/AirtableImport.vue | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/packages/nc-gui-v2/components/dlg/AirtableImport.vue b/packages/nc-gui-v2/components/dlg/AirtableImport.vue index e3dfd3415b..cae86ba300 100644 --- a/packages/nc-gui-v2/components/dlg/AirtableImport.vue +++ b/packages/nc-gui-v2/components/dlg/AirtableImport.vue @@ -15,6 +15,9 @@ interface Props { const { modelValue } = defineProps() const emit = defineEmits(['update:modelValue']) +// TODO: handle baseURL +const baseURL = 'http://localhost:8080' // this.$axios.defaults.baseURL + const { $state } = useNuxtApp() const toast = useToast() const { sqlUi, project, loadTables } = useProject() @@ -72,12 +75,16 @@ async function createOrUpdate() { const { id, ...payload } = syncSource.value if (id !== '') { await $fetch(`/api/v1/db/meta/syncs/${id}`, { + baseURL, method: 'PATCH', + headers: { 'xc-auth': $state.token.value as string }, body: payload, }) } else { - const { data }: any = await $fetch(`/api/v1/db/meta/projects/${project.value.id}/syncs`, { + const data: any = await $fetch(`/api/v1/db/meta/projects/${project.value.id}/syncs`, { + baseURL, method: 'POST', + headers: { 'xc-auth': $state.token.value as string }, body: payload, }) syncSource.value = data @@ -88,11 +95,12 @@ async function createOrUpdate() { } async function loadSyncSrc() { - const { - data: { list: srcs }, - }: any = await $fetch(`/api/v1/db/meta/projects/${project.value.id}/syncs`, { + const data: any = await $fetch(`/api/v1/db/meta/projects/${project.value.id}/syncs`, { + baseURL, method: 'GET', + headers: { 'xc-auth': $state.token.value as string }, }) + const { list: srcs } = data if (srcs && srcs[0]) { srcs[0].details = srcs[0].details || {} syncSource.value = migrateSync(srcs[0]) @@ -122,12 +130,19 @@ async function loadSyncSrc() { async function sync() { step.value = 2 - const { data }: any = await $fetch(`/api/v1/db/meta/syncs/${syncSource.value.id}/trigger`, { - method: 'POST', - body: { - id: socket.value.id, - }, - }) + try { + await $fetch(`/api/v1/db/meta/syncs/${syncSource.value.id}/trigger`, { + baseURL, + method: 'POST', + headers: { 'xc-auth': $state.token.value as string }, + body: { + id: socket.value.id, + }, + }) + } catch (e: any) { + console.log(e) + toast.error(e) + } } function migrateSync(src: any) { @@ -153,9 +168,7 @@ watch(syncSourceUrlOrId, (v) => { } }) -onMounted(() => { - // TODO: handle base url - const baseURL = 'http://localhost:8080' // this.$axios.defaults.baseURL +onMounted(async () => { socket.value = io(new URL(baseURL, window.location.href.split(/[?#]/)[0]).href, { extraHeaders: { 'xc-auth': $state.token.value as string }, }) @@ -185,7 +198,13 @@ onMounted(() => { // TODO: add tab of the first table } }) - loadSyncSrc() + await loadSyncSrc() +}) + +onBeforeUnmount(() => { + if (socket.value) { + socket.value.disconnect() + } })