Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { applyMiddleware, compose, createStore } from 'redux'
- import thunk from 'redux-thunk';
- import promise from 'redux-promise-middleware'
- import logger from 'redux-logger'
- import reducer from './reducers'
- const middleware = applyMiddleware(thunk, promise(), logger({diff: true}));
- const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
- const enhancer = composeEnhancers(middleware);
- export default createStore(
- reducer,
- enhancer)
- export function startTest(test) {
- return dispatch => {
- return dispatch({
- type: START_TEST,
- id: test,
- payload: Promise.all([
- dispatch(axios.post(urlJoin(config.portalUrl, 'account/login')))
- ])
- })
- };
- }
- export function startTest(test) {
- return dispatch => {
- return dispatch({
- type: START_TEST_THUNK,
- payload: Promise.all([
- dispatch({
- type: START_TEST,
- payload: axios.post(urlJoin(config.portalUrl, 'account/login')),
- meta: {
- id: test
- }
- })
- ])
- })
- };
- }
- export default function PromiseMiddleware() {
- return (next) => (action) => {
- const {promise, type, ...rest} = action
- if (!promise) return next(action)
- const REQUEST = type + '_REQUEST'
- const SUCCESS = type + '_SUCCESS'
- const FAILURE = type + '_FAILURE'
- next({...rest, type: REQUEST})
- return promise
- .then(result => {
- next({...rest, result, type: SUCCESS})
- return true
- })
- .catch(error => {
- if (DEBUG) {
- console.error(error)
- console.log(error.stack)
- }
- next({...rest, error, type: FAILURE})
- return false
- })
- }
- }
- const middleware = applyMiddleware(thunk, PromiseMiddleware, logger({diff: true}))
Add Comment
Please, Sign In to add comment