2024-05-08 17:31:20 +00:00
|
|
|
import set from "lodash/set";
|
|
|
|
import { action, computed, makeObservable, observable, runInAction } from "mobx";
|
|
|
|
// types
|
|
|
|
import { IUser } from "@plane/types";
|
|
|
|
// services
|
2024-05-14 08:56:54 +00:00
|
|
|
import { AuthService } from "@/services/auth.service";
|
2024-05-08 17:31:20 +00:00
|
|
|
import { UserService } from "@/services/user.service";
|
2024-05-15 10:58:38 +00:00
|
|
|
// store types
|
2024-05-14 08:56:54 +00:00
|
|
|
import { ProfileStore, IProfileStore } from "@/store/profile.store";
|
2024-05-15 10:58:38 +00:00
|
|
|
import { RootStore } from "@/store/root.store";
|
|
|
|
// types
|
2024-05-08 17:31:20 +00:00
|
|
|
import { ActorDetail } from "@/types/issue";
|
|
|
|
|
|
|
|
type TUserErrorStatus = {
|
|
|
|
status: string;
|
|
|
|
message: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface IUserStore {
|
|
|
|
// observables
|
|
|
|
isAuthenticated: boolean;
|
|
|
|
isLoading: boolean;
|
|
|
|
error: TUserErrorStatus | undefined;
|
|
|
|
data: IUser | undefined;
|
|
|
|
// store observables
|
2024-05-14 08:56:54 +00:00
|
|
|
profile: IProfileStore;
|
2024-05-08 17:31:20 +00:00
|
|
|
// computed
|
|
|
|
currentActor: ActorDetail;
|
|
|
|
// actions
|
|
|
|
fetchCurrentUser: () => Promise<IUser | undefined>;
|
|
|
|
updateCurrentUser: (data: Partial<IUser>) => Promise<IUser | undefined>;
|
2024-05-16 13:58:37 +00:00
|
|
|
hydrate: (data: IUser | undefined) => void;
|
2024-05-09 12:16:31 +00:00
|
|
|
reset: () => void;
|
2024-05-08 17:31:20 +00:00
|
|
|
signOut: () => Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class UserStore implements IUserStore {
|
|
|
|
// observables
|
|
|
|
isAuthenticated: boolean = false;
|
2024-05-15 22:33:43 +00:00
|
|
|
isLoading: boolean = true;
|
2024-05-08 17:31:20 +00:00
|
|
|
error: TUserErrorStatus | undefined = undefined;
|
|
|
|
data: IUser | undefined = undefined;
|
|
|
|
// store observables
|
2024-05-14 08:56:54 +00:00
|
|
|
profile: IProfileStore;
|
2024-05-08 17:31:20 +00:00
|
|
|
// service
|
|
|
|
userService: UserService;
|
|
|
|
authService: AuthService;
|
|
|
|
|
|
|
|
constructor(private store: RootStore) {
|
|
|
|
// stores
|
2024-05-14 08:56:54 +00:00
|
|
|
this.profile = new ProfileStore(store);
|
2024-05-08 17:31:20 +00:00
|
|
|
// 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,
|
2024-05-14 08:56:54 +00:00
|
|
|
profile: observable,
|
2024-05-08 17:31:20 +00:00
|
|
|
// computed
|
|
|
|
currentActor: computed,
|
|
|
|
// actions
|
|
|
|
fetchCurrentUser: action,
|
|
|
|
updateCurrentUser: action,
|
2024-05-09 12:16:31 +00:00
|
|
|
reset: action,
|
2024-05-08 17:31:20 +00:00
|
|
|
signOut: action,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// computed
|
|
|
|
get currentActor(): ActorDetail {
|
|
|
|
return {
|
|
|
|
id: this.data?.id,
|
|
|
|
first_name: this.data?.first_name,
|
|
|
|
last_name: this.data?.last_name,
|
|
|
|
display_name: this.data?.display_name,
|
|
|
|
avatar: this.data?.avatar || undefined,
|
|
|
|
is_bot: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// actions
|
|
|
|
/**
|
|
|
|
* @description fetches the current user
|
|
|
|
* @returns {Promise<IUser>}
|
|
|
|
*/
|
|
|
|
fetchCurrentUser = async (): Promise<IUser> => {
|
|
|
|
try {
|
|
|
|
runInAction(() => {
|
2024-05-16 13:58:37 +00:00
|
|
|
if (this.data === undefined) this.isLoading = true;
|
2024-05-08 17:31:20 +00:00
|
|
|
this.error = undefined;
|
|
|
|
});
|
|
|
|
const user = await this.userService.currentUser();
|
|
|
|
if (user && user?.id) {
|
2024-05-14 08:56:54 +00:00
|
|
|
await this.profile.fetchUserProfile();
|
2024-05-08 17:31:20 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.data = user;
|
|
|
|
this.isLoading = false;
|
|
|
|
this.isAuthenticated = true;
|
|
|
|
});
|
|
|
|
} else
|
|
|
|
runInAction(() => {
|
|
|
|
this.data = user;
|
|
|
|
this.isLoading = false;
|
|
|
|
this.isAuthenticated = false;
|
|
|
|
});
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-16 13:58:37 +00:00
|
|
|
hydrate = (data: IUser | undefined): void => {
|
|
|
|
if (!data) return;
|
2024-05-14 08:56:54 +00:00
|
|
|
this.data = { ...this.data, ...data };
|
|
|
|
};
|
|
|
|
|
2024-05-09 12:16:31 +00:00
|
|
|
/**
|
|
|
|
* @description resets the user store
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
reset = (): void => {
|
|
|
|
runInAction(() => {
|
|
|
|
this.isAuthenticated = false;
|
|
|
|
this.isLoading = false;
|
|
|
|
this.error = undefined;
|
|
|
|
this.data = undefined;
|
2024-05-14 08:56:54 +00:00
|
|
|
this.profile = new ProfileStore(this.store);
|
2024-05-09 12:16:31 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
/**
|
|
|
|
* @description signs out the current user
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
signOut = async (): Promise<void> => {
|
2024-05-17 11:24:58 +00:00
|
|
|
this.store.reset();
|
2024-05-08 17:31:20 +00:00
|
|
|
};
|
|
|
|
}
|