mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
175 lines
5.0 KiB
TypeScript
175 lines
5.0 KiB
TypeScript
import set from "lodash/set";
|
|
import { action, makeObservable, observable, runInAction } from "mobx";
|
|
// types
|
|
import { IUser } from "@plane/types";
|
|
// helpers
|
|
import { API_BASE_URL } from "@/helpers/common.helper";
|
|
// services
|
|
import { AuthService } from "@/services/auth.service";
|
|
import { UserService } from "@/services/user.service";
|
|
// stores
|
|
import { RootStore } from "@/store/root.store";
|
|
import { IAccountStore } from "@/store/user/account.store";
|
|
import { ProfileStore, IUserProfileStore } from "@/store/user/profile.store";
|
|
import { IUserMembershipStore, UserMembershipStore } from "@/store/user/user-membership.store";
|
|
import { IUserSettingsStore, UserSettingsStore } from "./settings.store";
|
|
|
|
type TUserErrorStatus = {
|
|
status: string;
|
|
message: string;
|
|
};
|
|
|
|
export interface IUserStore {
|
|
// observables
|
|
isAuthenticated: boolean;
|
|
isLoading: boolean;
|
|
error: TUserErrorStatus | undefined;
|
|
data: IUser | undefined;
|
|
// store observables
|
|
userProfile: IUserProfileStore;
|
|
userSettings: IUserSettingsStore;
|
|
accounts: Record<string, IAccountStore>;
|
|
membership: IUserMembershipStore;
|
|
// actions
|
|
fetchCurrentUser: () => Promise<IUser | undefined>;
|
|
updateCurrentUser: (data: Partial<IUser>) => Promise<IUser | undefined>;
|
|
deactivateAccount: () => Promise<void>;
|
|
signOut: () => Promise<void>;
|
|
}
|
|
|
|
export class UserStore implements IUserStore {
|
|
// observables
|
|
isAuthenticated: boolean = false;
|
|
isLoading: boolean = false;
|
|
error: TUserErrorStatus | undefined = undefined;
|
|
data: IUser | undefined = undefined;
|
|
// store observables
|
|
userProfile: IUserProfileStore;
|
|
userSettings: IUserSettingsStore;
|
|
accounts: Record<string, IAccountStore> = {};
|
|
membership: IUserMembershipStore;
|
|
// service
|
|
userService: UserService;
|
|
authService: AuthService;
|
|
|
|
constructor(private store: RootStore) {
|
|
// stores
|
|
this.userProfile = new ProfileStore();
|
|
this.userSettings = new UserSettingsStore();
|
|
this.membership = new UserMembershipStore(store);
|
|
// service
|
|
this.userService = new UserService();
|
|
this.authService = new AuthService();
|
|
// observables
|
|
makeObservable(this, {
|
|
// observables
|
|
isAuthenticated: observable.ref,
|
|
isLoading: observable.ref,
|
|
error: observable,
|
|
// model observables
|
|
data: observable,
|
|
userProfile: observable,
|
|
userSettings: observable,
|
|
accounts: observable,
|
|
membership: observable,
|
|
// actions
|
|
fetchCurrentUser: action,
|
|
updateCurrentUser: action,
|
|
deactivateAccount: action,
|
|
signOut: action,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description fetches the current user
|
|
* @returns {Promise<IUser>}
|
|
*/
|
|
fetchCurrentUser = async (): Promise<IUser> => {
|
|
try {
|
|
runInAction(() => {
|
|
this.isLoading = true;
|
|
this.error = undefined;
|
|
});
|
|
const user = await this.userService.currentUser();
|
|
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(() => {
|
|
this.isLoading = false;
|
|
this.isAuthenticated = false;
|
|
this.error = {
|
|
status: "user-fetch-error",
|
|
message: "Failed to fetch current user",
|
|
};
|
|
});
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @description updates the current user
|
|
* @param data
|
|
* @returns {Promise<IUser>}
|
|
*/
|
|
updateCurrentUser = async (data: Partial<IUser>): Promise<IUser> => {
|
|
const currentUserData = this.data;
|
|
try {
|
|
if (currentUserData) {
|
|
Object.keys(data).forEach((key: string) => {
|
|
const userKey: keyof IUser = key as keyof IUser;
|
|
if (this.data) set(this.data, userKey, data[userKey]);
|
|
});
|
|
}
|
|
const user = await this.userService.updateUser(data);
|
|
return user;
|
|
} catch (error) {
|
|
if (currentUserData) {
|
|
Object.keys(currentUserData).forEach((key: string) => {
|
|
const userKey: keyof IUser = key as keyof IUser;
|
|
if (this.data) set(this.data, userKey, currentUserData[userKey]);
|
|
});
|
|
}
|
|
runInAction(() => {
|
|
this.error = {
|
|
status: "user-update-error",
|
|
message: "Failed to update current user",
|
|
};
|
|
});
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @description deactivates the current user
|
|
* @returns {Promise<void>}
|
|
*/
|
|
deactivateAccount = async (): Promise<void> => {
|
|
await this.userService.deactivateAccount();
|
|
this.store.resetOnSignOut();
|
|
};
|
|
|
|
/**
|
|
* @description signs out the current user
|
|
* @returns {Promise<void>}
|
|
*/
|
|
signOut = async (): Promise<void> => {
|
|
await this.authService.signOut(API_BASE_URL);
|
|
this.store.resetOnSignOut();
|
|
};
|
|
}
|