DocSpace-client/packages/asc-web-common/store/UserStore.js

62 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-02-03 12:42:47 +00:00
import { action, makeObservable, observable } from "mobx";
import api from "../api";
import { LANGUAGE } from "../constants";
class UserStore {
user = null;
2021-02-03 12:42:47 +00:00
isLoading = false;
isLoaded = false;
constructor() {
2021-02-01 11:01:09 +00:00
makeObservable(this, {
user: observable,
2021-02-03 12:42:47 +00:00
isLoading: observable,
isLoaded: observable,
2021-02-02 09:55:14 +00:00
getCurrentUser: action,
2021-02-03 12:42:47 +00:00
setIsLoading: action,
setIsLoaded: action,
setUser: action,
2021-02-01 11:01:09 +00:00
});
}
2021-02-02 09:55:14 +00:00
getCurrentUser = async () => {
const user = await api.people.getUser();
user.cultureName &&
localStorage.getItem(LANGUAGE) !== user.cultureName &&
localStorage.setItem(LANGUAGE, user.cultureName);
2021-02-03 12:42:47 +00:00
this.setUser(user);
};
init = async () => {
this.setIsLoading(true);
await this.getCurrentUser();
this.setIsLoading(false);
this.setIsLoaded(true);
};
setIsLoading = (isLoading) => {
this.isLoading = isLoading;
};
setIsLoaded = (isLoaded) => {
this.isLoaded = isLoaded;
};
setUser = (user) => {
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);
};
}
export default UserStore;