methods/signup/index.js

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

const formModel = {
    buttonSignup: 'frmSignup',
    buttonFbSignup: 'signup_fb_connect',
    email: 'email',
    switchEye: 'add_mail_password_switch_type',
    password: 'password',
    container: 'smartsignup-container'
};

/**
 * @memberOf Useris
 * @function signup
 *
 * @description Validate and recognize the object to add a new free newton identity.
 *
 * Remember to check if an identy is already present.
 * If yes you have to delete the old and add the new one
 *
 * It must be used with the **smartsignup** 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 {boolean} [options.ohterParams=0] params to substitute for mail
 * @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.onFbSignupSucces='no-default']
 * Facebook function to signup with fb identity (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.signup({
 *      element: document.getElementById('elementId'),
 *      regexObject: {
 *          msidn: '',
 *          pin: '',
 *          email: '',
 *          password: '',
 *          generic: '',
 *          genericPassword: ''
 *      },
 *      access_token: '',
 *      doNotLoad: false,
 *      otherParams: '{}'
 *  }, {
 *      onSuccess: function(userCredentials){
 *          // remember to check if an identy is already present.
 *          // If yes you have to delete the old and add the new one
 *          callsToNewton.signup(userCredentials);
 *      },
 *      onFbSignupSuccess: function(userCredentials){
 *          // fb code
 *          callsToNewton.signup(userCredentials);
 *      },
 *      onError: function(error){
 *          console.log('Error:', error);
 *          manageError(error);
 *      },
 *      onTemplateLoaded: function(tmplInfo){
 *          console.log('Template loaded', tmplInfo);
 *      }
 *  });
 *
 * @returns {object}
 * **Email case:**
 * <pre>
 *     {
 *         email: string,
 *         password: string
 *     }
 * </pre>
 */
export default class Signup extends Method {
    init = (options) => {
        this.smartsectionObj = {
            subsection: 'smartsignup',
            loadForm: options.element || false,
            access_token: options.access_token || '',
            doNotLoad: options.doNotLoad || false
        };
        this.initUserView();
    };

    attachEvents = () => {
        const buttonSignup = this.UserViewInstance.getElement(formModel.buttonSignup);
        const buttonFbSignup = this.UserViewInstance.getElement(formModel.buttonFbSignup);
        const switchEye = this.UserViewInstance.getElement(formModel.switchEye);

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

                const email = this.UserViewInstance.getValue('email');
                const password = this.UserViewInstance.getValue('password');

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

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

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

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

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