Skip to content

Instantly share code, notes, and snippets.

@alexbeletsky
Created November 17, 2017 13:20
Show Gist options
  • Save alexbeletsky/032c6b96686acf9aa426f54fa70bc027 to your computer and use it in GitHub Desktop.
Save alexbeletsky/032c6b96686acf9aa426f54fa70bc027 to your computer and use it in GitHub Desktop.
export default function websiteContext(Component) {
class WebsiteContextComponent extends React.Component {
static propTypes = {
dispatch: PropTypes.func.isRequired,
state: PropTypes.object.isRequire,
params: PropTypes.shape({
websiteId: PropTypes.string.isRequired
})
};
componentDidMount() {
this._setCurrentWebsite();
}
componentDidUpdate() {
this._setCurrentWebsite();
}
_setCurrentWebsite() {
const {
dispatch,
params: { websiteId },
state: { websites }
} = this.props;
// prepare context data here..
const website = _.find(websites, { id: websiteId });
dispatch(loadContextWebsite(website));
}
render() {
const { state: { website } } = this.props;
return (
<div className="website-context">
{ website ? <Component {...this.props} /> : null }
</div>
);
}
}
const mapStateToProps = (state) => {
return {
state: state.application
};
};
return connect(mapStateToProps)(WebsiteContextComponent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment