Skip to content

Instantly share code, notes, and snippets.

@thiagovilla
Last active August 31, 2018 19:47
Show Gist options
  • Save thiagovilla/62e635273b4a823e7f7a2b611b17ef48 to your computer and use it in GitHub Desktop.
Save thiagovilla/62e635273b4a823e7f7a2b611b17ef48 to your computer and use it in GitHub Desktop.
// products/ducks/productsOperations.js
import {
getProducts,
patchProduct as _patchProduct
// other API endpoints (HTTP verbs)
} from 'api/products' // webpack 'api' alias
import {
fetchProductsRequest,
fetchProductsSuccess,
fetchProductsError,
patchProductRequest,
patchProductSuccess,
patchProductError
} from './productsActions'
const fetchProducts = params =>
dispatch => {
dispatch(fetchProductsRequest())
getProducts(params) // API endpoint
.then(products => dispatch(fetchProductsSuccess(products)))
.catch(error => dispatch(fetchProductsError(error)))
// addProduct (POST) and updateProduct (PUT) thunks here
const patchProduct = (id, key, value) =>
dispatch => {
dispatch(patchProductRequest(id, key))
_patchProduct({ id, key, value }) // API endpoint
.then(response => dispatch(patchProductSuccess(id, key, response)))
.catch(error => dispatch(patchProductError(id, key, error)))
}
// removeProduct (DELETE) thunk here
export {
fetchProducts,
patchProduct
// other operations
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment