2023-08-08 07:20:27 +00:00
|
|
|
// mobx
|
2023-08-10 07:33:42 +00:00
|
|
|
import { action, observable, computed, runInAction, makeObservable } from "mobx";
|
2023-08-08 07:20:27 +00:00
|
|
|
// services
|
|
|
|
import UserService from "services/user.service";
|
|
|
|
// interfaces
|
2023-08-21 10:41:08 +00:00
|
|
|
import { IUser } from "types/users";
|
2023-08-16 11:20:39 +00:00
|
|
|
import { IRootStore } from "./root";
|
2023-08-08 07:20:27 +00:00
|
|
|
|
2023-08-14 07:44:28 +00:00
|
|
|
export interface IUserStore {
|
2023-08-21 10:41:08 +00:00
|
|
|
// observable
|
|
|
|
isLoggingIn: boolean;
|
|
|
|
currentUser: IUser | null;
|
|
|
|
// action
|
|
|
|
setLoggingIn: (isLoggingIn: boolean) => void;
|
|
|
|
setCurrentUser: (currentUser: IUser | null) => void;
|
|
|
|
loadUserDetailsAsync: () => void;
|
|
|
|
updateCurrentUserAsync: (user: Partial<IUser>) => void;
|
|
|
|
// store
|
|
|
|
rootStore: IRootStore;
|
2023-08-14 07:44:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 07:20:27 +00:00
|
|
|
class UserStore {
|
2023-08-21 10:41:08 +00:00
|
|
|
isLoggingIn: boolean = false;
|
|
|
|
currentUser: IUser | null = null;
|
2023-08-08 07:20:27 +00:00
|
|
|
// root store
|
2023-08-16 11:20:39 +00:00
|
|
|
rootStore: IRootStore;
|
2023-08-08 07:20:27 +00:00
|
|
|
|
2023-08-16 11:20:39 +00:00
|
|
|
constructor(_rootStore: IRootStore) {
|
2023-08-08 07:20:27 +00:00
|
|
|
makeObservable(this, {
|
|
|
|
// observable
|
2023-08-21 10:41:08 +00:00
|
|
|
isLoggingIn: observable,
|
2023-08-08 07:20:27 +00:00
|
|
|
currentUser: observable.ref,
|
|
|
|
// action
|
2023-08-21 10:41:08 +00:00
|
|
|
setLoggingIn: action,
|
|
|
|
setCurrentUser: action,
|
|
|
|
loadUserDetailsAsync: action,
|
|
|
|
updateCurrentUserAsync: action,
|
2023-08-08 07:20:27 +00:00
|
|
|
// computed
|
|
|
|
});
|
2023-08-21 10:41:08 +00:00
|
|
|
// store
|
2023-08-08 07:20:27 +00:00
|
|
|
this.rootStore = _rootStore;
|
2023-08-21 10:41:08 +00:00
|
|
|
|
|
|
|
// init load
|
2023-08-08 07:20:27 +00:00
|
|
|
this.initialLoad();
|
|
|
|
}
|
|
|
|
|
2023-08-21 10:41:08 +00:00
|
|
|
setLoggingIn = (_isLoggingIn: boolean) => {
|
|
|
|
this.isLoggingIn = _isLoggingIn;
|
|
|
|
};
|
|
|
|
|
|
|
|
setCurrentUser = (_currentUser: IUser | null) => {
|
|
|
|
this.currentUser = _currentUser;
|
|
|
|
};
|
|
|
|
|
|
|
|
loadUserDetailsAsync = async () => {
|
2023-08-08 07:20:27 +00:00
|
|
|
try {
|
2023-08-16 11:20:39 +00:00
|
|
|
let userResponse: IUser | null = await UserService.currentUser();
|
2023-08-08 07:20:27 +00:00
|
|
|
userResponse = userResponse || null;
|
2023-08-21 10:41:08 +00:00
|
|
|
if (userResponse)
|
2023-08-10 07:33:42 +00:00
|
|
|
runInAction(() => {
|
2023-08-21 10:41:08 +00:00
|
|
|
this.setCurrentUser(userResponse);
|
|
|
|
this.setLoggingIn(true);
|
2023-08-10 07:33:42 +00:00
|
|
|
});
|
2023-08-21 10:41:08 +00:00
|
|
|
return {};
|
2023-08-08 07:20:27 +00:00
|
|
|
} catch (error) {
|
2023-08-21 10:41:08 +00:00
|
|
|
console.error("Fetching user error", error);
|
|
|
|
return error;
|
2023-08-08 07:20:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-21 10:41:08 +00:00
|
|
|
updateCurrentUserAsync = async (user: Partial<IUser>) => {
|
2023-08-08 07:20:27 +00:00
|
|
|
try {
|
2023-08-21 10:41:08 +00:00
|
|
|
const userResponse: IUser = await UserService.updateUser(user);
|
2023-08-08 07:20:27 +00:00
|
|
|
if (userResponse) {
|
2023-08-21 10:41:08 +00:00
|
|
|
this.loadUserDetailsAsync();
|
2023-08-08 07:20:27 +00:00
|
|
|
}
|
2023-08-21 10:41:08 +00:00
|
|
|
return {};
|
2023-08-08 07:20:27 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error("Updating user error", error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// init load
|
2023-08-10 07:33:42 +00:00
|
|
|
initialLoad() {}
|
2023-08-08 07:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default UserStore;
|