dg-npm-templates
Version:
Npx generator for react app dependency creation by digite
74 lines (58 loc) • 1.78 kB
JavaScript
import { showLoader, hideLoader } from '../appcomponents/app/reducers/AppReducer';
let appStore;
export const setAppStore = store => {
appStore = store;
}
export const getAppStore = () => appStore;
const AppUtil = {
ajaxCounter: 0,
ajax(config = {}, showMask = true) {
const that = this;
if (showMask) {
this.ajaxCounter++;
that.showHideAppMask();
}
const options = {
headers: {
'Accept': 'application/json'
},
credentials: 'include'
};
if ( config.options ) {
for ( let key in config.options ) {
options[key] = config.options[key];
}
}
fetch(config.url, options)
.then( response => config.responseProcessor || (config.isResponseRequired ? response : response.json()) )
.then( result => {
if (showMask) {
that.ajaxCounter--;
that.showHideAppMask();
}
if (config.successFn) {
config.successFn(result);
}
}
)
.catch(function (err) {
if (showMask) {
that.ajaxCounter--;
that.showHideAppMask();
}
if (config.failureFn) {
config.failureFn(err);
}
});
},
showHideAppMask() {
if ( appStore ) {
if (this.ajaxCounter === 0) {
appStore.dispatch( hideLoader() );
} else {
appStore.dispatch( showLoader() );
}
}
}
}
export default AppUtil;