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