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;
// 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<TAuthenticationWrapper> = observer((props
return redirectionRoute;
};
if (isUserLoading || isUserProfileLoading || isUserSettingsLoading || isWorkspaceLoading)
if (isUserSWRLoading || isUserLoading)
return (
<div className="relative flex h-screen w-full items-center justify-center">
<Spinner />
@ -106,7 +97,7 @@ export const AuthenticationWrapper: FC<TAuthenticationWrapper> = 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 <></>;

View File

@ -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<IUser>}
*/
fetchCurrentUser = async () => {
fetchCurrentUser = async (): Promise<IUser> => {
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<IUser>}
*/
updateCurrentUser = async (data: Partial<IUser>) => {
updateCurrentUser = async (data: Partial<IUser>): Promise<IUser> => {
const currentUserData = this.data;
try {
if (currentUserData) {
@ -154,17 +158,17 @@ export class UserStore implements IUserStore {
* @description deactivates the current user
* @returns {Promise<void>}
*/
deactivateAccount = async () => {
deactivateAccount = async (): Promise<void> => {
await this.userService.deactivateAccount();
this.rootStore.resetOnSignOut();
this.store.resetOnSignOut();
};
/**
* @description signs out the current user
* @returns {Promise<void>}
*/
signOut = async () => {
signOut = async (): Promise<void> => {
await this.authService.signOut(API_BASE_URL);
this.rootStore.resetOnSignOut();
this.store.resetOnSignOut();
};
}