Last active
August 31, 2018 19:47
-
-
Save thiagovilla/62e635273b4a823e7f7a2b611b17ef48 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
// 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