All files / src/parts ajaxHelper.js

0% Statements 0/41
0% Branches 0/20
0% Functions 0/4
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 100 101                                                                                                                                                                                                         
/**
 * Collection of ajax helper
 */
import * as globals from './globalMethods';
import notifyFader from './notifyFader';
 
const onSuccess = (response) => {
    
    // Check type of response and take action accordingly
    if (response == '') {
        console.error('No response from server');
        notifyFader.create('No response from server', 'alert-danger');
        return false;
    }
 
    if (!response.loggedIn) {
        // Hide any modals that might be open
        $('.modal').modal('hide');
        $('#ajax-helper-modal .modal-content').html(response.html);
        $('#ajax-helper-modal').modal('show');
        return false;
    }
 
    // No permission
    if (!response.hasPermission) {
        notifyFader(response.noPermissionText, 'well-lg bg-danger text-center');
        return false;
    }
 
    // Error popup
    if (response.error) {
        notifyFader(response.error.message, 'well-lg bg-danger text-center');
        return false;
    }
 
    // Put HTML into element.
    if (response.outputType == 'jsonoutputhtml') {
        $('#' + response.target).html(response.html);
        globals.globalWindowMethods.doToolTip();
        return;
    }
 
    // Success popup
    if (response.success) {
        notifyFader(response.success, 'well-lg bg-primary text-center');
    }
 
    // Modal popup
    if (response.html) {
        $('#ajax-helper-modal .modal-content').html(response.html);
        $('#ajax-helper-modal').modal('show');
    }
 
    return true;
};
 
/**
* Like $.ajax, but with checks for errors,
* permission etc. Should be used together
* with the PHP AjaxHelper.
* @todo Handle error from server (500)?
* @param {object} options - Exactly the same as $.ajax options
* @return {object} ajax promise
*/
const ajax = (options) => {
 
   var oldSuccess = options.success;
   var oldError = options.error;
 
   options.success = (response, textStatus, jqXHR) => {
 
       $('#ls-loading').hide();
 
       // User-supplied success is always run EXCEPT when login fails
       var runOldSuccess = onSuccess(response);
 
       if (oldSuccess && runOldSuccess) {
           oldSuccess(response, textStatus, jqXHR);
       }
   }
 
   options.error = (jqXHR, textStatus, errorThrown) => {
       $('#ls-loading').hide();
 
       console.error('AJAX CALL FAILED -> ', {
            errorThrown: errorThrown,
            textStatus: textStatus,
            jqXHR: jqXHR,
       });
 
       if (oldError) {
           oldError(jqXHR, textStatus, errorThrown);
       }
   }
 
   $('#ls-loading').show();
   return $.ajax(options);
}
 
export {ajax, onSuccess};