Guest User

Untitled

a guest
Jan 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import { applyMiddleware, compose, createStore } from 'redux'
  2.  
  3. import thunk from 'redux-thunk';
  4. import promise from 'redux-promise-middleware'
  5. import logger from 'redux-logger'
  6.  
  7. import reducer from './reducers'
  8.  
  9. const middleware = applyMiddleware(thunk, promise(), logger({diff: true}));
  10. const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
  11. const enhancer = composeEnhancers(middleware);
  12.  
  13. export default createStore(
  14. reducer,
  15. enhancer)
  16.  
  17. export function startTest(test) {
  18. return dispatch => {
  19. return dispatch({
  20. type: START_TEST,
  21. id: test,
  22. payload: Promise.all([
  23. dispatch(axios.post(urlJoin(config.portalUrl, 'account/login')))
  24. ])
  25. })
  26. };
  27. }
  28.  
  29. export function startTest(test) {
  30. return dispatch => {
  31. return dispatch({
  32. type: START_TEST_THUNK,
  33. payload: Promise.all([
  34. dispatch({
  35. type: START_TEST,
  36. payload: axios.post(urlJoin(config.portalUrl, 'account/login')),
  37. meta: {
  38. id: test
  39. }
  40. })
  41. ])
  42. })
  43. };
  44. }
  45.  
  46. export default function PromiseMiddleware() {
  47. return (next) => (action) => {
  48. const {promise, type, ...rest} = action
  49.  
  50. if (!promise) return next(action)
  51.  
  52. const REQUEST = type + '_REQUEST'
  53. const SUCCESS = type + '_SUCCESS'
  54. const FAILURE = type + '_FAILURE'
  55.  
  56. next({...rest, type: REQUEST})
  57.  
  58. return promise
  59. .then(result => {
  60.  
  61. next({...rest, result, type: SUCCESS})
  62.  
  63. return true
  64. })
  65. .catch(error => {
  66. if (DEBUG) {
  67. console.error(error)
  68. console.log(error.stack)
  69. }
  70. next({...rest, error, type: FAILURE})
  71.  
  72. return false
  73. })
  74. }
  75. }
  76.  
  77. const middleware = applyMiddleware(thunk, PromiseMiddleware, logger({diff: true}))
Add Comment
Please, Sign In to add comment