Skip to content

Commit

Permalink
fix: weird redirection in index page (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
dakshesh14 authored Apr 12, 2023
1 parent 9196fb4 commit db48833
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
24 changes: 11 additions & 13 deletions apps/app/components/workspace/sidebar-dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
31 changes: 20 additions & 11 deletions apps/app/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="h-screen grid place-items-center">
<div className="grid h-screen place-items-center">
<Spinner />
</div>
);
Expand Down

0 comments on commit db48833

Please sign in to comment.