methods/changePassword/index.js

import { cleanError, manageError } from '../../errorManager';
import { isValid, isVoid } from '../../validation';
import Method from '../Method';

const formModel = {
    buttonChange: 'frmChangePsw',
    buttonCancelChangePsw: 'frmChangePswReturn',
    oldPassword: 'OldPassword',
    password: 'password',
    oldPasswordSwitchEye: 'change_psw_old_password_switch_type',
    newPasswordSwitchEye: 'change_psw_password_switch_type'
};

/**
 * @memberOf Useris
 * @function changePassword
 *
 * @description Validate and recognize the object to make the user change its password.
 *
 * It must be used with the **newtonsmartchangepassword** template.
 *
 * @param {Object} options Options object
 * @param {Object} [options.element=false] The div element where the html's tmpl will be injected
 * @param {Object} [options.regexObject={}]
 * **Optionals** Contains all the regular expressions for input validation
 * (msisdn, pin, email, password and generic regex are allowed)
 * @param {String} [options.acccess_token='']
 * **Optionals** if true, the tmpl calls will be called with the params access_token filled
 * @param {Boolean} [options.doNotLoad=false] **Optionals** if true,
 * this params will tell useris to not load the template (It doesn't make the apicall)
 * @param {Object} callbackObject Manage the success and error callback
 * @param {function} [callbackObject.onSuccess='no-default']
 * Success callback (code managed by the app)
 * @param {function} [callbackObject.onCancel='no-default']
 * Cancel callback (code managed by the app)
 * @param {function} [callbackObject.onError='no-default']
 * Error callback (code managed by the app)
 * @param {function} [callbackObject.onTemplateLoaded='no-default']
 * Callback executed when the template has been loaded.
 *
 * @example
 *  Useris.changePassword({
 *      element: document.getElementById('elementId'),
 *  }, {
 *      onSuccess: function(userCredentials){
 *          console.log(userCredentials);
 *          callsToNewtonAdapter.changePassword(userCredentials);
 *      },
 *      onCancel: function(){
 *          window.history.back();
 *      },
 *      onError: function(error){
 *          console.log('Error:', error);
 *          manageError(error);
 *      },
 *      onTemplateLoaded: function(tmplInfo){
 *          console.log('Template loaded', tmplInfo);
 *      }
 *  });
 *
 * @returns {object}
 * <pre>
 *  {
 *      oldPassword: string,
 *      newPassword: string,
 *      repeatedPassword: string
 *  }
 * </pre>
 */
export default class ChangePassword extends Method {
    init = (options) => {
        this.smartsectionObj = {
            subsection: 'newtonsmartchangepassword',
            loadForm: options.element || false,
            access_token: options.access_token || '',
            doNotLoad: options.doNotLoad || false
        };
        this.initUserView();
    };

    attachEvents = () => {
        const changePasswordButton = this.UserViewInstance
            .getElement(formModel.buttonChange);
        const buttonCancelChangePsw = this.UserViewInstance
            .getElement(formModel.buttonCancelChangePsw);
        const oldPasswordSwitchEye = this.UserViewInstance
            .getElement(formModel.oldPasswordSwitchEye);
        const newPasswordSwitchEye = this.UserViewInstance
            .getElement(formModel.newPasswordSwitchEye);

        if (changePasswordButton) {
            this.UserViewInstance.attachEvent(changePasswordButton, {
                eventName: 'click',
                event: (e) => {
                    e.preventDefault();
                    cleanError();

                    const oldPassword = this.UserViewInstance.getValue(formModel.oldPassword);
                    const newPassword = this.UserViewInstance.getValue(formModel.password);

                    try {
                        if (isVoid(oldPassword)) {
                            manageError('useris-error-old-password');
                            return;
                        }

                        if (isVoid(newPassword)) {
                            manageError('useris-error-password');
                            return;
                        }

                        if (oldPassword === newPassword) {
                            manageError('useris-error-password-not-changed');
                            return;
                        }

                        if (!isValid(newPassword, this.regexObject.password)) {
                            manageError('useris-error-password');
                            return;
                        }
                        this.callback('onSuccess', {
                            oldPassword,
                            newPassword
                        });
                    } catch (error) {
                        manageError(error);
                    }
                }
            });
        }

        if (oldPasswordSwitchEye) {
            this.UserViewInstance.attachEvent(oldPasswordSwitchEye, {
                eventName: 'click',
                event: (e) => {
                    e.preventDefault();
                    this.UserViewInstance.switchInputType(
                        formModel.oldPassword, oldPasswordSwitchEye.id
                    );
                }
            });
        }

        if (newPasswordSwitchEye) {
            this.UserViewInstance.attachEvent(newPasswordSwitchEye, {
                eventName: 'click',
                event: (e) => {
                    e.preventDefault();
                    this.UserViewInstance
                        .switchInputType(formModel.password, newPasswordSwitchEye.id);
                }
            });
        }

        if (buttonCancelChangePsw) {
            this.UserViewInstance.attachEvent(buttonCancelChangePsw, {
                eventName: 'click',
                event: (e) => {
                    e.preventDefault();
                    this.callback('onCancel');
                }
            });
        }
    }
}