Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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