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