DocSpace-buildtools/packages/common/store/UserStore.js
Alexey Safronov 53737a06e8 Merge branch 'develop' into refactoring/client
# Conflicts:
#	packages/editor/src/client/components/DynamicComponent.js
#	packages/editor/src/client/components/Editor.js
#	packages/editor/src/client/components/SelectFileDialog.js
#	packages/editor/src/client/components/SelectFolderDialog.js
#	packages/editor/src/client/components/SharingDialog.js
#	packages/editor/src/client/helpers/utils.js
#	packages/editor/src/client/helpers/withDialogs.js
#	packages/editor/src/server/lib/helpers/index.js
2022-07-28 10:38:29 +03:00

78 lines
1.4 KiB
JavaScript

import { makeAutoObservable } from "mobx";
import api from "../api";
class UserStore {
user = null;
isLoading = false;
isLoaded = false;
userIsUpdate = false;
constructor() {
makeAutoObservable(this);
}
loadCurrentUser = async () => {
let user = null;
if (window?.__ASC_INITIAL_EDITOR_STATE__?.user)
user = window.__ASC_INITIAL_EDITOR_STATE__.user;
else user = await api.people.getUser();
this.setUser(user);
};
init = async () => {
if (this.isLoaded) return;
this.setIsLoading(true);
await this.loadCurrentUser();
this.setIsLoading(false);
this.setIsLoaded(true);
};
setIsLoading = (isLoading) => {
this.isLoading = isLoading;
};
setIsLoaded = (isLoaded) => {
this.isLoaded = isLoaded;
};
setUser = (user) => {
this.user = user;
};
changeEmail = async (userId, email, key) => {
this.setIsLoading(true);
const user = await api.people.changeEmail(userId, email, key);
this.setUser(user);
this.setIsLoading(false);
};
changeTheme = async (key) => {
this.setIsLoading(true);
const { theme } = await api.people.changeTheme(key);
this.user.theme = theme;
this.setIsLoading(false);
return theme;
};
setUserIsUpdate = (isUpdate) => {
//console.log("setUserIsUpdate");
this.userIsUpdate = isUpdate;
};
get isAuthenticated() {
return !!this.user;
}
}
export default UserStore;