From af9b0a738d358f9a87e051957d02f7462b03fbc4 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 30 Jul 2021 20:39:42 +0100 Subject: [PATCH] Fixing issues with pages reloading in weird ways. --- .../builder/src/pages/builder/_layout.svelte | 7 +++++-- .../builder/src/pages/builder/admin/index.svelte | 10 ++++------ .../builder/src/pages/builder/auth/index.svelte | 2 +- .../builder/src/pages/builder/auth/login.svelte | 5 ++++- .../builder/src/pages/builder/auth/org.svelte | 6 +++--- packages/builder/src/pages/index.svelte | 5 ++++- packages/builder/src/stores/portal/auth.js | 16 +++++++++++----- 7 files changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/builder/src/pages/builder/_layout.svelte b/packages/builder/src/pages/builder/_layout.svelte index 9ea21317a7..b1f6d7e733 100644 --- a/packages/builder/src/pages/builder/_layout.svelte +++ b/packages/builder/src/pages/builder/_layout.svelte @@ -4,6 +4,7 @@ import { onMount } from "svelte" let loaded = false + $: multiTenancyEnabled = $admin.multiTenancy $: hasAdminUser = !!$admin?.checklist?.adminUser $: tenantSet = $auth.tenantSet @@ -14,12 +15,14 @@ loaded = true }) - // Force creation of an admin user if one doesn't exist $: { const apiReady = $admin.loaded && $auth.loaded + // if tenant is not set go to it if (loaded && apiReady && multiTenancyEnabled && !tenantSet) { $redirect("./auth/org") - } else if (loaded && apiReady && !hasAdminUser) { + } + // Force creation of an admin user if one doesn't exist + else if (loaded && apiReady && !hasAdminUser) { $redirect("./admin") } } diff --git a/packages/builder/src/pages/builder/admin/index.svelte b/packages/builder/src/pages/builder/admin/index.svelte index abacb86b8d..bc76c69cc0 100644 --- a/packages/builder/src/pages/builder/admin/index.svelte +++ b/packages/builder/src/pages/builder/admin/index.svelte @@ -36,11 +36,6 @@ notifications.error(`Failed to create admin user`) } } - - function changeOrg() { - auth.setOrg(null) - $goto("../auth") - }
@@ -62,7 +57,10 @@ Create super admin user {#if multiTenancyEnabled} - + { + admin.unload() + $goto("../auth/org") + }}> Change organisation {/if} diff --git a/packages/builder/src/pages/builder/auth/index.svelte b/packages/builder/src/pages/builder/auth/index.svelte index daeb56cb6e..a2a02e65c1 100644 --- a/packages/builder/src/pages/builder/auth/index.svelte +++ b/packages/builder/src/pages/builder/auth/index.svelte @@ -18,7 +18,7 @@ onMount(async () => { await admin.init() - auth.checkQueryString() + await auth.checkQueryString() loaded = true }) diff --git a/packages/builder/src/pages/builder/auth/login.svelte b/packages/builder/src/pages/builder/auth/login.svelte index 2dc3781e30..36632a4862 100644 --- a/packages/builder/src/pages/builder/auth/login.svelte +++ b/packages/builder/src/pages/builder/auth/login.svelte @@ -84,7 +84,10 @@ Forgot password? {#if multiTenancyEnabled} - $goto("./org")}> + { + admin.unload() + $goto("./org") + }}> Change organisation {/if} diff --git a/packages/builder/src/pages/builder/auth/org.svelte b/packages/builder/src/pages/builder/auth/org.svelte index 7e26924423..785cf05914 100644 --- a/packages/builder/src/pages/builder/auth/org.svelte +++ b/packages/builder/src/pages/builder/auth/org.svelte @@ -13,7 +13,7 @@ if (tenantId == null || tenantId === "") { tenantId = "default" } - auth.setOrg(tenantId) + await auth.setOrg(tenantId) // re-init now org selected await admin.init() $goto("../") @@ -23,8 +23,8 @@ if (evt.key === "Enter") setOrg() } - onMount(() => { - auth.checkQueryString() + onMount(async () => { + await auth.checkQueryString() if (!multiTenancyEnabled) { $goto("../") } else { diff --git a/packages/builder/src/pages/index.svelte b/packages/builder/src/pages/index.svelte index 0c083391ad..477097f726 100644 --- a/packages/builder/src/pages/index.svelte +++ b/packages/builder/src/pages/index.svelte @@ -1,8 +1,11 @@ diff --git a/packages/builder/src/stores/portal/auth.js b/packages/builder/src/stores/portal/auth.js index b8eec0730e..fe8f87cfb2 100644 --- a/packages/builder/src/stores/portal/auth.js +++ b/packages/builder/src/stores/portal/auth.js @@ -1,5 +1,6 @@ import { derived, writable, get } from "svelte/store" import api from "../../builderStore/api" +import { admin } from "stores/portal" export function createAuthStore() { const auth = writable({ @@ -50,25 +51,30 @@ export function createAuthStore() { }) } - function setOrganisation(tenantId) { + async function setOrganisation(tenantId) { + const prevId = get(store).tenantId auth.update(store => { store.tenantId = tenantId store.tenantSet = !!tenantId return store }) + if (prevId !== tenantId) { + // re-init admin after setting org + await admin.init() + } } return { subscribe: store.subscribe, - checkQueryString: () => { + checkQueryString: async () => { const urlParams = new URLSearchParams(window.location.search) if (urlParams.has("tenantId")) { const tenantId = urlParams.get("tenantId") - setOrganisation(tenantId) + await setOrganisation(tenantId) } }, - setOrg: tenantId => { - setOrganisation(tenantId) + setOrg: async tenantId => { + await setOrganisation(tenantId) }, checkAuth: async () => { const response = await api.get("/api/global/users/self")