From f4611be8c4b183a416310d8339188c784d847961 Mon Sep 17 00:00:00 2001 From: gurusainath Date: Thu, 2 May 2024 17:21:03 +0530 Subject: [PATCH] chore: updated store loader --- web/lib/wrappers/authentication-wrapper.tsx | 21 +++++---------- web/store/user/index.ts | 30 ++++++++++++--------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/web/lib/wrappers/authentication-wrapper.tsx b/web/lib/wrappers/authentication-wrapper.tsx index c695133ee..517afd9ad 100644 --- a/web/lib/wrappers/authentication-wrapper.tsx +++ b/web/lib/wrappers/authentication-wrapper.tsx @@ -27,24 +27,15 @@ export const AuthenticationWrapper: FC = observer((props const { children, pageType = EPageTypes.AUTHENTICATED } = props; // hooks const { isLoading: isUserLoading, data: currentUser, fetchCurrentUser } = useUser(); - const { isLoading: isUserProfileLoading, data: currentUserProfile, fetchUserProfile } = useUserProfile(); - const { isLoading: isUserSettingsLoading, data: currentUserSettings, fetchCurrentUserSettings } = useUserSettings(); - const { loader: isWorkspaceLoading, workspaces, fetchWorkspaces } = useWorkspace(); + const { data: currentUserProfile } = useUserProfile(); + const { data: currentUserSettings } = useUserSettings(); + const { workspaces } = useWorkspace(); - useSWR("USER_INFORMATION", async () => await fetchCurrentUser(), { + const { isLoading: isUserSWRLoading } = useSWR("USER_INFORMATION", async () => await fetchCurrentUser(), { revalidateOnFocus: false, shouldRetryOnError: false, }); - useSWR( - currentUser && currentUser?.id ? "USER_PROFILE_SETTINGS_INFORMATION" : null, - async () => { - if (currentUser && currentUser?.id) - await Promise.all([fetchUserProfile(), fetchCurrentUserSettings(), fetchWorkspaces()]); - }, - { revalidateOnFocus: false, shouldRetryOnError: false } - ); - const getWorkspaceRedirectionUrl = (): string => { let redirectionRoute = "/profile"; @@ -68,7 +59,7 @@ export const AuthenticationWrapper: FC = observer((props return redirectionRoute; }; - if (isUserLoading || isUserProfileLoading || isUserSettingsLoading || isWorkspaceLoading) + if (isUserSWRLoading || isUserLoading) return (
@@ -106,7 +97,7 @@ export const AuthenticationWrapper: FC = observer((props if (pageType === EPageTypes.AUTHENTICATED) { if (currentUser?.id) { - if (currentUserProfile?.id && currentUserProfile?.is_onboarded) return <>{children}; + if (currentUserProfile && currentUserProfile?.id && currentUserProfile?.is_onboarded) return <>{children}; else { router.push(`/onboarding`); return <>; diff --git a/web/store/user/index.ts b/web/store/user/index.ts index 2f00174a5..6f9be2dca 100644 --- a/web/store/user/index.ts +++ b/web/store/user/index.ts @@ -51,15 +51,12 @@ export class UserStore implements IUserStore { // service userService: UserService; authService: AuthService; - // root store - rootStore: RootStore; - constructor(rootStore: RootStore) { + constructor(private store: RootStore) { // stores - this.rootStore = rootStore; this.userProfile = new ProfileStore(); this.userSettings = new UserSettingsStore(); - this.membership = new UserMembershipStore(rootStore); + this.membership = new UserMembershipStore(store); // service this.userService = new UserService(); this.authService = new AuthService(); @@ -87,22 +84,29 @@ export class UserStore implements IUserStore { * @description fetches the current user * @returns {Promise} */ - fetchCurrentUser = async () => { + fetchCurrentUser = async (): Promise => { try { runInAction(() => { this.isLoading = true; this.error = undefined; }); const user = await this.userService.currentUser(); - if (user && user.id) { + if (user && user?.id) { await this.userProfile.fetchUserProfile(); await this.userSettings.fetchCurrentUserSettings(); + await this.store.workspaceRoot.fetchWorkspaces(); runInAction(() => { this.data = user; this.isLoading = false; this.isAuthenticated = true; }); - } + } else + runInAction(() => { + this.data = user; + this.isLoading = false; + this.isAuthenticated = false; + }); + console.log("this.isLoading", this.isLoading); return user; } catch (error) { runInAction(() => { @@ -122,7 +126,7 @@ export class UserStore implements IUserStore { * @param data * @returns {Promise} */ - updateCurrentUser = async (data: Partial) => { + updateCurrentUser = async (data: Partial): Promise => { const currentUserData = this.data; try { if (currentUserData) { @@ -154,17 +158,17 @@ export class UserStore implements IUserStore { * @description deactivates the current user * @returns {Promise} */ - deactivateAccount = async () => { + deactivateAccount = async (): Promise => { await this.userService.deactivateAccount(); - this.rootStore.resetOnSignOut(); + this.store.resetOnSignOut(); }; /** * @description signs out the current user * @returns {Promise} */ - signOut = async () => { + signOut = async (): Promise => { await this.authService.signOut(API_BASE_URL); - this.rootStore.resetOnSignOut(); + this.store.resetOnSignOut(); }; }