Skip to content

Instantly share code, notes, and snippets.

@alexbeletsky
Created November 17, 2017 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbeletsky/e5d8c265218d57d1f3cc8569dcb983f9 to your computer and use it in GitHub Desktop.
Save alexbeletsky/e5d8c265218d57d1f3cc8569dcb983f9 to your computer and use it in GitHub Desktop.
export default function checkBoarded(Component, { withValue, redirectTo }) {
class CompletedComponent extends React.Component {
static propTypes = {
state: PropTypes.object.isRequired,
dispatch: PropTypes.func.isRequired
};
componentDidMount() {
this._checkAndRedirect();
}
componentDidUpdate() {
this._checkAndRedirect();
}
_checkAndRedirect() {
const { dispatch } = this.props;
if (this._shouldRedirect()) {
dispatch(push(redirectTo));
}
}
_shouldRedirect() {
return this.props.state.user.boarded === withValue;
}
render() {
return (
<div className="boarded">
{ !this._shouldRedirect() ? <Component {...this.props} /> : null }
</div>
);
}
}
const mapStateToProps = (state) => {
return {
state: state.account
};
};
return connect(mapStateToProps)(CompletedComponent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment