Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { observable, action, computed } from 'mobx';
- import { getUserProfile as getUserProfileApi, updatePersonalInfo } from '../../endpoints/users';
- import AuthStore from './Auth';
- import CountriesStore from '../App/Countries';
- import SnackbarAlert from '../Alerts/Snackbar';
- import { ACC_TYPE_MAPPINGS } from '../../consts/roles';
- import { translate } from './Localization';
- const resolveCountryLabel = country => (country ? country.label : '');
- export class User {
- @observable isLoadingUserInfo = false;
- @observable info = {};
- @observable adminSignature = null;
- @action async getUserProfile() {
- if (
- this.isLoadingUserInfo
- || !AuthStore.isLoggedIn()
- || (AuthStore.isLoggedIn() && Object.keys(this.info).length > 0)
- ) {
- return;
- }
- this.isLoadingUserInfo = true;
- return getUserProfileApi()
- .then(({ data }) => data)
- .then((info) => {
- this.info = info;
- this.isLoadingUserInfo = false;
- info.l && this.setAdminSignature(info.l);
- });
- }
- @action setEmail(email) {
- this.info.email = email;
- }
- @action setFirstName(firstName) {
- this.info.first_name = firstName;
- }
- @action setLastName(lastName) {
- this.info.last_name = lastName;
- }
- @action setCountry(country) {
- this.info.country = country;
- }
- @action setCountryId(countryId) {
- this.info.countryId = countryId;
- }
- @action setPassword(password) {
- this.info.password = password;
- }
- @action setConfirmPassword(password) {
- this.info.confirm_password = password;
- }
- @action setAdminSignature(signature) {
- this.adminSignature = signature;
- }
- @action clear() {
- this.info = {};
- this.adminSignature = null;
- }
- @action update() {
- const {
- id,
- first_name: firstName,
- last_name: lastName,
- countryId,
- password,
- confirm_password: confirmPass,
- } = this.info;
- const ctry = CountriesStore.countriesOptions.find(
- ({ value, label }) => value === countryId || label === countryId,
- );
- if (!ctry) {
- return Promise.reject(Error('Country is not valid'));
- }
- const { label } = ctry;
- const unlocalizedCountry = CountriesStore.unlocalize(label);
- return this.storeUpdatedUser({
- id,
- first_name: firstName,
- last_name: lastName,
- country: unlocalizedCountry ? unlocalizedCountry.countryName : label,
- password,
- confirm_password: confirmPass,
- })
- .then(() => SnackbarAlert.push(translate('snackbar.update_success'), 200))
- .then(() => this.invalidatePassword())
- .catch(() => SnackbarAlert.push(translate('snackbar.failed_user_info'), 400));
- }
- @action storeUpdatedUser(data) {
- return updatePersonalInfo(data);
- }
- @computed get countryLabel() {
- const localizedCountry = CountriesStore.localize(this.info.country);
- if (localizedCountry) {
- return localizedCountry.countryName;
- }
- const country = CountriesStore.countriesOptions.find(
- ({ value, label }) => value === this.info.country || label === this.info.country,
- );
- return resolveCountryLabel(country);
- }
- @computed get isAdmin() {
- return this.info.role === ACC_TYPE_MAPPINGS.corpAdmin;
- }
- @computed get isPlatformAdmin() {
- return this.info.role === ACC_TYPE_MAPPINGS.admin;
- }
- @computed get updateProfileData() {
- const countryUnlocalized = CountriesStore.unlocalize(this.countryLabel).countryName;
- return {
- first_name: this.info.first_name,
- last_name: this.info.last_name,
- email: this.info.email,
- country: countryUnlocalized,
- password: this.info.password,
- confirm_password: this.info.confirm_password,
- };
- }
- invalidatePassword() {
- this.info.password = null;
- this.info.confirm_password = null;
- }
- @computed get isCompanyUser() {
- return (
- [ACC_TYPE_MAPPINGS.corpAdmin, ACC_TYPE_MAPPINGS.corpRegular].includes(this.info.role)
- || this.isPlatformAdmin
- );
- }
- @computed get isIndividual() {
- return !this.isCompanyUser || this.isPlatformAdmin;
- }
- @computed get isPremium() {
- return this.info.isPremium;
- }
- }
- export default new User();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement