import { useEffect, useState } from "react"; // next imports import Router from "next/router"; // services import userService from "services/user.service"; import workspaceService from "services/workspace.service"; // hooks import useUserAuth from "hooks/use-user-auth"; // layouts import DefaultLayout from "layouts/default-layout"; // components import { InviteMembers, OnboardingCard, OnboardingLogo, UserDetails, Workspace, } from "components/onboarding"; // ui import { PrimaryButton, Spinner } from "components/ui"; // constant import { ONBOARDING_CARDS } from "constants/workspace"; // types import type { NextPage } from "next"; const Onboarding: NextPage = () => { const [step, setStep] = useState(null); const [userRole, setUserRole] = useState(null); const [isLoading, setIsLoading] = useState(false); const [workspace, setWorkspace] = useState(); const { user, isLoading: userLoading, mutateUser } = useUserAuth("onboarding"); useEffect(() => { if (user && step === null) { let currentStep: null | number = null; if (user?.role) currentStep = 2; if (user?.last_workspace_id) currentStep = 4; if (currentStep != null) setStep(() => currentStep); } }, [step, user]); return ( {userLoading || isLoading || step === null ? (
) : (
{step <= 3 ? (
{step === 1 ? ( ) : step === 2 ? ( ) : ( step === 3 && )}
) : (
{step === 4 ? ( ) : step === 5 ? ( ) : step === 6 ? ( ) : step === 7 ? ( ) : ( step === 8 && )}
{ if (step === 8) { setIsLoading(true); userService .updateUserOnBoard({ userRole }, user) .then(async () => { mutateUser(); const userWorkspaces = await workspaceService.userWorkspaces(); const lastActiveWorkspace = userWorkspaces.find( (workspace) => workspace.id === user?.last_workspace_id ) ?? userWorkspaces[0]; if (lastActiveWorkspace) { userService .updateUser({ last_workspace_id: lastActiveWorkspace.id, }) .then((res) => { mutateUser(); }) .catch((err) => { console.log(err); }); Router.push(`/${lastActiveWorkspace.slug}`); return; } else { const invitations = await workspaceService.userWorkspaceInvitations(); if (invitations.length > 0) { Router.push(`/invitations`); return; } else { Router.push(`/create-workspace`); return; } } }) .catch((err) => { setIsLoading(false); console.log(err); }); } else setStep((prevData) => (prevData != null ? prevData + 1 : prevData)); }} > {step === 4 || step === 8 ? "Get Started" : "Next"}
)}
Logged in: {user?.email}
)}
); }; export default Onboarding;