Skip to content

Instantly share code, notes, and snippets.

@akmur
Created March 15, 2019 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akmur/0cd71297e3be5d0805dd48a3f3a9375c to your computer and use it in GitHub Desktop.
Save akmur/0cd71297e3be5d0805dd48a3f3a9375c to your computer and use it in GitHub Desktop.
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