Files
talk/client/coral-framework/hocs/notifyOnMutationError.js
T

39 lines
955 B
JavaScript

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { compose } from 'react-apollo';
import { notify } from 'coral-framework/actions/notification';
import { forEachError } from 'coral-framework/utils';
import { withProps, branch } from 'recompose';
const notifyOnMutationError = keys =>
compose(
branch(
({ notify }) => !notify,
connect(null, dispatch =>
bindActionCreators(
{
notify,
},
dispatch
)
)
),
withProps(ownProps =>
keys.reduce((props, key) => {
props[key] = async (...args) => {
try {
return await ownProps[key](...args);
} catch (e) {
forEachError(e, ({ msg }) => {
ownProps.notify('error', msg);
});
throw e;
}
};
return props;
}, {})
)
);
export default notifyOnMutationError;