Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { push } from 'react-router-redux'
- import * as actionTypes from '../../actions/actionTypes';
- import appConfig from '../../config/appConfig';
- const parseJSON = response => response.json();
- const checkStatus = (dispatch, response) => {
- if (response.status >= 200 && response.status < 300) {
- return response;
- }
- if (response.status === 401) {
- dispatch({type: actionTypes.UNAUTHORIZED_REDIRECT});
- dispatch(push(appConfig.UNAUTHORIZED_ENDPOINT));
- } else if (response.status === 403) {
- dispatch({type: actionTypes.FORBIDDEN_REDIRECT});
- dispatch(push(appConfig.FORBIDDEN_ENDPOINT));
- } else if (response.status === 404) {
- dispatch({type: actionTypes.NOT_FOUND_RENDER});
- } else if (response.status >= 400 && response.status < 500) {
- response.json().then((data) => {
- dispatch({type: actionTypes.SHOW_ALERT_MESSAGE, isError: true, error: data.error, errors: data.errors});
- });
- } else if (response.status >= 500 && response.status < 600) {
- dispatch({type: actionTypes.SERVER_ERROR_REDIRECT});
- dispatch(push(appConfig.SERVER_ERROR_ENDPOINT));
- }
- return response.json().then((data) => {
- const error = new Error(response.statusText, data);
- throw error;
- });
- };
- const checkPath = (dispatch, response) => {
- if (response.path) {
- dispatch({type: actionTypes.BREADCRUMBS_SET, path: response.path})
- }
- return response;
- }
- const checkMessage = (dispatch, response) => {
- if (response.message) {
- dispatch({type: actionTypes.SHOW_ALERT_MESSAGE, message: response.message, isError: false})
- }
- return response
- }
- export default function promiseMiddleware({ dispatch, getState }) {
- return next => (action) => {
- const { promise, onRequest, onSuccess, onFailure, ...rest } = action;
- if (!promise) {
- // if action dispatched is not a promise, just send it to the next processor
- return next(action);
- }
- if (typeof onRequest === 'function') {
- onRequest(dispatch, getState, ...rest);
- } else {
- dispatch({ type: onRequest, ...rest });
- }
- return promise
- .catch((error, ...rest) => {
- dispatch({type: actionTypes.NOT_FOUND_RENDER});
- throw new Error('Network failure', error.message);
- })
- .then(checkStatus.bind(null, dispatch))
- .then(parseJSON)
- .then(checkPath.bind(null, dispatch))
- .then(checkMessage.bind(null, dispatch))
- .then((response) => {
- try {
- if (typeof onSuccess === 'function') {
- onSuccess(response, dispatch, getState, ...rest);
- } else {
- dispatch({ type: onSuccess, response, ...rest });
- }
- } catch (e) {
- e.message = `Action success error: ${e.message}`;
- e.type = 'ActionError';
- throw e;
- }
- })
- .catch((error) => {
- if (error.type !== 'ActionError' || error.type === 'Unauthorized') {
- if (typeof onFailure === 'function') {
- onFailure(error.response, dispatch, getState, ...rest);
- } else {
- dispatch({ type: onFailure, error: error.response, ...rest });
- }
- } else {
- throw error;
- }
- });
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement