2022-10-04 03:30:36 +00:00
|
|
|
import React from "react";
|
|
|
|
import { toastr } from "@docspace/components";
|
2023-04-11 08:47:08 +00:00
|
|
|
import { makeAutoObservable, runInAction } from "mobx";
|
2022-10-04 03:30:36 +00:00
|
|
|
import { Trans } from "react-i18next";
|
2021-02-01 08:45:26 +00:00
|
|
|
import api from "../api";
|
2022-10-04 03:30:36 +00:00
|
|
|
import { EmployeeActivationStatus } from "../constants";
|
2021-02-01 08:45:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-10-17 11:43:04 +00:00
|
|
|
withSendAgain = true;
|
|
|
|
|
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 () => {
|
2022-07-25 13:14:24 +00:00
|
|
|
let user = null;
|
2022-07-26 07:56:25 +00:00
|
|
|
if (window?.__ASC_INITIAL_EDITOR_STATE__?.user)
|
|
|
|
user = window.__ASC_INITIAL_EDITOR_STATE__.user;
|
2022-07-25 13:14:24 +00:00
|
|
|
else user = await api.people.getUser();
|
|
|
|
|
2021-02-03 12:42:47 +00:00
|
|
|
this.setUser(user);
|
2023-04-12 21:01:37 +00:00
|
|
|
|
|
|
|
return user;
|
2021-02-03 12:42:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-12-27 10:45:47 +00:00
|
|
|
try {
|
|
|
|
await this.loadCurrentUser();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
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
|
|
|
|
2023-03-06 10:20:59 +00:00
|
|
|
updateEmailActivationStatus = async (activationStatus, userId, key) => {
|
|
|
|
this.setIsLoading(true);
|
|
|
|
|
|
|
|
const user = await api.people.updateActivationStatus(
|
|
|
|
activationStatus,
|
|
|
|
userId,
|
|
|
|
key
|
|
|
|
);
|
|
|
|
|
|
|
|
this.setUser(user);
|
|
|
|
this.setIsLoading(false);
|
|
|
|
};
|
|
|
|
|
2022-04-21 07:31:32 +00:00
|
|
|
changeTheme = async (key) => {
|
|
|
|
this.setIsLoading(true);
|
|
|
|
|
|
|
|
const { theme } = await api.people.changeTheme(key);
|
|
|
|
|
2023-04-11 08:47:08 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.user.theme = theme;
|
|
|
|
})
|
2022-04-21 07:31:32 +00:00
|
|
|
|
|
|
|
this.setIsLoading(false);
|
|
|
|
|
|
|
|
return theme;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2022-10-17 11:43:04 +00:00
|
|
|
setWithSendAgain = (withSendAgain) => {
|
|
|
|
this.withSendAgain = withSendAgain;
|
2022-10-04 03:30:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
sendActivationLink = (t) => {
|
|
|
|
const { email, id } = this.user;
|
|
|
|
|
2023-03-02 13:47:33 +00:00
|
|
|
return api.people.resendUserInvites([id]).then(() => {
|
|
|
|
toastr.success(
|
|
|
|
<Trans
|
|
|
|
i18nKey="MessageEmailActivationInstuctionsSentOnEmail"
|
|
|
|
ns="People"
|
|
|
|
t={t}
|
|
|
|
>
|
|
|
|
The email activation instructions have been sent to the
|
|
|
|
<strong>{{ email: email }}</strong> email address
|
|
|
|
</Trans>
|
|
|
|
);
|
|
|
|
});
|
2022-10-04 03:30:36 +00:00
|
|
|
};
|
|
|
|
|
2023-04-12 21:01:37 +00:00
|
|
|
updateAvatarInfo = (avatar, avatarSmall, avatarMedium, avatarMax) => {
|
|
|
|
this.user.avatar = avatar;
|
|
|
|
this.user.avatarSmall = avatarSmall;
|
|
|
|
this.user.avatarMedium = avatarMedium;
|
|
|
|
this.user.avatarMax = avatarMax;
|
|
|
|
};
|
|
|
|
|
2022-10-04 03:30:36 +00:00
|
|
|
get withActivationBar() {
|
|
|
|
return (
|
|
|
|
this.user &&
|
2023-03-02 07:47:12 +00:00
|
|
|
(this.user.activationStatus === EmployeeActivationStatus.Pending ||
|
|
|
|
this.user.activationStatus === EmployeeActivationStatus.NotActivated) &&
|
2022-10-17 11:43:04 +00:00
|
|
|
this.withSendAgain
|
2022-10-04 03:30:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-10 12:52:36 +00:00
|
|
|
get isAuthenticated() {
|
|
|
|
return !!this.user;
|
|
|
|
}
|
2021-02-01 08:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default UserStore;
|