All files / src/methods/ptAddPassword index.js

87.5% Statements 14/16
68.75% Branches 11/16
100% Functions 3/3
87.5% Lines 14/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95          1x                                                                                                   1x               1x       1x 1x   1x 1x   1x 1x     1x 1x   1x   1x         1x              
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);
 
        Eif (addPasswordButton && addPasswordInput) {
            this.UserViewInstance.attachEvent(addPasswordButton, {
                eventName: 'click',
                event: (e) => {
                    e.preventDefault();
                    cleanError();
 
                    const newPassword = this.UserViewInstance.getValue(formModel.addPasswordInput);
 
                    Iif (isVoid(newPassword)) {
                        manageError('useris-error-msg');
                        return;
                    }
 
                    this.callback('onAddPassword', {
                        newPassword
                    });
                }
            });
        }
    }
}