File size: 1,222 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
// @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<Object>,
render: Function,
};
type State = {
isLoading: boolean,
};
class MutationWrapper extends React.Component<Props, State> {
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 <div onClick={this.init}>{this.props.render(this.state)}</div>;
}
}
export default connect()(MutationWrapper);
|