Created
November 17, 2017 13:20
-
-
Save alexbeletsky/032c6b96686acf9aa426f54fa70bc027 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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