mirror of
https://github.com/appwrite/console.git
synced 2026-11-18 20:47:21 +00:00
refactor: move some redirection logic to store
This commit is contained in:
1 parent
3b358fb8a4
commit
9e0ca577e0
4 files changed
+39
-26
No files matched your search
@@ -2,6 +2,8 @@ import { sdkForConsole } from '$lib/stores/sdk';
|
||||
import type { Models } from '@aw-labs/appwrite-console';
|
||||
import { writable } from 'svelte/store';
|
||||
import { browser } from '$app/env';
|
||||
import { get } from 'svelte/store';
|
||||
import { base } from '$app/paths';
|
||||
|
||||
function createOrganizationList() {
|
||||
const { subscribe, set } = writable<Models.TeamList>(
|
||||
@@ -82,3 +84,21 @@ if (browser) {
|
||||
projectList.subscribe((n) => sessionStorage?.setItem('projectList', JSON.stringify(n ?? '')));
|
||||
memberList.subscribe((n) => sessionStorage?.setItem('memberList', JSON.stringify(n ?? '')));
|
||||
}
|
||||
|
||||
export const redirectTo = async () => {
|
||||
let org = get(organization);
|
||||
if (org) {
|
||||
return `${base}/console/organization-${org.$id}`;
|
||||
} else {
|
||||
await organizationList.load();
|
||||
const orgList = get(organizationList);
|
||||
if (orgList?.total) {
|
||||
await organization.load(orgList.teams[0].$id);
|
||||
org = get(organization);
|
||||
return `${base}/console/organization-${org.$id}`;
|
||||
} else {
|
||||
newOrgModal.set(true);
|
||||
return `${base}/console`;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -7,6 +7,7 @@
|
||||
import { goto } from '$app/navigation';
|
||||
import { page } from '$app/stores';
|
||||
import { user } from '$lib/stores/user';
|
||||
import { redirectTo } from '$lib/stores/organization';
|
||||
import { onMount } from 'svelte';
|
||||
import { base } from '$app/paths';
|
||||
import { browser } from '$app/env';
|
||||
@@ -29,7 +30,8 @@
|
||||
}
|
||||
|
||||
if (!$page.url.pathname.startsWith('/console')) {
|
||||
await goto(`${base}/console`);
|
||||
const destination = await redirectTo();
|
||||
await goto(destination);
|
||||
}
|
||||
} catch (error) {
|
||||
await goto(`${base}/login`);
|
||||
|
||||
@@ -3,45 +3,36 @@
|
||||
import SideNavigation from '$lib/layout/navigation.svelte';
|
||||
import Header from '$lib/layout/header.svelte';
|
||||
import { updateLayout } from '$lib/stores/layout';
|
||||
import { organization, organizationList, newOrgModal } from '$lib/stores/organization';
|
||||
import {
|
||||
organization,
|
||||
organizationList,
|
||||
newOrgModal,
|
||||
redirectTo
|
||||
} from '$lib/stores/organization';
|
||||
import Create from './_createOrganization.svelte';
|
||||
import { page } from '$app/stores';
|
||||
import { afterNavigate, goto } from '$app/navigation';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
let closable = true;
|
||||
|
||||
updateLayout({
|
||||
title: $organization?.name ?? 'Projects',
|
||||
level: 0
|
||||
});
|
||||
|
||||
onMount(async () => {
|
||||
if ($page.url.pathname === '/console') {
|
||||
await pageLoad();
|
||||
if ($page.url.pathname === '/console' && !$newOrgModal) {
|
||||
const destination = await redirectTo();
|
||||
await goto(destination);
|
||||
}
|
||||
});
|
||||
|
||||
afterNavigate(async () => {
|
||||
if ($page.url.pathname === '/console') {
|
||||
await pageLoad();
|
||||
if ($page.url.pathname === '/console' && !$newOrgModal) {
|
||||
console.log('test');
|
||||
const destination = await redirectTo();
|
||||
await goto(destination);
|
||||
}
|
||||
});
|
||||
|
||||
const pageLoad = async () => {
|
||||
if (!$organization) {
|
||||
await organizationList.load();
|
||||
if ($organizationList?.total) {
|
||||
await organization.load($organizationList.teams[0].$id);
|
||||
await goto(`/console/organization-${$organization.$id}`);
|
||||
} else {
|
||||
closable = false;
|
||||
newOrgModal.set(true);
|
||||
}
|
||||
} else {
|
||||
await goto(`/console/organization-${$organization.$id}`);
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<Shell showSideNavigation={!$page?.params.organization}>
|
||||
@@ -58,5 +49,5 @@
|
||||
</Shell>
|
||||
|
||||
{#if $newOrgModal}
|
||||
<Create bind:show={$newOrgModal} {closable} />
|
||||
<Create bind:show={$newOrgModal} closable={!$organizationList?.total} />
|
||||
{/if}
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
onMount(async () => {
|
||||
await projectList.load();
|
||||
projects = $projectList?.projects.filter((n) => n.teamId === org);
|
||||
projects = $projectList?.projects?.filter((n) => n.teamId === org);
|
||||
});
|
||||
|
||||
let showCreate = false;
|
||||
@@ -53,7 +53,7 @@
|
||||
return { name, icon };
|
||||
};
|
||||
|
||||
$: projects = $projectList?.projects.filter((a) => a.teamId === org);
|
||||
$: projects = $projectList?.projects?.filter((a) => a.teamId === org);
|
||||
</script>
|
||||
|
||||
<Container>
|
||||
|
||||
Reference in new issue
Block a user