From 725c9375ea6c534970b906ab3208f94a596f15b8 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal <65252264+aaryan610@users.noreply.github.com> Date: Tue, 11 Apr 2023 12:25:21 +0530 Subject: [PATCH] fix: onboarding loop (#775) * fix: dashboard workspace activity mutation * fix: onboarding loop --- apps/app/contexts/user.context.tsx | 6 +++--- apps/app/pages/onboarding.tsx | 17 ++++++++++++++++- apps/app/services/user.service.ts | 9 +++++++-- apps/app/types/users.d.ts | 7 +++++++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/apps/app/contexts/user.context.tsx b/apps/app/contexts/user.context.tsx index 6aa98d3cc..3f076b326 100644 --- a/apps/app/contexts/user.context.tsx +++ b/apps/app/contexts/user.context.tsx @@ -7,12 +7,12 @@ import userService from "services/user.service"; // constants import { CURRENT_USER } from "constants/fetch-keys"; // types -import type { IUser } from "types"; +import type { ICurrentUserResponse, IUser } from "types"; interface IUserContextProps { user?: IUser; isUserLoading: boolean; - mutateUser: KeyedMutator; + mutateUser: KeyedMutator; assignedIssuesLength?: number; workspaceInvitesLength?: number; } @@ -21,7 +21,7 @@ export const UserContext = createContext({} as IUserContextPr export const UserProvider = ({ children }: { children: ReactElement }) => { // API to fetch user information - const { data, error, mutate } = useSWR(CURRENT_USER, () => userService.currentUser(), { + const { data, error, mutate } = useSWR(CURRENT_USER, () => userService.currentUser(), { shouldRetryOnError: false, }); diff --git a/apps/app/pages/onboarding.tsx b/apps/app/pages/onboarding.tsx index 59913b9f0..86f6846cc 100644 --- a/apps/app/pages/onboarding.tsx +++ b/apps/app/pages/onboarding.tsx @@ -24,6 +24,7 @@ import Logo from "public/onboarding/logo.svg"; import type { NextPage } from "next"; // fetch-keys import { CURRENT_USER } from "constants/fetch-keys"; +import { ICurrentUserResponse } from "types"; const Onboarding: NextPage = () => { const [step, setStep] = useState(1); @@ -76,7 +77,21 @@ const Onboarding: NextPage = () => { userService .updateUserOnBoard({ userRole }) .then(() => { - mutate(CURRENT_USER); + mutate( + CURRENT_USER, + (prevData) => { + if (!prevData) return prevData; + + return { + ...prevData, + user: { + ...prevData.user, + is_onboarded: true, + }, + }; + }, + false + ); router.push("/"); }) .catch((err) => { diff --git a/apps/app/services/user.service.ts b/apps/app/services/user.service.ts index b035ecf8e..5f0786b81 100644 --- a/apps/app/services/user.service.ts +++ b/apps/app/services/user.service.ts @@ -2,7 +2,12 @@ import APIService from "services/api.service"; import trackEventServices from "services/track-event.service"; -import type { IUser, IUserActivityResponse, IUserWorkspaceDashboard } from "types"; +import type { + ICurrentUserResponse, + IUser, + IUserActivityResponse, + IUserWorkspaceDashboard, +} from "types"; const { NEXT_PUBLIC_API_BASE_URL } = process.env; @@ -29,7 +34,7 @@ class UserService extends APIService { }); } - async currentUser(): Promise { + async currentUser(): Promise { return this.get("/api/users/me/") .then((response) => response?.data) .catch((error) => { diff --git a/apps/app/types/users.d.ts b/apps/app/types/users.d.ts index 15c84aaaf..2d90c8b57 100644 --- a/apps/app/types/users.d.ts +++ b/apps/app/types/users.d.ts @@ -15,6 +15,7 @@ export interface IUser { last_location: readonly string; created_location: readonly string; is_email_verified: boolean; + is_onboarded: boolean; token: string; role: string; @@ -26,6 +27,12 @@ export interface IUser { [...rest: string]: any; } +export interface ICurrentUserResponse { + assigned_issues: number; + user: IUser; + workspace_invites: number; +} + export interface IUserLite { readonly id: string; first_name: string;