Created
October 4, 2018 13:33
-
-
Save jfbloom22/3c922dee4e7c6b836dafebbcfd531c02 to your computer and use it in GitHub Desktop.
Action creator with redux-thunk, redux-offline, and axios with typescript
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 { ThunkAction } from 'redux-thunk'; | |
import API from '../constants/apiEndpoints'; | |
type ThunkResult<R> = ThunkAction<R, IinitialState, undefined, any>; | |
export function updateProduct(product: Iproduct): ThunkResult<void> { | |
return (dispatch, getState) => { | |
const originalProduct = getState().manageInventory.data.find( | |
prod => prod.id === product.id | |
); | |
// find() potentially returns undefined. Should never happen since we have to have a product in order to edit it. | |
const rollbackProduct = !!originalProduct ? originalProduct : product; | |
const axiosRequest = { | |
url: API.POST.inventory.updateproduct, | |
method: 'post', | |
data: product | |
}; | |
dispatch({ | |
type: types.PRODUCT_UPDATE, | |
product, | |
meta: { | |
offline: { | |
effect: { axiosRequest, message: 'update product' }, | |
rollback: { type: types.PRODUCT_UPDATE, product: rollbackProduct } | |
} | |
} | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment