chore: updated store loader

This commit is contained in:
gurusainath 2024-05-02 17:21:03 +05:30
parent e1b16208cb
commit f4611be8c4
2 changed files with 23 additions and 28 deletions

View File

@ -27,24 +27,15 @@ export const AuthenticationWrapper: FC<TAuthenticationWrapper> = observer((props
const { children, pageType = EPageTypes.AUTHENTICATED } = props; const { children, pageType = EPageTypes.AUTHENTICATED } = props;
// hooks // hooks
const { isLoading: isUserLoading, data: currentUser, fetchCurrentUser } = useUser(); const { isLoading: isUserLoading, data: currentUser, fetchCurrentUser } = useUser();
const { isLoading: isUserProfileLoading, data: currentUserProfile, fetchUserProfile } = useUserProfile(); const { data: currentUserProfile } = useUserProfile();
const { isLoading: isUserSettingsLoading, data: currentUserSettings, fetchCurrentUserSettings } = useUserSettings(); const { data: currentUserSettings } = useUserSettings();
const { loader: isWorkspaceLoading, workspaces, fetchWorkspaces } = useWorkspace(); const { workspaces } = useWorkspace();
useSWR("USER_INFORMATION", async () => await fetchCurrentUser(), { const { isLoading: isUserSWRLoading } = useSWR("USER_INFORMATION", async () => await fetchCurrentUser(), {
revalidateOnFocus: false, revalidateOnFocus: false,
shouldRetryOnError: 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 => { const getWorkspaceRedirectionUrl = (): string => {
let redirectionRoute = "/profile"; let redirectionRoute = "/profile";
@ -68,7 +59,7 @@ export const AuthenticationWrapper: FC<TAuthenticationWrapper> = observer((props
return redirectionRoute; return redirectionRoute;
}; };
if (isUserLoading || isUserProfileLoading || isUserSettingsLoading || isWorkspaceLoading) if (isUserSWRLoading || isUserLoading)
return ( return (
<div className="relative flex h-screen w-full items-center justify-center"> <div className="relative flex h-screen w-full items-center justify-center">
<Spinner /> <Spinner />
@ -106,7 +97,7 @@ export const AuthenticationWrapper: FC<TAuthenticationWrapper> = observer((props
if (pageType === EPageTypes.AUTHENTICATED) { if (pageType === EPageTypes.AUTHENTICATED) {
if (currentUser?.id) { if (currentUser?.id) {
if (currentUserProfile?.id && currentUserProfile?.is_onboarded) return <>{children}</>; if (currentUserProfile && currentUserProfile?.id && currentUserProfile?.is_onboarded) return <>{children}</>;
else { else {
router.push(`/onboarding`); router.push(`/onboarding`);
return <></>; return <></>;

View File

@ -51,15 +51,12 @@ export class UserStore implements IUserStore {
// service // service
userService: UserService; userService: UserService;
authService: AuthService; authService: AuthService;
// root store
rootStore: RootStore;
constructor(rootStore: RootStore) { constructor(private store: RootStore) {
// stores // stores
this.rootStore = rootStore;
this.userProfile = new ProfileStore(); this.userProfile = new ProfileStore();
this.userSettings = new UserSettingsStore(); this.userSettings = new UserSettingsStore();
this.membership = new UserMembershipStore(rootStore); this.membership = new UserMembershipStore(store);
// service // service
this.userService = new UserService(); this.userService = new UserService();
this.authService = new AuthService(); this.authService = new AuthService();
@ -87,22 +84,29 @@ export class UserStore implements IUserStore {
* @description fetches the current user * @description fetches the current user
* @returns {Promise<IUser>} * @returns {Promise<IUser>}
*/ */
fetchCurrentUser = async () => { fetchCurrentUser = async (): Promise<IUser> => {
try { try {
runInAction(() => { runInAction(() => {
this.isLoading = true; this.isLoading = true;
this.error = undefined; this.error = undefined;
}); });
const user = await this.userService.currentUser(); const user = await this.userService.currentUser();
if (user && user.id) { if (user && user?.id) {
await this.userProfile.fetchUserProfile(); await this.userProfile.fetchUserProfile();
await this.userSettings.fetchCurrentUserSettings(); await this.userSettings.fetchCurrentUserSettings();
await this.store.workspaceRoot.fetchWorkspaces();
runInAction(() => { runInAction(() => {
this.data = user; this.data = user;
this.isLoading = false; this.isLoading = false;
this.isAuthenticated = true; this.isAuthenticated = true;
}); });
} } else
runInAction(() => {
this.data = user;
this.isLoading = false;
this.isAuthenticated = false;
});
console.log("this.isLoading", this.isLoading);
return user; return user;
} catch (error) { } catch (error) {
runInAction(() => { runInAction(() => {
@ -122,7 +126,7 @@ export class UserStore implements IUserStore {
* @param data * @param data
* @returns {Promise<IUser>} * @returns {Promise<IUser>}
*/ */
updateCurrentUser = async (data: Partial<IUser>) => { updateCurrentUser = async (data: Partial<IUser>): Promise<IUser> => {
const currentUserData = this.data; const currentUserData = this.data;
try { try {
if (currentUserData) { if (currentUserData) {
@ -154,17 +158,17 @@ export class UserStore implements IUserStore {
* @description deactivates the current user * @description deactivates the current user
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
deactivateAccount = async () => { deactivateAccount = async (): Promise<void> => {
await this.userService.deactivateAccount(); await this.userService.deactivateAccount();
this.rootStore.resetOnSignOut(); this.store.resetOnSignOut();
}; };
/** /**
* @description signs out the current user * @description signs out the current user
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
signOut = async () => { signOut = async (): Promise<void> => {
await this.authService.signOut(API_BASE_URL); await this.authService.signOut(API_BASE_URL);
this.rootStore.resetOnSignOut(); this.store.resetOnSignOut();
}; };
} }