Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require('./style.scss');
- const lodash = require('lodash').noConflict();
- angular.module('app.routers.user').component('bfUserSettings', {
- bindings: {
- setTopBarTitle: '<',
- user: '<'
- },
- controller(
- $scope,
- $state,
- $q,
- bfTimeService,
- bfyUserService,
- Notification,
- bfyApi,
- SafeApply,
- gettextCatalog,
- bfyTime,
- LocaleService
- ) {
- 'ngInject';
- const $ctrl = this;
- $ctrl.$onInit = () => {
- $ctrl.setTopBarTitle(gettextCatalog.getString('User Settings'));
- };
- $ctrl.tabs = [
- {
- title: gettextCatalog.getString('account'),
- slug: 'account',
- enabled: true
- },
- {
- title: gettextCatalog.getString('email'),
- slug: 'email',
- enabled: true
- },
- {
- title: gettextCatalog.getString('privacy'),
- slug: 'privacy',
- enabled: true
- }
- ];
- $ctrl.onTabSelect = (selectedTab) => {
- $ctrl.selectedTab = selectedTab;
- };
- const selectedTab = $ctrl.tabs[0];
- $ctrl.onTabSelect(selectedTab);
- $ctrl.resendVerificationEmail = _.throttle(() => bfyUserService.resendVerification($ctrl.user.email)
- .then(() => Notification.show(gettextCatalog.getString('Verification Email Sent!'), {
- type: 'success',
- title: gettextCatalog.getString('Success!')
- }))
- .catch((error) => Notification.show(gettextCatalog.getString('Verification email could not be sent!'), {
- type: 'error',
- title: gettextCatalog.getString('Error')
- })), 10000);
- $ctrl.saving = false;
- $ctrl.$onChanges = (changesObj) => {};
- $ctrl.timezoneChanged = (newTimezone) => {
- $ctrl.newTimezone = newTimezone;
- startTime();
- };
- $ctrl.revertUserSettings = () => {
- $ctrl.newUsername = $ctrl.user.username;
- $ctrl.newTimezone = {zone: $ctrl.user.timezone};
- startTime();
- $ctrl.currentPassword = '';
- $ctrl.newPassword = '';
- $ctrl.confirmNewPassword = '';
- };
- function startTime() {
- bfTimeService.getTime()
- .then((time) => {
- if (time && $ctrl.newTimezone) {
- const dateTime = {
- date: time,
- time,
- zone: $ctrl.newTimezone.zone
- };
- $ctrl.currentTimeInZone = bfyTime.getLocal(dateTime).locale(LocaleService.getLocale()).format('h:mm A z');
- SafeApply($scope);
- }
- });
- }
- $ctrl.saveUserSettings = () => {
- $ctrl.saving = true;
- const savePromises = [
- saveTimezone(),
- saveUsername()
- ];
- if ($ctrl.canChangePassword) {
- savePromises.push(savePassword());
- }
- $q.all(savePromises)
- .then(() => {
- $ctrl.saving = false;
- return Notification.show(gettextCatalog.getString('Your account settings have been saved.'), {
- title: gettextCatalog.getString('Settings Saved')
- });
- })
- .catch(() => {
- $ctrl.saving = false;
- return Notification.show(gettextCatalog.getString('Your account settings could not be saved.'), {
- type: 'error'
- });
- });
- };
- function saveTimezone() {
- if ($ctrl.user.timezone !== $ctrl.newTimezone.zone) {
- mixpanel.track('Changed Timezone Value', {
- From: $ctrl.user.timezone,
- To: $ctrl.newTimezone.zone
- });
- return bfyApi.save('users', {
- _id: $ctrl.user._id,
- timezone: $ctrl.newTimezone.zone
- }).then(() => {
- $ctrl.user.timezone = $ctrl.newTimezone.zone;
- });
- }
- return $q.resolve();
- }
- function saveUsername() {
- if ($ctrl.usernameValid()) {
- mixpanel.track('Changed Username', {
- From: $ctrl.user.username,
- To: $ctrl.newUsername
- });
- return bfyApi.save('users', {
- _id: $ctrl.user._id,
- username: $ctrl.newUsername
- }).then(() => {
- $ctrl.user.username = $ctrl.newUsername;
- });
- }
- return $q.resolve();
- }
- function savePassword() {
- if ($ctrl.passwordsValid()) {
- return bfyApi.post('users/change-password', {
- currentPassword: $ctrl.currentPassword,
- newPassword: $ctrl.newPassword,
- confirmNewPassword: $ctrl.confirmNewPassword
- }).then(() => {
- $ctrl.currentPassword = '';
- $ctrl.newPassword = '';
- $ctrl.confirmNewPassword = '';
- $scope.currentPasswordForm.$setPristine();
- $scope.currentPasswordForm.$setUntouched();
- $scope.newPasswordForm.$setPristine();
- $scope.newPasswordForm.$setUntouched();
- $scope.confirmNewPasswordForm.$setPristine();
- $scope.confirmNewPasswordForm.$setUntouched();
- });
- }
- return $q.resolve();
- }
- $ctrl.usernameValid = () => {
- return $ctrl.newUsername && $ctrl.newUsername !== $ctrl.user.username && $ctrl.newUsername.length >= 2 && $ctrl.newUsername.length <= 40;
- };
- $ctrl.passwordsMatch = () => {
- return $ctrl.newPassword === $ctrl.confirmNewPassword;
- };
- $ctrl.passwordsValid = () => {
- return $ctrl.currentPassword && $ctrl.passwordsMatch();
- };
- },
- template: require('./template.html')
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement