Created
March 30, 2016 17:13
-
-
Save bruce/79540b328321a71e71508606da3479e6 to your computer and use it in GitHub Desktop.
Probably dispatching rendering different components when I should just have routed to separate Product components instead...
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
| import classnames from 'classnames'; | |
| // Redux | |
| import { connect } from 'react-redux'; | |
| import Redux, { bindActionCreators } from 'redux'; | |
| import * as productSlugActions from '../../actions/currentProductSlug'; | |
| import ProductADashboard from '../../components/ProductADashboard'; | |
| import ProductBDashboard from '../../components/ProductBDashboard'; | |
| import ProductCDashboard from '../../components/ProductCDashboard'; | |
| const DASHBOARDS = { | |
| a: ProductADashboard, | |
| b: ProductBDashboard, | |
| c: ProductCDashboard | |
| }; | |
| const DASHBOARD_FRAGMENTS = { | |
| a: ['shipments'], | |
| b: [], | |
| c: [] | |
| }; | |
| import s from './Product.scss'; | |
| class Product extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.notifyAppOfProduct(props); | |
| } | |
| notifyAppOfProduct({actions, viewer}) { | |
| actions.setCurrentProductSlug(viewer.product.slug); | |
| } | |
| componentWillUpdate(props) { | |
| this.notifyAppOfProduct(props); | |
| } | |
| render() { | |
| const { viewer } = this.props; | |
| const ProductDashboard = DASHBOARDS[viewer.product.slug]; | |
| return ( | |
| <div className={s.root}> | |
| <ProductDashboard/> | |
| </div> | |
| ); | |
| } | |
| } | |
| const mapDispatchToProps = (dispatch) => { | |
| return { | |
| actions: bindActionCreators(productSlugActions, dispatch) | |
| }; | |
| }; | |
| const ReduxProduct = connect(null, mapDispatchToProps)(Product); | |
| function fragmentsForProductDashboard(slug) { | |
| return (DASHBOARD_FRAGMENTS[slug] || []).map(fragment => DASHBOARDS[slug].getFragment(fragment)); | |
| } | |
| export default Relay.createContainer(ReduxProduct, { | |
| initialVariables: { | |
| productSlug: null | |
| }, | |
| fragments: { | |
| viewer: ({productSlug}) => Relay.QL` | |
| fragment on User { | |
| product(slug: $productSlug) { | |
| name | |
| slug | |
| } | |
| ${fragmentsForProductDashboard(productSlug)} | |
| } | |
| ` | |
| }, | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment