import { cleanError, manageError } from '../../errorManager';
import { isValid, isVoid } from '../../validation';
import Method from '../Method';
const formModelLogged = {
unsubButton: 'unsubscribe'
};
const formModelUnlogged = {
inputMsisdn: 'unsub_msisdn',
unsubButton: 'status_button',
unsubscribeUnlogged: 'unsubscribe_3g_unlogged'
};
/**
* @memberOf Useris
* @function smartstatus
*
* @description Manage the click on the unsubscription (to unsubscribe a user)
* and facebook (to add the facebook identity) buttons, calling the relative function.
*
* It must be used with the **smartstatus** 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 {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 Manage the success and error callback
* @param {function} [callbackObject.onUnsubLoggedSuccess='no-default']
* Unsubscriptions logged click callback (code managed by the app)
* @param {function} [callbackObject.onUnsubLoggedError='no-default']
* Unsubscriptions logged click error callback (code managed by the app)
* @param {function} [callbackObject.onUnsubUnloggedSuccess='no-default']
* Unsubscriptions unlogged click callback (code managed by the app)
* @param {function} [callbackObject.onUnsubUnloggedError='no-default']
* Unsubscriptions unlogged click error callback (code managed by the app)
* @param {function} [callbackObject.onTemplateLoaded='no-default']
* Callback executed when the template has been loaded.
*
* @example
* Useris.smartstatus({
* element: document.getElementById('elem'useris-username-error'entId'),
* access_token: abcdefgh1234567890,
* regexObject: {
* msidn: '',
* pin: '',
* email: '',
* password: '',
* generic: '',
* genericPassword: ''
* }, {
* onUnsubSuccess: function(){
* console.log();
* callToUnsubscribe();
* },
* onUnsubError: function(error){
* console.log('Error:', error);
* manageError(error);
* },
* onTemplateLoaded: function(tmplInfo){
* console.log('Template laoded', tmplInfo);
* }
* });
*/
export default class Smartstatus extends Method {
init = (options) => {
this.access_token = options.access_token;
this.smartsectionObj = {
subsection: 'smartstatus',
loadForm: options.element ? options.element : false,
access_token: options.access_token ? options.access_token : '',
doNotLoad: options.doNotLoad ? options.doNotLoad : false
};
this.initUserView();
};
attachEvents = () => {
if (this.access_token) {
const unsubButtonLogged = this.UserViewInstance.getElement(formModelLogged.unsubButton);
if (unsubButtonLogged) {
this.UserViewInstance.attachEvent(unsubButtonLogged, {
eventName: 'click',
event: (e) => {
e.preventDefault();
this.callback('onUnsubLoggedSuccess', {
IDENTITY: '',
TYPE: '',
ACCESS_TOKEN: this.access_token
});
}
});
}
} else { // User is not logged
const unsubButtonUnlogged = this.UserViewInstance
.getElement(formModelUnlogged.unsubButton);
const unsubscribeUnloggedButton = this.UserViewInstance
.getElement(formModelUnlogged.unsubscribeUnlogged);
if (unsubButtonUnlogged) {
this.UserViewInstance.attachEvent(unsubButtonUnlogged, {
eventName: 'click',
event: (e) => {
e.preventDefault();
cleanError();
const msisdn = this.UserViewInstance
.getValue(formModelUnlogged.inputMsisdn);
try {
if (isVoid(msisdn)) {
manageError('useris-error-required-input');
return;
}
if (!isValid(msisdn, this.regexObject.msisdn)) {
manageError('useris-error-invalid-number');
return;
}
this.callback('onUnsubUnloggedSuccess', {
IDENTITY: msisdn,
TYPE: 'msisdn',
ACCESS_TOKEN: ''
});
} catch (error) {
manageError(error);
}
}
});
} else if (unsubscribeUnloggedButton) {
/* Movistar case unlogged ... */
this.UserViewInstance.attachEvent(unsubscribeUnloggedButton, {
eventName: 'click',
event: (e) => {
e.preventDefault();
this.callback('onUnsubUnloggedSuccess', {
IDENTITY: '',
TYPE: '',
ACCESS_TOKEN: ''
});
}
});
}
}
}
}