Created
November 17, 2017 13:00
-
-
Save alexbeletsky/e5d8c265218d57d1f3cc8569dcb983f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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