All files / src errorManager.js

7.69% Statements 2/26
0% Branches 0/22
0% Functions 0/8
7.69% Lines 2/26
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 96 97 98 99      15x                                     15x                                                                                                                                                        
import UserView from './userView';
import global from './global';
 
const arrayError = [
    'useris-error-empty-terms',
    'useris-error-pin',
    'useris-error-msg',
    'useris-error-op',
    'useris-error-password',
    'useris-error-password2',
    'useris-error-old-password',
    'useris-error-repeated-password',
    'useris-error-password-not-changed',
    'useris-error-password-match',
    'useris-error-password-not-valid',
    'useris-error-newton-error',
    'useris-username-error',
    'useris-nickname-not-changed',
    'useris-error-required-input',
    'useris-error-invalid-number'
];
 
const UserViewInstance = new UserView();
 
export function manageErrorWithClass(error) {
    global.logger.log('ErrorManager: ', error);
    let errorName = '';
 
    if (error) {
        errorName = error.message || error;
    }
 
    if (errorName && arrayError.indexOf(errorName) >= 0) {
        UserViewInstance.showElementByClass(errorName);
    }
}
 
export function manageErrorInContainer(error, containerId) {
    global.logger.log('ErrorManager: ', 'Container Error:', `${error} in ${containerId}`);
 
    let errorName = '';
 
    if (error) {
        errorName = error.message || error;
    }
 
    if (errorName && arrayError.indexOf(errorName) >= 0) {
        UserViewInstance.showElementByClassInContainer(errorName, containerId);
    }
}
 
export function cleanError() {
    arrayError.forEach((singleError) => {
        UserViewInstance.hideElementByClass(singleError);
    });
}
 
export function cleanErrorEvo() {
    let errorElements = [];
    arrayError.forEach((singleError) => {
        errorElements = UserViewInstance.getElementByQuerySelectorAll(`.${singleError}`);
        if (errorElements) {
            errorElements.forEach((element) => {
                if (element.style) {
                    element.style.display = 'none';
                }
            });
        }
    });
}
 
/**
 * @memberOf Useris
 * @function triggerError
 *
 * @description Expose the useris error manager
 * to the web application to show error or warning message.
 *
 * @param {string} error div to show for the error
 *
 * @example
 *  Useris.triggerError('useris-error-newton-error');
 */
export function manageError(error, containerId) {
    if (containerId) {
        manageErrorInContainer(error, containerId);
    } else {
        manageErrorWithClass(error);
    }
}
 
export default {
    manageError,
    manageErrorInContainer,
    manageErrorWithClass,
    cleanError,
    cleanErrorEvo
};