aretherecookies-mobile/js/components/ProductSaveBtn.js
2020-04-17 22:50:23 -05:00

26 lines
794 B
JavaScript

// @flow
import { withCreateProductState } from '../enhancers/createProductEnhancers';
import { withReplaceRoute } from '../enhancers/routeEnhancers';
import { compose, onlyUpdateForKeys, withHandlers } from 'recompose';
import { routeWithTitle } from '../helpers/RouteHelpers';
import TopSaveButton from './TopSaveButton';
export default compose(
withCreateProductState,
withReplaceRoute,
withHandlers({
onSave: ({ saveProduct, setLoading, setError, replaceRoute }) => async () => {
setError(null);
setLoading(true);
try {
const { id, name } = await saveProduct();
replaceRoute(routeWithTitle(`/product/${id || ''}`, name));
} catch (error) {
setError(error);
} finally {
setLoading(false);
}
},
}),
onlyUpdateForKeys(['loading'])
)(TopSaveButton);