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

122 lines
2.4 KiB
JavaScript
Raw Normal View History

import React from "react";
import { toastr } from "@docspace/components";
import { makeAutoObservable } from "mobx";
import { Trans } from "react-i18next";
import api from "../api";
import { EmployeeActivationStatus } from "../constants";
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;
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();
2021-02-03 12:42:47 +00:00
this.setUser(user);
};
init = async () => {
if (this.isLoaded) return;
2021-02-03 12:42:47 +00:00
this.setIsLoading(true);
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) => {
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
changeTheme = async (key) => {
this.setIsLoading(true);
const { theme } = await api.people.changeTheme(key);
this.user.theme = theme;
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;
};
setWithSendAgain = (withSendAgain) => {
this.withSendAgain = withSendAgain;
};
sendActivationLink = (t) => {
const { email, id } = this.user;
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>
);
})
.finally(() => {
this.setWithSendAgain(false);
});
};
get withActivationBar() {
return (
this.user &&
this.user.activationStatus === EmployeeActivationStatus.Pending &&
this.withSendAgain
);
}
get isAuthenticated() {
return !!this.user;
}
}
export default UserStore;