Skip to content

Instantly share code, notes, and snippets.

@AllGistsEqual
Created February 22, 2021 20:04
Show Gist options
  • Save AllGistsEqual/bcb96e1889f7f8a52724246ef417c223 to your computer and use it in GitHub Desktop.
Save AllGistsEqual/bcb96e1889f7f8a52724246ef417c223 to your computer and use it in GitHub Desktop.
// File: src/redux/ducks/article.ts
export const articleMiddleware: Middleware = ({ dispatch, getState }) => next => action => {
next(action)
if (requestArticlesData.match(action)) {
const state = getState()
if (!(state.article && state.article.status === 'pending')) {
dispatch(apiRequest({
url: '/articles',
method: 'GET',
onSuccess: data => storeArticlesData(data),
onError: error => cancelArticlesRequest(error)
}))
dispatch(setArticlesStatus('pending'))
}
}
if (cancelArticlesRequest.match(action)) {
const { error } = action.payload
console.log("Error while requesting articles: ", error) // eslint-disable-line no-console
dispatch(setArticlesStatus('idle'))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment