Convert Redux custom middleware to TypeScript
This commit is contained in:
parent
7038d6a844
commit
57e5d81e33
3 changed files with 29 additions and 0 deletions
Binary file not shown.
29
app/soapbox/middleware/errors.ts
Normal file
29
app/soapbox/middleware/errors.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
import { showAlertForError } from '../actions/alerts';
|
||||
|
||||
import type { AnyAction } from 'redux';
|
||||
import type { ThunkMiddleware } from 'redux-thunk';
|
||||
|
||||
/** Whether the action is considered a failure. */
|
||||
const isFailType = (type: string): boolean => type.endsWith('_FAIL');
|
||||
|
||||
/** Whether the action is a failure to fetch from browser storage. */
|
||||
const isRememberFailType = (type: string): boolean => type.endsWith('_REMEMBER_FAIL');
|
||||
|
||||
/** Whether the error contains an Axios response. */
|
||||
const hasResponse = (error: any): boolean => Boolean(error && error.response);
|
||||
|
||||
/** Whether the error should be shown to the user. */
|
||||
const shouldShowError = ({ type, skipAlert, error }: AnyAction): boolean => {
|
||||
return !skipAlert && hasResponse(error) && isFailType(type) && !isRememberFailType(type);
|
||||
};
|
||||
|
||||
/** Middleware to display Redux errors to the user. */
|
||||
export default function errorsMiddleware(): ThunkMiddleware {
|
||||
return ({ dispatch }) => next => action => {
|
||||
if (shouldShowError(action)) {
|
||||
dispatch(showAlertForError(action.error));
|
||||
}
|
||||
|
||||
return next(action);
|
||||
};
|
||||
}
|
Binary file not shown.
Loading…
Reference in a new issue