methods/ptChangePassword/index.js

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


const formModel = {
    changePasswordButton: 'useris-button-confirm',
    changePasswordInput: 'useris-input-password'
};

/**
 * @memberOf Useris
 * @function ptChangePassword
 *
 * @description Validate and expose function for account creation portugal
 *
 * It must be used with the **newtonsmartchangepswmsisdn** template;
 *
 * @param {Object} options Options object
 * @param {Object} [options.element=false] The div element where the html's tmpl will be injected
 * @param {String} [options.access_token=''] **Optionals** if true,
 * the tmpl calls will be called with the params access_token filled
 * @param {String} [options.contentId=''] **Optionals** content_id of smartsection
 * @param {String} [options.returnUrl=''] **Optionals** return url to call for php
 * @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 Object containg all the callbacks
 * @param {function} [callbackObject.onTemplateLoaded='no-default']
 * Callback executed when the template has been loaded.
 * @param {function} [callbackObject.onChangePassword='no-default']
 * Callback executed when the click on button.
 *
 * @example
 *  Useris.ptChangePassword({
 *      element: document.getElementById('elementId'),
 *      access_token: 'abcdefgh1234567890',
 *      contentId: 0,
 *      returnUrl: 'http://www.google.com'
 *  }, {
 *      onTemplateLoaded: function(tmplInfo){
 *          console.log('Template loaded', tmplInfo);
 *      },
 *      onChangePassword: function(){
 *          //chhange password code
 *          changePassword();
 *      }
 *  });
 *
 *  @returns {object}
 *  <pre>
 *      {}
 *  </pre>
 */
export default class PtChangePassword extends Method {
    init = (options) => {
        this.smartsectionObj = {
            subsection: 'newtonsmartchangepswmsisdn',
            loadForm: options.element || false,
            access_token: options.access_token || '',
            returnUrl: options.returnUrl || '',
            contentId: options.contentId || '',
            doNotLoad: options.doNotLoad || false
        };
        this.initUserView();
    };

    attachEvents = () => {
        let changePasswordButton = false;
        let changePasswordInput = false;

        changePasswordButton = this.UserViewInstance.getElement(formModel.changePasswordButton);
        changePasswordInput = this.UserViewInstance.getElement(formModel.changePasswordInput);

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

                    const newPassword = this.UserViewInstance
                        .getValue(formModel.changePasswordInput);

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

                    this.callback('onChangePassword', {
                        newPassword
                    });
                }
            });
        }
    }
}