methods/resetPassword/index.js

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

const formModel = {
    password: 'password',
    resetButton: 'frmResetEmailPsw',
    switchEye: 'reset_psw_password_switch_type',
    token: 'token',
    email: 'email'
};

/**
 * @memberOf Useris
 * @function resetPassword
 *
 * @description Validate and recognize the object to reset a user password.
 *
 *  It must be used with the **newtonsmartresetpsw** template, it works only for email identity.
 *
 * @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 {string} [options.token=''] Token present as url params inside the reset link
 * @param {string} [options.email=''] Email present as url params inside the reset link
 * @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.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.resetPassword({
 *      element: document.getElementById('elementId'),
 *      regexObject: {
 *          msidn: '',
 *          pin: '',
 *          email: '',
 *          password: '',
 *          generic: '',
 *          genericPassword: ''
 *      },
 *      token: '123459325',
 *      email: 'test@email.com'
 *  }, {
 *      onSuccess: function(userCredentials){
 *          console.log(userCredentials);
 *          callsToNewtonAdapter.resetPassword(userCredentials);
 *      },
 *      onError: function(error){
 *          console.log('Error:', error);
 *          manageError(error);
 *      },
 *      onTemplateLoaded: function(tmplInfo){
 *          console.log('Template loaded', tmplInfo);
 *      }
 *  });
 *
 * @returns {object}
 * <pre>
 *     {
 *         password: string,
 *         token: url_params.token,
 *         email: url_params.mail
 *     }
 * </pre>
 */
export default class ResetPassword extends Method {
    init = (options) => {
        this.token = options.token;
        this.email = options.email;
        this.smartsectionObj = {
            email: this.email,
            token: this.token,
            subsection: 'newtonsmartresetpsw',
            loadForm: options.element || false,
            access_token: options.access_token || '',
            doNotLoad: options.doNotLoad || false
        };
        if (this.token && this.email) {
            this.initUserView();
        } else {
            throw new Error('resetPassword without token or/and email');
        }
    };

    attachEvents = () => {
        this.UserViewInstance.setValue(formModel.token, this.token);
        this.UserViewInstance.setValue(formModel.email, this.email);

        const resetButton = this.UserViewInstance.getElement(formModel.resetButton);
        const switchEye = this.UserViewInstance.getElement(formModel.switchEye);

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

                const password = this.UserViewInstance.getValue(formModel.password);
                this.token = this.UserViewInstance.getValue(formModel.token);

                try {
                    if (isVoid(password) || !isValid(password, this.regexObject.password)) {
                        manageError('useris-error-password');
                        return;
                    }

                    this.callback('onSuccess', {
                        password,
                        email: this.email,
                        token: this.token
                    });
                } catch (error) {
                    manageError(error);
                }
            }
        });

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