nexshop-web-skeleton
Version:
Nexshop Web Skeleton Project
32 lines (22 loc) • 952 B
JavaScript
import endsWith from 'lodash/endsWith';
import replace from 'lodash/replace';
const loadingStatusMap = {};
const reduxLoadingMiddleware = store => next => action => {
if(typeof action === 'function') {
const actionType = action.name;
if(typeof actionType === 'string' && actionType.length > 0) {
loadingStatusMap[actionType] = (loadingStatusMap[actionType] || 0) + 1;
store.dispatch({type: 'FETCH_STATUS', status: true, id: actionType});
}
} else {
if(endsWith(action.type, 'SUCCESS') || endsWith(action.type, 'FAIL')) {
const actionType = replace(action.type, /(_SUCCESS|_FAIL)/, '');
loadingStatusMap[actionType]--;
if(loadingStatusMap[actionType] === 0) {
store.dispatch({type: 'FETCH_STATUS', status: false, id: actionType});
}
}
}
return next(action);
};
export default reduxLoadingMiddleware;