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

27 lines
921 B
JavaScript
Raw Permalink Normal View History

import isObject from 'lodash.isobject'
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 (isObject(action) && action.type && action.type.indexOf('TIMELINE') > -1) {
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)
}
}