Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1.  
  2. import { observable, action, computed } from 'mobx';
  3. import { getUserProfile as getUserProfileApi, updatePersonalInfo } from '../../endpoints/users';
  4. import AuthStore from './Auth';
  5. import CountriesStore from '../App/Countries';
  6. import SnackbarAlert from '../Alerts/Snackbar';
  7. import { ACC_TYPE_MAPPINGS } from '../../consts/roles';
  8. import { translate } from './Localization';
  9.  
  10. const resolveCountryLabel = country => (country ? country.label : '');
  11.  
  12. export class User {
  13. @observable isLoadingUserInfo = false;
  14.  
  15. @observable info = {};
  16.  
  17. @observable adminSignature = null;
  18.  
  19. @action async getUserProfile() {
  20. if (
  21. this.isLoadingUserInfo
  22. || !AuthStore.isLoggedIn()
  23. || (AuthStore.isLoggedIn() && Object.keys(this.info).length > 0)
  24. ) {
  25. return;
  26. }
  27.  
  28. this.isLoadingUserInfo = true;
  29. return getUserProfileApi()
  30. .then(({ data }) => data)
  31. .then((info) => {
  32. this.info = info;
  33. this.isLoadingUserInfo = false;
  34. info.l && this.setAdminSignature(info.l);
  35. });
  36. }
  37.  
  38. @action setEmail(email) {
  39. this.info.email = email;
  40. }
  41.  
  42. @action setFirstName(firstName) {
  43. this.info.first_name = firstName;
  44. }
  45.  
  46. @action setLastName(lastName) {
  47. this.info.last_name = lastName;
  48. }
  49.  
  50. @action setCountry(country) {
  51. this.info.country = country;
  52. }
  53.  
  54. @action setCountryId(countryId) {
  55. this.info.countryId = countryId;
  56. }
  57.  
  58. @action setPassword(password) {
  59. this.info.password = password;
  60. }
  61.  
  62. @action setConfirmPassword(password) {
  63. this.info.confirm_password = password;
  64. }
  65.  
  66. @action setAdminSignature(signature) {
  67. this.adminSignature = signature;
  68. }
  69.  
  70. @action clear() {
  71. this.info = {};
  72. this.adminSignature = null;
  73. }
  74.  
  75. @action update() {
  76. const {
  77. id,
  78. first_name: firstName,
  79. last_name: lastName,
  80. countryId,
  81. password,
  82. confirm_password: confirmPass,
  83. } = this.info;
  84. const ctry = CountriesStore.countriesOptions.find(
  85. ({ value, label }) => value === countryId || label === countryId,
  86. );
  87. if (!ctry) {
  88. return Promise.reject(Error('Country is not valid'));
  89. }
  90.  
  91. const { label } = ctry;
  92.  
  93. const unlocalizedCountry = CountriesStore.unlocalize(label);
  94.  
  95. return this.storeUpdatedUser({
  96. id,
  97. first_name: firstName,
  98. last_name: lastName,
  99. country: unlocalizedCountry ? unlocalizedCountry.countryName : label,
  100. password,
  101. confirm_password: confirmPass,
  102. })
  103. .then(() => SnackbarAlert.push(translate('snackbar.update_success'), 200))
  104. .then(() => this.invalidatePassword())
  105. .catch(() => SnackbarAlert.push(translate('snackbar.failed_user_info'), 400));
  106. }
  107.  
  108. @action storeUpdatedUser(data) {
  109. return updatePersonalInfo(data);
  110. }
  111.  
  112. @computed get countryLabel() {
  113. const localizedCountry = CountriesStore.localize(this.info.country);
  114. if (localizedCountry) {
  115. return localizedCountry.countryName;
  116. }
  117.  
  118. const country = CountriesStore.countriesOptions.find(
  119. ({ value, label }) => value === this.info.country || label === this.info.country,
  120. );
  121.  
  122. return resolveCountryLabel(country);
  123. }
  124.  
  125. @computed get isAdmin() {
  126. return this.info.role === ACC_TYPE_MAPPINGS.corpAdmin;
  127. }
  128.  
  129. @computed get isPlatformAdmin() {
  130. return this.info.role === ACC_TYPE_MAPPINGS.admin;
  131. }
  132.  
  133. @computed get updateProfileData() {
  134. const countryUnlocalized = CountriesStore.unlocalize(this.countryLabel).countryName;
  135. return {
  136. first_name: this.info.first_name,
  137. last_name: this.info.last_name,
  138. email: this.info.email,
  139. country: countryUnlocalized,
  140. password: this.info.password,
  141. confirm_password: this.info.confirm_password,
  142. };
  143. }
  144.  
  145. invalidatePassword() {
  146. this.info.password = null;
  147. this.info.confirm_password = null;
  148. }
  149.  
  150. @computed get isCompanyUser() {
  151. return (
  152. [ACC_TYPE_MAPPINGS.corpAdmin, ACC_TYPE_MAPPINGS.corpRegular].includes(this.info.role)
  153. || this.isPlatformAdmin
  154. );
  155. }
  156.  
  157. @computed get isIndividual() {
  158. return !this.isCompanyUser || this.isPlatformAdmin;
  159. }
  160.  
  161. @computed get isPremium() {
  162. return this.info.isPremium;
  163. }
  164. }
  165.  
  166. export default new User();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement