~cytrogen/masto-fe

ref: e1f466fbbe94a57f1552b28fc819dc90cd3294d3 masto-fe/app/javascript/mastodon/middleware/loading_bar.js -rw-r--r-- 854 bytes
e1f466fb — Claire Fix javascript on moderation interface (#24933) 2 years ago
                                                                                
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
import { showLoading, hideLoading } from 'react-redux-loading-bar';

const defaultTypeSuffixes = ['PENDING', 'FULFILLED', 'REJECTED'];

export default function loadingBarMiddleware(config = {}) {
  const promiseTypeSuffixes = config.promiseTypeSuffixes || defaultTypeSuffixes;

  return ({ dispatch }) => next => (action) => {
    if (action.type && !action.skipLoading) {
      const [PENDING, FULFILLED, REJECTED] = promiseTypeSuffixes;

      const isPending = new RegExp(`${PENDING}$`, 'g');
      const isFulfilled = new RegExp(`${FULFILLED}$`, 'g');
      const isRejected = new RegExp(`${REJECTED}$`, 'g');

      if (action.type.match(isPending)) {
        dispatch(showLoading());
      } else if (action.type.match(isFulfilled) || action.type.match(isRejected)) {
        dispatch(hideLoading());
      }
    }

    return next(action);
  };
}