2020-03-27 13:59:38 -07:00
|
|
|
import { showAlertForError } from '../actions/alerts';
|
|
|
|
|
2021-10-20 14:27:36 -07:00
|
|
|
const isFailType = type => type.endsWith('_FAIL');
|
|
|
|
const isRememberFailType = type => type.endsWith('_REMEMBER_FAIL');
|
|
|
|
|
2021-10-20 15:50:03 -07:00
|
|
|
const hasResponse = error => Boolean(error && error.response);
|
|
|
|
|
|
|
|
const shouldShowError = ({ type, skipAlert, error }) => {
|
|
|
|
return !skipAlert && hasResponse(error) && isFailType(type) && !isRememberFailType(type);
|
2021-10-20 14:27:36 -07:00
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
export default function errorsMiddleware() {
|
|
|
|
return ({ dispatch }) => next => action => {
|
2021-10-20 14:27:36 -07:00
|
|
|
if (shouldShowError(action)) {
|
|
|
|
dispatch(showAlertForError(action.error));
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|