gab-social/app/javascript/gabsocial/middleware/errors.js

28 lines
767 B
JavaScript
Raw Normal View History

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
const defaultSuccessSuffix = 'SUCCESS'
const defaultFailSuffix = 'FAIL'
2019-07-02 08:10:25 +01:00
export default function errorsMiddleware() {
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)) {
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
}
return next(action)
}
}