2020-12-08 04:39:13 +00:00
|
|
|
import isObject from 'lodash.isobject'
|
|
|
|
import { showToast } from '../actions/toasts'
|
|
|
|
import {
|
|
|
|
TOAST_TYPE_ERROR,
|
|
|
|
TOAST_TYPE_SUCCESS,
|
|
|
|
} from '../constants'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-12-08 04:39:13 +00:00
|
|
|
const defaultSuccessSuffix = 'SUCCESS'
|
|
|
|
const defaultFailSuffix = 'FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
export default function errorsMiddleware() {
|
2020-12-08 04:39:13 +00:00
|
|
|
return ({ dispatch }) => (next) => (action) => {
|
|
|
|
if (isObject(action) && action.type && action.showToast) {
|
|
|
|
const isFail = new RegExp(`${defaultFailSuffix}$`, 'g')
|
|
|
|
const isSuccess = new RegExp(`${defaultSuccessSuffix}$`, 'g')
|
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
if (action.type.match(isFail)) {
|
2020-12-08 04:39:13 +00:00
|
|
|
dispatch(showToast(TOAST_TYPE_ERROR, action))
|
|
|
|
} else if (action.type.match(isSuccess)) {
|
|
|
|
dispatch(showToast(TOAST_TYPE_SUCCESS, action))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
2020-12-08 04:39:13 +00:00
|
|
|
return next(action)
|
|
|
|
}
|
|
|
|
}
|