Created
March 15, 2019 10:28
-
-
Save akmur/0cd71297e3be5d0805dd48a3f3a9375c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
export function middlewareActions({ dispatch }) { | |
return next => action => { | |
if (action.type === 'USER_LOADED') { | |
dispatch({ | |
type: 'STATUS_CHANGED', | |
payload: true | |
}) | |
} | |
if (action.type === 'LOAD_NEWS') { | |
return axios.get('https://jsonplaceholder.typicode.com/posts').then( | |
json => | |
new Promise(resolve => { | |
// artificial timeout simulation | |
setTimeout(() => { | |
dispatch({ | |
type: 'NEWS_LOADED', | |
payload: json.data | |
}) | |
resolve() | |
}, Math.floor(Math.random() * 2000) + 500) | |
}) | |
) | |
} | |
if (action.type === 'LOAD_USER') { | |
return axios.get('https://jsonplaceholder.typicode.com/users/1').then( | |
json => | |
new Promise(resolve => { | |
// artificial timeout simulation | |
setTimeout(() => { | |
dispatch({ | |
type: 'USER_LOADED', | |
payload: json.data | |
}) | |
resolve() | |
}, Math.floor(Math.random() * 2000) + 500) | |
}) | |
) | |
} | |
return next(action) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment