fix: unusual redirection on onboarding (#808)

This commit is contained in:
Dakshesh Jain 2023-04-13 15:38:00 +05:30 committed by GitHub
parent cd69b06e5e
commit 484a88d881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,70 +1,60 @@
import { useEffect } from "react"; import { useEffect } from "react";
import { useRouter } from "next/router"; import Router from "next/router";
import useSWR from "swr";
// services // services
import userService from "services/user.service";
import workspaceService from "services/workspace.service"; import workspaceService from "services/workspace.service";
// hooks
import useUser from "hooks/use-user";
import useWorkspaces from "hooks/use-workspaces";
// ui // ui
import { Spinner } from "components/ui"; import { Spinner } from "components/ui";
// types // types
import type { NextPage } from "next"; import type { NextPage } from "next";
// fetch-keys
import { USER_WORKSPACE_INVITATIONS } from "constants/fetch-keys";
const Home: NextPage = () => { const redirectUserTo = async () => {
const router = useRouter(); const user = await userService
.currentUser()
const { user, isUserLoading } = useUser(); .then((res) => res)
const { workspaces, error: workspacesError } = useWorkspaces(); .catch(() => {
Router.push("/signin");
const lastActiveWorkspace =
user && workspaces.find((workspace) => workspace.id === user.last_workspace_id);
const { data: invitations, error: invitationsError } = useSWR(USER_WORKSPACE_INVITATIONS, () =>
workspaceService.userWorkspaceInvitations()
);
useEffect(() => {
if (isUserLoading) return;
if (!user) {
router.push("/signin");
return; return;
} else if (!user.is_onboarded) { });
router.push("/onboarding");
return;
}
if (!user || (!workspaces && !workspacesError)) return; if (!user) {
Router.push("/signin");
return;
} else if (!user.user.is_onboarded) {
Router.push("/onboarding");
return;
} else {
const userWorkspaces = await workspaceService.userWorkspaces();
const lastActiveWorkspace = userWorkspaces.find(
(workspace) => workspace.id === user.user.last_workspace_id
);
if (lastActiveWorkspace) { if (lastActiveWorkspace) {
router.push(`/${lastActiveWorkspace.slug}`); Router.push(`/${lastActiveWorkspace.slug}`);
return; return;
} else if (workspaces.length > 0) { } else if (userWorkspaces.length > 0) {
router.push(`/${workspaces[0].slug}`); Router.push(`/${userWorkspaces[0].slug}`);
return;
} else if (!invitationsError && invitations && invitations.length > 0) {
router.push("/invitations");
return; return;
} else { } else {
router.push("/create-workspace"); const invitations = await workspaceService.userWorkspaceInvitations();
return; if (invitations.length > 0) {
Router.push(`/invitations`);
return;
} else {
Router.push(`/create-workspace`);
return;
}
} }
}, [ }
invitations, };
invitationsError,
isUserLoading, const Home: NextPage = () => {
lastActiveWorkspace, useEffect(() => {
user, redirectUserTo();
workspaces, }, []);
router,
workspacesError,
]);
return ( return (
<div className="grid h-screen place-items-center"> <div className="grid h-screen place-items-center">