summaryrefslogtreecommitdiffstats
path: root/app/profile-settings/controllers/profile-settings-controller.js
blob: 404e05545d42f4c95f7761fc1c28cd7a0f2cf067 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
 * Controller for the profile settings page
 *
 * @module app/profile-settings/controllers/index
 * @exports ProfileSettingsController
 * @name ProfileSettingsController
 */

window.angular && (function(angular) {
  'use strict';

  angular.module('app.profileSettings')
      .controller('profileSettingsController', [
        '$scope', 'APIUtils', 'dataService', 'toastService',
        function($scope, APIUtils, dataService, toastService) {
          $scope.username;
          $scope.minPasswordLength;
          $scope.maxPasswordLength;
          $scope.password;
          $scope.passwordConfirm;

          /**
           * Make API call to update user password
           * @param {string} password
           */
          const updatePassword = function(password) {
            $scope.loading = true;
            APIUtils.updateUser($scope.username, null, password)
                .then(
                    () => toastService.success(
                        'Password has been updated successfully.'))
                .catch((error) => {
                  console.log(JSON.stringify(error));
                  toastService.error('Unable to update password.')
                })
                .finally(() => {
                  $scope.password = '';
                  $scope.passwordConfirm = '';
                  $scope.form.$setPristine();
                  $scope.form.$setUntouched();
                  $scope.loading = false;
                })
          };

          /**
           * API call to get account settings for min/max
           * password length requirement
           */
          const getAllUserAccountProperties = function() {
            APIUtils.getAllUserAccountProperties().then((accountSettings) => {
              $scope.minPasswordLength = accountSettings.MinPasswordLength;
              $scope.maxPasswordLength = accountSettings.MaxPasswordLength;
            })
          };

          /**
           * Callback after form submitted
           */
          $scope.onSubmit = function(form) {
            if (form.$valid) {
              const password = form.password.$viewValue;
              updatePassword(password);
            }
          };

          /**
           * Callback after view loaded
           */
          $scope.$on('$viewContentLoaded', () => {
            getAllUserAccountProperties();
            $scope.username = dataService.getUser();
          });
        }
      ]);
})(angular);
OpenPOWER on IntegriCloud