Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. import { push } from 'react-router-redux'
  2. import * as actionTypes from '../../actions/actionTypes';
  3. import appConfig from '../../config/appConfig';
  4.  
  5. const parseJSON = response => response.json();
  6.  
  7. const checkStatus = (dispatch, response) => {
  8. if (response.status >= 200 && response.status < 300) {
  9. return response;
  10. }
  11.  
  12. if (response.status === 401) {
  13. dispatch({type: actionTypes.UNAUTHORIZED_REDIRECT});
  14. dispatch(push(appConfig.UNAUTHORIZED_ENDPOINT));
  15. } else if (response.status === 403) {
  16. dispatch({type: actionTypes.FORBIDDEN_REDIRECT});
  17. dispatch(push(appConfig.FORBIDDEN_ENDPOINT));
  18. } else if (response.status === 404) {
  19. dispatch({type: actionTypes.NOT_FOUND_RENDER});
  20. } else if (response.status >= 400 && response.status < 500) {
  21. response.json().then((data) => {
  22. dispatch({type: actionTypes.SHOW_ALERT_MESSAGE, isError: true, error: data.error, errors: data.errors});
  23. });
  24. } else if (response.status >= 500 && response.status < 600) {
  25. dispatch({type: actionTypes.SERVER_ERROR_REDIRECT});
  26. dispatch(push(appConfig.SERVER_ERROR_ENDPOINT));
  27. }
  28.  
  29. return response.json().then((data) => {
  30. const error = new Error(response.statusText, data);
  31. throw error;
  32. });
  33. };
  34.  
  35. const checkPath = (dispatch, response) => {
  36. if (response.path) {
  37. dispatch({type: actionTypes.BREADCRUMBS_SET, path: response.path})
  38. }
  39. return response;
  40. }
  41.  
  42. const checkMessage = (dispatch, response) => {
  43. if (response.message) {
  44. dispatch({type: actionTypes.SHOW_ALERT_MESSAGE, message: response.message, isError: false})
  45. }
  46. return response
  47. }
  48.  
  49. export default function promiseMiddleware({ dispatch, getState }) {
  50. return next => (action) => {
  51. const { promise, onRequest, onSuccess, onFailure, ...rest } = action;
  52.  
  53. if (!promise) {
  54. // if action dispatched is not a promise, just send it to the next processor
  55. return next(action);
  56. }
  57. if (typeof onRequest === 'function') {
  58. onRequest(dispatch, getState, ...rest);
  59. } else {
  60. dispatch({ type: onRequest, ...rest });
  61. }
  62.  
  63. return promise
  64. .catch((error, ...rest) => {
  65. dispatch({type: actionTypes.NOT_FOUND_RENDER});
  66. throw new Error('Network failure', error.message);
  67. })
  68. .then(checkStatus.bind(null, dispatch))
  69. .then(parseJSON)
  70. .then(checkPath.bind(null, dispatch))
  71. .then(checkMessage.bind(null, dispatch))
  72. .then((response) => {
  73. try {
  74. if (typeof onSuccess === 'function') {
  75. onSuccess(response, dispatch, getState, ...rest);
  76. } else {
  77. dispatch({ type: onSuccess, response, ...rest });
  78. }
  79. } catch (e) {
  80. e.message = `Action success error: ${e.message}`;
  81. e.type = 'ActionError';
  82. throw e;
  83. }
  84. })
  85. .catch((error) => {
  86. if (error.type !== 'ActionError' || error.type === 'Unauthorized') {
  87. if (typeof onFailure === 'function') {
  88. onFailure(error.response, dispatch, getState, ...rest);
  89. } else {
  90. dispatch({ type: onFailure, error: error.response, ...rest });
  91. }
  92. } else {
  93. throw error;
  94. }
  95. });
  96. };
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement