2023-05-09 10:56:26 -04:00
|
|
|
import { Middleware } from 'redux';
|
|
|
|
import { showAlertForError } from '../../actions/alerts';
|
|
|
|
import { RootState } from '..';
|
2016-10-18 11:09:45 -04:00
|
|
|
|
|
|
|
const defaultFailSuffix = 'FAIL';
|
|
|
|
|
2023-05-09 10:56:26 -04:00
|
|
|
export const errorsMiddleware: Middleware<Record<string, never>, RootState> =
|
2023-05-09 13:02:12 -04:00
|
|
|
({ dispatch }) =>
|
|
|
|
(next) =>
|
|
|
|
(action) => {
|
2017-02-26 17:06:27 -05:00
|
|
|
if (action.type && !action.skipAlert) {
|
2016-10-18 11:09:45 -04:00
|
|
|
const isFail = new RegExp(`${defaultFailSuffix}$`, 'g');
|
|
|
|
|
|
|
|
if (action.type.match(isFail)) {
|
2020-03-28 12:59:45 -04:00
|
|
|
dispatch(showAlertForError(action.error, action.skipNotFound));
|
2016-10-18 11:09:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
};
|