2023-08-11 11:48:33 +00:00
|
|
|
// mobx
|
|
|
|
import { observable, action, computed, makeObservable, runInAction } from "mobx";
|
|
|
|
// service
|
|
|
|
import UserService from "services/user.service";
|
2023-09-01 15:08:53 +00:00
|
|
|
// types
|
|
|
|
import { IUser } from "types/user";
|
2023-09-01 11:12:30 +00:00
|
|
|
|
|
|
|
export interface IUserStore {
|
2023-10-04 13:51:04 +00:00
|
|
|
loader: boolean;
|
|
|
|
error: any | null;
|
2023-09-01 11:12:30 +00:00
|
|
|
currentUser: any | null;
|
|
|
|
fetchCurrentUser: () => void;
|
2023-09-01 15:08:53 +00:00
|
|
|
currentActor: () => any;
|
2023-09-01 11:12:30 +00:00
|
|
|
}
|
2023-08-11 11:48:33 +00:00
|
|
|
|
|
|
|
class UserStore implements IUserStore {
|
2023-10-04 13:51:04 +00:00
|
|
|
loader: boolean = false;
|
|
|
|
error: any | null = null;
|
|
|
|
|
2023-09-01 15:08:53 +00:00
|
|
|
currentUser: IUser | null = null;
|
2023-08-11 11:48:33 +00:00
|
|
|
// root store
|
|
|
|
rootStore;
|
|
|
|
// service
|
|
|
|
userService;
|
|
|
|
|
|
|
|
constructor(_rootStore: any) {
|
|
|
|
makeObservable(this, {
|
|
|
|
// observable
|
2023-10-05 06:34:09 +00:00
|
|
|
loader: observable.ref,
|
|
|
|
error: observable.ref,
|
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
currentUser: observable.ref,
|
2023-08-11 11:48:33 +00:00
|
|
|
// actions
|
2023-09-01 11:12:30 +00:00
|
|
|
setCurrentUser: action,
|
2023-08-11 11:48:33 +00:00
|
|
|
// computed
|
2023-09-01 15:08:53 +00:00
|
|
|
currentActor: computed,
|
2023-08-11 11:48:33 +00:00
|
|
|
});
|
|
|
|
this.rootStore = _rootStore;
|
|
|
|
this.userService = new UserService();
|
|
|
|
}
|
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
setCurrentUser = (user: any) => {
|
2023-09-01 11:40:06 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.currentUser = { ...user };
|
|
|
|
});
|
2023-09-01 11:12:30 +00:00
|
|
|
};
|
|
|
|
|
2023-09-01 15:08:53 +00:00
|
|
|
get currentActor(): any {
|
|
|
|
return {
|
|
|
|
avatar: this.currentUser?.avatar,
|
|
|
|
display_name: this.currentUser?.display_name,
|
|
|
|
first_name: this.currentUser?.first_name,
|
|
|
|
id: this.currentUser?.id,
|
|
|
|
is_bot: false,
|
|
|
|
last_name: this.currentUser?.last_name,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param callback
|
|
|
|
* @description A wrapper function to check user authentication; it redirects to the login page if not authenticated, otherwise, it executes a callback.
|
|
|
|
* @example this.requiredLogin(() => { // do something });
|
|
|
|
*/
|
|
|
|
|
|
|
|
requiredLogin = (callback: () => void) => {
|
|
|
|
if (this.currentUser) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-05 10:42:17 +00:00
|
|
|
const currentPath = window.location.pathname + window.location.search;
|
2023-09-01 11:12:30 +00:00
|
|
|
this.fetchCurrentUser()
|
|
|
|
.then(() => {
|
2023-09-05 10:42:17 +00:00
|
|
|
if (!this.currentUser) window.location.href = `/?next_path=${currentPath}`;
|
|
|
|
else callback();
|
2023-09-01 11:12:30 +00:00
|
|
|
})
|
2023-09-05 10:42:17 +00:00
|
|
|
.catch(() => (window.location.href = `/?next_path=${currentPath}`));
|
2023-09-01 11:12:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fetchCurrentUser = async () => {
|
2023-08-11 11:48:33 +00:00
|
|
|
try {
|
2023-10-04 13:51:04 +00:00
|
|
|
this.loader = true;
|
|
|
|
this.error = null;
|
2023-09-01 11:12:30 +00:00
|
|
|
const response = await this.userService.currentUser();
|
2023-08-11 11:48:33 +00:00
|
|
|
if (response) {
|
|
|
|
runInAction(() => {
|
2023-10-04 13:51:04 +00:00
|
|
|
this.loader = false;
|
2023-08-11 11:48:33 +00:00
|
|
|
this.currentUser = response;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-09-01 11:12:30 +00:00
|
|
|
console.error("Failed to fetch current user", error);
|
2023-10-04 13:51:04 +00:00
|
|
|
this.loader = false;
|
|
|
|
this.error = error;
|
2023-08-11 11:48:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default UserStore;
|