methods/ptAddPassword/index.js

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


const formModel = {
    addPasswordButton: 'useris-button-confirm',
    addPasswordInput: 'useris-input-password'
};

/**
 * @memberOf Useris
 * @function ptAddPassword
 *
 * @description Validate and expose function for account creation portugal
 *
 * It must be used with the **newtonsmartaddpswmsisdn** 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.onAddPassword='no-default']
 * Callback executed when the click on button.
 *
 * @example
 *  Useris.ptAddPassword({
 *      element: document.getElementById('elementId'),
 *      access_token: 'abcdefgh1234567890',
 *      contentId: 0,
 *      returnUrl: 'http://www.google.com'
 *  }, {
 *      onTemplateLoaded: function(tmplInfo){
 *          console.log('Template loaded', tmplInfo);
 *      },
 *      onAddPassword: function(){
 *          //add password code
 *          addpsw();
 *      }
 *  });
 *
 *  @returns {object}
 *  <pre>
 *      {}
 *  </pre>
 */
export default class PtAddPassword extends Method {
    init = (options) => {
        this.smartsectionObj = {
            subsection: 'newtonsmartaddpswmsisdn',
            loadForm: options.element || false,
            access_token: options.access_token || '',
            returnUrl: options.returnUrl || '',
            contentId: options.contentId || '',
            doNotLoad: options.doNotLoad || false
        };
        this.initUserView();
    };

    attachEvents = () => {
        let addPasswordButton = false;
        let addPasswordInput = false;

        addPasswordButton = this.UserViewInstance.getElement(formModel.addPasswordButton);
        addPasswordInput = this.UserViewInstance.getElement(formModel.addPasswordInput);

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

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

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

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