// @flow import * as React from 'react'; import { connect } from 'react-redux'; import { addToastWithTimeout } from 'src/actions/toasts'; import type { Dispatch } from 'redux'; type Props = { mutation: ?Function, variables: any, dispatch: Dispatch, render: Function, }; type State = { isLoading: boolean, }; class MutationWrapper extends React.Component { initialState = { isLoading: false }; state = this.initialState; init = () => { if (!this.props.mutation) return; this.setState({ isLoading: true }); return this.mutate(); }; terminate = () => { return this.setState(this.initialState); }; mutate = () => { if (!this.props.mutation) return; return this.props .mutation(this.props.variables) .then(() => { this.props.dispatch( addToastWithTimeout('success', 'Saved permissions') ); return this.terminate(); }) .catch(err => { this.props.dispatch(addToastWithTimeout('error', err.message)); return this.terminate(); }); }; render() { return
{this.props.render(this.state)}
; } } export default connect()(MutationWrapper);