Skip to content

Instantly share code, notes, and snippets.

@jfbloom22
Created October 4, 2018 13:33
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 jfbloom22/3c922dee4e7c6b836dafebbcfd531c02 to your computer and use it in GitHub Desktop.
Save jfbloom22/3c922dee4e7c6b836dafebbcfd531c02 to your computer and use it in GitHub Desktop.
Action creator with redux-thunk, redux-offline, and axios with typescript
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