2024-03-29 13:57:39 +00:00
|
|
|
import {
|
|
|
|
isAction,
|
|
|
|
isAsyncThunkAction,
|
|
|
|
isRejectedWithValue,
|
|
|
|
} from '@reduxjs/toolkit';
|
2024-01-08 10:57:40 +00:00
|
|
|
import type { Action, Middleware } from '@reduxjs/toolkit';
|
2023-05-10 10:59:29 +00:00
|
|
|
|
|
|
|
import type { RootState } from '..';
|
2023-05-09 14:56:26 +00:00
|
|
|
import { showAlertForError } from '../../actions/alerts';
|
2024-03-29 13:57:39 +00:00
|
|
|
import type { AsyncThunkRejectValue } from '../typed_functions';
|
2016-10-18 15:09:45 +00:00
|
|
|
|
|
|
|
const defaultFailSuffix = 'FAIL';
|
2024-01-08 10:57:40 +00:00
|
|
|
const isFailedAction = new RegExp(`${defaultFailSuffix}$`, 'g');
|
2016-10-18 15:09:45 +00:00
|
|
|
|
2024-03-29 13:57:39 +00:00
|
|
|
interface ActionWithMaybeAlertParams extends Action, AsyncThunkRejectValue {}
|
|
|
|
|
|
|
|
interface RejectedAction extends Action {
|
|
|
|
payload: AsyncThunkRejectValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isRejectedActionWithPayload(
|
|
|
|
action: unknown,
|
|
|
|
): action is RejectedAction {
|
|
|
|
return isAsyncThunkAction(action) && isRejectedWithValue(action);
|
2024-01-08 10:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isActionWithmaybeAlertParams(
|
|
|
|
action: unknown,
|
|
|
|
): action is ActionWithMaybeAlertParams {
|
|
|
|
return isAction(action);
|
|
|
|
}
|
|
|
|
|
2024-09-11 13:59:46 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-object-type -- we need to use `{}` here to ensure the dispatch types can be merged
|
2024-04-02 09:56:03 +00:00
|
|
|
export const errorsMiddleware: Middleware<{}, RootState> =
|
2023-05-09 17:02:12 +00:00
|
|
|
({ dispatch }) =>
|
|
|
|
(next) =>
|
2024-01-08 10:57:40 +00:00
|
|
|
(action) => {
|
2024-03-29 13:57:39 +00:00
|
|
|
if (isRejectedActionWithPayload(action) && !action.payload.skipAlert) {
|
|
|
|
dispatch(
|
|
|
|
showAlertForError(action.payload.error, action.payload.skipNotFound),
|
|
|
|
);
|
|
|
|
} else if (
|
2024-01-08 10:57:40 +00:00
|
|
|
isActionWithmaybeAlertParams(action) &&
|
|
|
|
!action.skipAlert &&
|
|
|
|
action.type.match(isFailedAction)
|
|
|
|
) {
|
|
|
|
dispatch(showAlertForError(action.error, action.skipNotFound));
|
2016-10-18 15:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
};
|