diff --git a/apps/app/components/onboarding/invite-members.tsx b/apps/app/components/onboarding/invite-members.tsx index 0114b2dcd..913607801 100644 --- a/apps/app/components/onboarding/invite-members.tsx +++ b/apps/app/components/onboarding/invite-members.tsx @@ -7,7 +7,7 @@ import { ICurrentUserResponse, IUser } from "types"; import { MultiInput, PrimaryButton, SecondaryButton } from "components/ui"; type Props = { - setStep: React.Dispatch>; + setStep: React.Dispatch>; workspace: any; user: ICurrentUserResponse | undefined; }; diff --git a/apps/app/components/onboarding/user-details.tsx b/apps/app/components/onboarding/user-details.tsx index 63df0d968..1e96bb143 100644 --- a/apps/app/components/onboarding/user-details.tsx +++ b/apps/app/components/onboarding/user-details.tsx @@ -21,7 +21,7 @@ const defaultValues: Partial = { type Props = { user?: IUser; - setStep: React.Dispatch>; + setStep: React.Dispatch>; setUserRole: React.Dispatch>; }; diff --git a/apps/app/components/onboarding/workspace.tsx b/apps/app/components/onboarding/workspace.tsx index 783f5f5e5..0beef1419 100644 --- a/apps/app/components/onboarding/workspace.tsx +++ b/apps/app/components/onboarding/workspace.tsx @@ -17,7 +17,7 @@ import { PrimaryButton } from "components/ui"; import { getFirstCharacters, truncateText } from "helpers/string.helper"; type Props = { - setStep: React.Dispatch>; + setStep: React.Dispatch>; setWorkspace: React.Dispatch>; user: ICurrentUserResponse | undefined; }; diff --git a/apps/app/components/workspace/create-workspace-form.tsx b/apps/app/components/workspace/create-workspace-form.tsx index fdc078cf0..507d88ea7 100644 --- a/apps/app/components/workspace/create-workspace-form.tsx +++ b/apps/app/components/workspace/create-workspace-form.tsx @@ -6,6 +6,7 @@ import { mutate } from "swr"; import { Controller, useForm } from "react-hook-form"; // services import workspaceService from "services/workspace.service"; +import userService from "services/user.service"; // hooks import useToast from "hooks/use-toast"; // ui @@ -77,7 +78,7 @@ export const CreateWorkspaceForm: React.FC = ({ message: "Workspace created successfully.", }); mutate(USER_WORKSPACES, (prevData) => [res, ...(prevData ?? [])]); - onSubmit(res); + updateLastWorkspaceIdUnderUSer(res); }) .catch((err) => { console.error(err); @@ -93,6 +94,18 @@ export const CreateWorkspaceForm: React.FC = ({ }); }; + // update last_workspace_id + const updateLastWorkspaceIdUnderUSer = (workspace: any) => { + userService + .updateUser({ last_workspace_id: workspace.id }) + .then((res) => { + onSubmit(workspace); + }) + .catch((err) => { + console.log(err); + }); + }; + useEffect( () => () => { // when the component unmounts set the default values to whatever user typed in diff --git a/apps/app/pages/onboarding.tsx b/apps/app/pages/onboarding.tsx index 59463c248..cc6b742df 100644 --- a/apps/app/pages/onboarding.tsx +++ b/apps/app/pages/onboarding.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useEffect, useState } from "react"; // next imports import Router from "next/router"; // services @@ -24,7 +24,7 @@ import { ONBOARDING_CARDS } from "constants/workspace"; import type { NextPage } from "next"; const Onboarding: NextPage = () => { - const [step, setStep] = useState(1); + const [step, setStep] = useState(null); const [userRole, setUserRole] = useState(null); const [isLoading, setIsLoading] = useState(false); @@ -32,113 +32,102 @@ const Onboarding: NextPage = () => { const { user, isLoading: userLoading, mutateUser } = useUserAuth("onboarding"); + useEffect(() => { + if (user && step === null) { + let currentStep: number = 1; + if (user?.role) currentStep = 2; + if (user?.last_workspace_id) currentStep = 4; + setStep(() => currentStep); + } + }, [step, user]); + return ( - {userLoading ? ( + {userLoading || isLoading || step === null ? (
) : ( - <> - {isLoading ? ( -
- +
+ {step <= 3 ? ( +
+
+ +
+ {step === 1 ? ( + + ) : step === 2 ? ( + + ) : ( + step === 3 && + )}
) : ( -
- {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(); +
+
+ {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]; + 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 + 1); - }} - > - {step === 4 || step === 8 ? "Get Started" : "Next"} - -
-
+ if (lastActiveWorkspace) { + mutateUser(); + 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}
)} - +
+ Logged in: + {user?.email} +
+
)} );