2021-11-10 12:52:36 +00:00
|
|
|
import { makeAutoObservable } from "mobx";
|
2021-02-01 08:45:26 +00:00
|
|
|
import api from "../api";
|
|
|
|
|
|
|
|
class UserStore {
|
|
|
|
user = null;
|
2021-02-03 12:42:47 +00:00
|
|
|
isLoading = false;
|
|
|
|
isLoaded = false;
|
2021-08-11 07:18:10 +00:00
|
|
|
userIsUpdate = false;
|
2021-02-01 08:45:26 +00:00
|
|
|
|
|
|
|
constructor() {
|
2021-11-10 12:52:36 +00:00
|
|
|
makeAutoObservable(this);
|
2021-02-01 08:45:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 12:52:36 +00:00
|
|
|
loadCurrentUser = async () => {
|
2021-02-01 08:45:26 +00:00
|
|
|
const user = await api.people.getUser();
|
2021-02-03 12:42:47 +00:00
|
|
|
this.setUser(user);
|
|
|
|
};
|
|
|
|
|
|
|
|
init = async () => {
|
2021-11-10 12:52:36 +00:00
|
|
|
if (this.isLoaded) return;
|
|
|
|
|
2021-02-03 12:42:47 +00:00
|
|
|
this.setIsLoading(true);
|
|
|
|
|
2021-11-10 12:52:36 +00:00
|
|
|
await this.loadCurrentUser();
|
2021-02-03 12:42:47 +00:00
|
|
|
|
|
|
|
this.setIsLoading(false);
|
|
|
|
this.setIsLoaded(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
setIsLoading = (isLoading) => {
|
|
|
|
this.isLoading = isLoading;
|
|
|
|
};
|
|
|
|
|
|
|
|
setIsLoaded = (isLoaded) => {
|
|
|
|
this.isLoaded = isLoaded;
|
|
|
|
};
|
|
|
|
|
|
|
|
setUser = (user) => {
|
2021-02-01 08:45:26 +00:00
|
|
|
this.user = user;
|
2021-02-02 09:55:14 +00:00
|
|
|
};
|
2021-02-15 19:43:07 +00:00
|
|
|
|
|
|
|
changeEmail = async (userId, email, key) => {
|
|
|
|
this.setIsLoading(true);
|
|
|
|
|
|
|
|
const user = await api.people.changeEmail(userId, email, key);
|
|
|
|
|
|
|
|
this.setUser(user);
|
|
|
|
this.setIsLoading(false);
|
|
|
|
};
|
2021-08-11 07:18:10 +00:00
|
|
|
|
|
|
|
setUserIsUpdate = (isUpdate) => {
|
2021-09-06 09:30:58 +00:00
|
|
|
//console.log("setUserIsUpdate");
|
2021-08-11 07:18:10 +00:00
|
|
|
this.userIsUpdate = isUpdate;
|
|
|
|
};
|
2021-11-10 12:52:36 +00:00
|
|
|
|
|
|
|
get isAuthenticated() {
|
|
|
|
return !!this.user;
|
|
|
|
}
|
2021-02-01 08:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default UserStore;
|