DocSpace-client/packages/common/store/UserStore.js
Ilya Oleshko 19e8b2210a Merge branch 'master' into develop
# Conflicts:
#	packages/client/src/helpers/constants.js
#	packages/client/src/pages/AccountsHome/Section/Filter/index.js
#	packages/client/src/pages/Home/InfoPanel/Body/sub-components/ItemTitle/AccountsItemTitle.js
#	packages/client/src/pages/Home/Section/AccountsBody/RowView/userContent.js
#	packages/client/src/pages/Home/Section/Header/index.js
#	packages/client/src/pages/Home/index.js
#	packages/client/src/pages/PortalSettings/categories/common/Customization/dns-settings.js
#	packages/client/src/pages/PortalSettings/categories/common/branding.js
#	packages/client/src/pages/PortalSettings/categories/security/access-portal/index.js
#	packages/client/src/store/FilesActionsStore.js
#	packages/common/components/Article/sub-components/article-payment-alert.js
#	packages/common/components/Section/index.js
#	packages/components/floating-button/styled-floating-button.js
#	packages/login/src/client/App.tsx
#	packages/login/src/client/components/Invalid/index.tsx
#	products/ASC.Files/Core/Core/FileStorageService.cs
#	products/ASC.Files/Core/Utils/FileConverter.cs
#	yarn.lock
2023-05-29 14:36:12 +03:00

131 lines
2.7 KiB
JavaScript

import React from "react";
import { toastr } from "@docspace/components";
import { makeAutoObservable, runInAction } from "mobx";
import { Trans } from "react-i18next";
import api from "../api";
import { EmployeeActivationStatus } from "../constants";
class UserStore {
user = null;
isLoading = false;
isLoaded = false;
userIsUpdate = false;
withSendAgain = true;
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);
return user;
};
init = async () => {
if (this.isLoaded) return;
this.setIsLoading(true);
try {
await this.loadCurrentUser();
} catch (e) {
console.error(e);
}
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);
};
updateEmailActivationStatus = async (activationStatus, userId, key) => {
this.setIsLoading(true);
const user = await api.people.updateActivationStatus(
activationStatus,
userId,
key
);
this.setUser(user);
this.setIsLoading(false);
};
changeTheme = async (key) => {
this.setIsLoading(true);
const { theme } = await api.people.changeTheme(key);
runInAction(() => {
this.user.theme = theme;
})
this.setIsLoading(false);
return theme;
};
setUserIsUpdate = (isUpdate) => {
//console.log("setUserIsUpdate");
this.userIsUpdate = isUpdate;
};
setWithSendAgain = (withSendAgain) => {
this.withSendAgain = withSendAgain;
};
sendActivationLink = async () => {
const { email, id } = this.user;
await api.people.resendUserInvites([id]);
return email;
};
updateAvatarInfo = (avatar, avatarSmall, avatarMedium, avatarMax) => {
this.user.avatar = avatar;
this.user.avatarSmall = avatarSmall;
this.user.avatarMedium = avatarMedium;
this.user.avatarMax = avatarMax;
};
get withActivationBar() {
return (
this.user &&
(this.user.activationStatus === EmployeeActivationStatus.Pending ||
this.user.activationStatus === EmployeeActivationStatus.NotActivated) &&
this.withSendAgain
);
}
get isAuthenticated() {
return !!this.user;
}
}
export default UserStore;