From db488338fbd661e7e055a302b9a59d82dab60355 Mon Sep 17 00:00:00 2001 From: Dakshesh Jain <65905942+dakshesh14@users.noreply.github.com> Date: Wed, 12 Apr 2023 19:03:08 +0530 Subject: [PATCH] fix: weird redirection in index page (#801) --- .../components/workspace/sidebar-dropdown.tsx | 24 +++++++------- apps/app/pages/index.tsx | 31 ++++++++++++------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/apps/app/components/workspace/sidebar-dropdown.tsx b/apps/app/components/workspace/sidebar-dropdown.tsx index 8ac3b4ccc..93be576fe 100644 --- a/apps/app/components/workspace/sidebar-dropdown.tsx +++ b/apps/app/components/workspace/sidebar-dropdown.tsx @@ -67,19 +67,17 @@ export const WorkspaceSidebarDropdown = () => { }; const handleSignOut = async () => { - await authenticationService - .signOut() - .catch(() => - setToastAlert({ - type: "error", - title: "Error!", - message: "Failed to sign out. Please try again.", - }) - ) - .finally(() => { - router.push("/signin"); - mutateUser(); - }); + router.push("/signin").then(() => { + mutateUser(); + }); + + await authenticationService.signOut().catch(() => + setToastAlert({ + type: "error", + title: "Error!", + message: "Failed to sign out. Please try again.", + }) + ); }; return ( diff --git a/apps/app/pages/index.tsx b/apps/app/pages/index.tsx index ef67f765d..ddc19fe57 100644 --- a/apps/app/pages/index.tsx +++ b/apps/app/pages/index.tsx @@ -19,46 +19,55 @@ import { USER_WORKSPACE_INVITATIONS } from "constants/fetch-keys"; const Home: NextPage = () => { const router = useRouter(); - const { user } = useUser(); - const { workspaces } = useWorkspaces(); + const { user, isUserLoading } = useUser(); + const { workspaces, error: workspacesError } = useWorkspaces(); const lastActiveWorkspace = user && workspaces.find((workspace) => workspace.id === user.last_workspace_id); - const { data: invitations } = useSWR(USER_WORKSPACE_INVITATIONS, () => + const { data: invitations, error: invitationsError } = useSWR(USER_WORKSPACE_INVITATIONS, () => workspaceService.userWorkspaceInvitations() ); useEffect(() => { + if (isUserLoading) return; + if (!user) { router.push("/signin"); return; - } - - if (!user.is_onboarded) { + } else if (!user.is_onboarded) { router.push("/onboarding"); return; } + if (!user || (!workspaces && !workspacesError)) return; + if (lastActiveWorkspace) { router.push(`/${lastActiveWorkspace.slug}`); return; } else if (workspaces.length > 0) { router.push(`/${workspaces[0].slug}`); return; - } - - if (invitations && invitations.length > 0) { + } else if (!invitationsError && invitations && invitations.length > 0) { router.push("/invitations"); return; } else { router.push("/create-workspace"); return; } - }, [user, router, lastActiveWorkspace, workspaces, invitations]); + }, [ + invitations, + invitationsError, + isUserLoading, + lastActiveWorkspace, + user, + workspaces, + router, + workspacesError, + ]); return ( -
+
);