import { action, observable, runInAction, makeObservable } from "mobx";
import { IUser } from "@plane/types";
// helpers
import { EUserStatus, TUserStatus } from "@/helpers";
// services
import { AuthService } from "@/services/auth.service";
import { UserService } from "@/services/user.service";
// root store
import { RootStore } from "@/store/root.store";
export interface IUserStore {
// observables
isLoading: boolean;
userStatus: TUserStatus | undefined;
isUserLoggedIn: boolean | undefined;
currentUser: IUser | undefined;
// fetch actions
hydrate: (data: any) => void;
fetchCurrentUser: () => Promise<IUser>;
reset: () => void;
signOut: () => void;
}
export class UserStore implements IUserStore {
isLoading: boolean = true;
userStatus: TUserStatus | undefined = undefined;
isUserLoggedIn: boolean | undefined = undefined;
currentUser: IUser | undefined = undefined;
userService;
authService;
constructor(private store: RootStore) {
makeObservable(this, {
isLoading: observable.ref,
userStatus: observable,
isUserLoggedIn: observable.ref,
currentUser: observable,
// action
fetchCurrentUser: action,
reset: action,
signOut: action,
});
this.userService = new UserService();
this.authService = new AuthService();
hydrate = (data: any) => {
if (data) this.currentUser = data;
};
/**
* @description Fetches the current user
* @returns Promise<IUser>
*/
fetchCurrentUser = async () => {
try {
if (this.currentUser === undefined) this.isLoading = true;
const currentUser = await this.userService.currentUser();
if (currentUser) {
await this.store.instance.fetchInstanceAdmins();
runInAction(() => {
this.isUserLoggedIn = true;
this.currentUser = currentUser;
this.isLoading = false;
} else {
this.isUserLoggedIn = false;
this.currentUser = undefined;
return currentUser;
} catch (error: any) {
if (error.status === 403)
this.userStatus = {
status: EUserStatus.AUTHENTICATION_NOT_DONE,
message: error?.message || "",
else
status: EUserStatus.ERROR,
throw error;
reset = async () => {
this.userStatus = undefined;
signOut = async () => {
this.store.resetOnSignOut();