Advertisement
Guest User

Untitled

a guest
May 25th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. require('./style.scss');
  2.  
  3. const lodash = require('lodash').noConflict();
  4.  
  5. angular.module('app.routers.user').component('bfUserSettings', {
  6. bindings: {
  7. setTopBarTitle: '<',
  8. user: '<'
  9. },
  10. controller(
  11. $scope,
  12. $state,
  13. $q,
  14. bfTimeService,
  15. bfyUserService,
  16. Notification,
  17. bfyApi,
  18. SafeApply,
  19. gettextCatalog,
  20. bfyTime,
  21. LocaleService
  22. ) {
  23. 'ngInject';
  24.  
  25. const $ctrl = this;
  26.  
  27. $ctrl.$onInit = () => {
  28. $ctrl.setTopBarTitle(gettextCatalog.getString('User Settings'));
  29. };
  30.  
  31. $ctrl.tabs = [
  32. {
  33. title: gettextCatalog.getString('account'),
  34. slug: 'account',
  35. enabled: true
  36. },
  37. {
  38. title: gettextCatalog.getString('email'),
  39. slug: 'email',
  40. enabled: true
  41. },
  42. {
  43. title: gettextCatalog.getString('privacy'),
  44. slug: 'privacy',
  45. enabled: true
  46. }
  47. ];
  48.  
  49. $ctrl.onTabSelect = (selectedTab) => {
  50. $ctrl.selectedTab = selectedTab;
  51. };
  52.  
  53. const selectedTab = $ctrl.tabs[0];
  54. $ctrl.onTabSelect(selectedTab);
  55.  
  56. $ctrl.resendVerificationEmail = _.throttle(() => bfyUserService.resendVerification($ctrl.user.email)
  57. .then(() => Notification.show(gettextCatalog.getString('Verification Email Sent!'), {
  58. type: 'success',
  59. title: gettextCatalog.getString('Success!')
  60. }))
  61. .catch((error) => Notification.show(gettextCatalog.getString('Verification email could not be sent!'), {
  62. type: 'error',
  63. title: gettextCatalog.getString('Error')
  64. })), 10000);
  65.  
  66.  
  67.  
  68. $ctrl.saving = false;
  69. $ctrl.$onChanges = (changesObj) => {};
  70.  
  71. $ctrl.timezoneChanged = (newTimezone) => {
  72. $ctrl.newTimezone = newTimezone;
  73. startTime();
  74. };
  75.  
  76. $ctrl.revertUserSettings = () => {
  77. $ctrl.newUsername = $ctrl.user.username;
  78. $ctrl.newTimezone = {zone: $ctrl.user.timezone};
  79. startTime();
  80.  
  81. $ctrl.currentPassword = '';
  82. $ctrl.newPassword = '';
  83. $ctrl.confirmNewPassword = '';
  84. };
  85.  
  86. function startTime() {
  87. bfTimeService.getTime()
  88. .then((time) => {
  89. if (time && $ctrl.newTimezone) {
  90. const dateTime = {
  91. date: time,
  92. time,
  93. zone: $ctrl.newTimezone.zone
  94. };
  95. $ctrl.currentTimeInZone = bfyTime.getLocal(dateTime).locale(LocaleService.getLocale()).format('h:mm A z');
  96. SafeApply($scope);
  97. }
  98. });
  99. }
  100.  
  101. $ctrl.saveUserSettings = () => {
  102. $ctrl.saving = true;
  103.  
  104. const savePromises = [
  105. saveTimezone(),
  106. saveUsername()
  107. ];
  108. if ($ctrl.canChangePassword) {
  109. savePromises.push(savePassword());
  110. }
  111.  
  112. $q.all(savePromises)
  113. .then(() => {
  114. $ctrl.saving = false;
  115.  
  116. return Notification.show(gettextCatalog.getString('Your account settings have been saved.'), {
  117. title: gettextCatalog.getString('Settings Saved')
  118. });
  119. })
  120. .catch(() => {
  121. $ctrl.saving = false;
  122.  
  123. return Notification.show(gettextCatalog.getString('Your account settings could not be saved.'), {
  124. type: 'error'
  125. });
  126. });
  127. };
  128.  
  129. function saveTimezone() {
  130. if ($ctrl.user.timezone !== $ctrl.newTimezone.zone) {
  131. mixpanel.track('Changed Timezone Value', {
  132. From: $ctrl.user.timezone,
  133. To: $ctrl.newTimezone.zone
  134. });
  135.  
  136. return bfyApi.save('users', {
  137. _id: $ctrl.user._id,
  138. timezone: $ctrl.newTimezone.zone
  139. }).then(() => {
  140. $ctrl.user.timezone = $ctrl.newTimezone.zone;
  141. });
  142. }
  143.  
  144. return $q.resolve();
  145. }
  146.  
  147. function saveUsername() {
  148. if ($ctrl.usernameValid()) {
  149. mixpanel.track('Changed Username', {
  150. From: $ctrl.user.username,
  151. To: $ctrl.newUsername
  152. });
  153.  
  154. return bfyApi.save('users', {
  155. _id: $ctrl.user._id,
  156. username: $ctrl.newUsername
  157. }).then(() => {
  158. $ctrl.user.username = $ctrl.newUsername;
  159. });
  160. }
  161.  
  162. return $q.resolve();
  163. }
  164.  
  165. function savePassword() {
  166. if ($ctrl.passwordsValid()) {
  167. return bfyApi.post('users/change-password', {
  168. currentPassword: $ctrl.currentPassword,
  169. newPassword: $ctrl.newPassword,
  170. confirmNewPassword: $ctrl.confirmNewPassword
  171. }).then(() => {
  172. $ctrl.currentPassword = '';
  173. $ctrl.newPassword = '';
  174. $ctrl.confirmNewPassword = '';
  175.  
  176. $scope.currentPasswordForm.$setPristine();
  177. $scope.currentPasswordForm.$setUntouched();
  178. $scope.newPasswordForm.$setPristine();
  179. $scope.newPasswordForm.$setUntouched();
  180. $scope.confirmNewPasswordForm.$setPristine();
  181. $scope.confirmNewPasswordForm.$setUntouched();
  182. });
  183. }
  184.  
  185. return $q.resolve();
  186. }
  187.  
  188. $ctrl.usernameValid = () => {
  189. return $ctrl.newUsername && $ctrl.newUsername !== $ctrl.user.username && $ctrl.newUsername.length >= 2 && $ctrl.newUsername.length <= 40;
  190. };
  191.  
  192. $ctrl.passwordsMatch = () => {
  193. return $ctrl.newPassword === $ctrl.confirmNewPassword;
  194. };
  195.  
  196. $ctrl.passwordsValid = () => {
  197. return $ctrl.currentPassword && $ctrl.passwordsMatch();
  198. };
  199. },
  200. template: require('./template.html')
  201. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement