mirror of
https://github.com/wassname/talk.git
synced 2026-08-01 13:00:55 +08:00
39 lines
955 B
JavaScript
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;
|