gab-social/app/javascript/gabsocial/middleware/loading_bar.js

26 lines
843 B
JavaScript
Raw Normal View History

import { showLoading, hideLoading } from 'react-redux-loading-bar'
2019-07-02 08:10:25 +01:00
const defaultTypeSuffixes = ['PENDING', 'FULFILLED', 'REJECTED']
2019-07-02 08:10:25 +01:00
export default function loadingBarMiddleware(config = {}) {
const promiseTypeSuffixes = config.promiseTypeSuffixes || defaultTypeSuffixes
2019-07-02 08:10:25 +01:00
return ({ dispatch }) => next => (action) => {
if (action.type && !action.skipLoading) {
const [PENDING, FULFILLED, REJECTED] = promiseTypeSuffixes
2019-07-02 08:10:25 +01:00
const isPending = new RegExp(`${PENDING}$`, 'g')
const isFulfilled = new RegExp(`${FULFILLED}$`, 'g')
const isRejected = new RegExp(`${REJECTED}$`, 'g')
2019-07-02 08:10:25 +01:00
if (action.type.match(isPending)) {
dispatch(showLoading())
2019-07-02 08:10:25 +01:00
} else if (action.type.match(isFulfilled) || action.type.match(isRejected)) {
dispatch(hideLoading())
2019-07-02 08:10:25 +01:00
}
}
return next(action)
}
}