1/**
2 * Controller for the profile settings page
3 *
4 * @module app/profile-settings/controllers/index
5 * @exports ProfileSettingsController
6 * @name ProfileSettingsController
7 */
8
9window.angular && (function(angular) {
10  'use strict';
11
12  angular.module('app.profileSettings')
13      .controller('profileSettingsController', [
14        '$scope', 'APIUtils', 'dataService', 'toastService',
15        function($scope, APIUtils, dataService, toastService) {
16          $scope.username;
17          $scope.minPasswordLength;
18          $scope.maxPasswordLength;
19          $scope.password;
20          $scope.passwordConfirm;
21
22          /**
23           * Make API call to update user password
24           * @param {string} password
25           */
26          const updatePassword = function(password) {
27            $scope.loading = true;
28            APIUtils.updateUser($scope.username, null, password)
29                .then(
30                    () => toastService.success(
31                        'Password has been updated successfully.'))
32                .catch((error) => {
33                  console.log(JSON.stringify(error));
34                  toastService.error('Unable to update password.')
35                })
36                .finally(() => {
37                  $scope.password = '';
38                  $scope.passwordConfirm = '';
39                  $scope.form.$setPristine();
40                  $scope.form.$setUntouched();
41                  $scope.loading = false;
42                })
43          };
44
45          /**
46           * API call to get account settings for min/max
47           * password length requirement
48           */
49          const getAllUserAccountProperties = function() {
50            APIUtils.getAllUserAccountProperties().then((accountSettings) => {
51              $scope.minPasswordLength = accountSettings.MinPasswordLength;
52              $scope.maxPasswordLength = accountSettings.MaxPasswordLength;
53            })
54          };
55
56          /**
57           * Callback after form submitted
58           */
59          $scope.onSubmit = function(form) {
60            if (form.$valid) {
61              const password = form.password.$viewValue;
62              updatePassword(password);
63            }
64          };
65
66          /**
67           * Callback after view loaded
68           */
69          $scope.$on('$viewContentLoaded', () => {
70            getAllUserAccountProperties();
71            $scope.username = dataService.getUser();
72          });
73        }
74      ]);
75})(angular);
76