Created
December 21, 2016 10:31
-
-
Save with-heart/9346c27e65dd940329115cfe923e4288 to your computer and use it in GitHub Desktop.
Fetching Nested Model by ID
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
import React, { Component } from 'react' | |
import { withRouter } from 'react-router' | |
import { connect } from 'react-redux' | |
import { projectSelector } from 'selectors/projects' | |
import { userSelector } from 'selectors/users' | |
import { fetchProjectsRequest } from 'actions/projects' | |
import { fetchUsersRequest } from 'actions/users' | |
import { Project } from 'components/projects' | |
class ProjectContainer extends Component { | |
componentDidMount() { | |
this.props.loadProject(this.props.params.id) | |
} | |
componentDidUpdate({ params }) { | |
if (params.id !== this.props.params.id) { | |
this.props.loadProject(this.props.params.id) | |
} | |
} | |
componentWillReceiveProps({ project, lead }) { | |
if (project && lead == null) { | |
this.props.loadUser(project.lead) | |
} | |
} | |
render() { | |
return <Project {...this.props} /> | |
} | |
} | |
const mapStateToProps = (state, { params }) => { | |
const project = projectSelector(state, params.id) | |
const lead = project ? userSelector(state, project.get('lead')) : null | |
return { | |
project, | |
lead, | |
} | |
} | |
const mapDispatchToProps = dispatch => ({ | |
loadProject(id) { | |
dispatch(fetchProjectsRequest({id: id})) | |
}, | |
loadUser(id) { | |
dispatch(fetchUsersRequest({id: id})) | |
} | |
}) | |
ProjectContainer = withRouter(connect( | |
mapStateToProps, | |
mapDispatchToProps, | |
)(ProjectContainer)) | |
export default ProjectContainer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment