-
Notifications
You must be signed in to change notification settings - Fork 93
/
loading_bar_middleware.js
31 lines (24 loc) · 1.03 KB
/
loading_bar_middleware.js
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
import { DEFAULT_SCOPE, showLoading, hideLoading } from './loading_bar_ducks'
const defaultTypeSuffixes = ['PENDING', 'FULFILLED', 'REJECTED']
export default function loadingBarMiddleware(config = {}) {
const promiseTypeSuffixes = config.promiseTypeSuffixes || defaultTypeSuffixes
const scope = config.scope || DEFAULT_SCOPE
return ({ dispatch }) => (next) => (action) => {
if (action.type) {
const [PENDING, FULFILLED, REJECTED] = promiseTypeSuffixes
const isPending = new RegExp(`${PENDING}$`, 'g')
const isFulfilled = new RegExp(`${FULFILLED}$`, 'g')
const isRejected = new RegExp(`${REJECTED}$`, 'g')
const actionScope = (action.meta && action.meta.scope)
|| action.scope
|| scope
if (action.type.match(isPending)) {
dispatch(showLoading(actionScope))
} else if (action.type.match(isFulfilled)
|| action.type.match(isRejected)) {
dispatch(hideLoading(actionScope))
}
}
return next(action)
}
}