react_router_helpers.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Switch, Route } from 'react-router-dom';
  4. import ColumnLoading from '../components/column_loading';
  5. import BundleColumnError from '../components/bundle_column_error';
  6. import BundleContainer from '../containers/bundle_container';
  7. // Small wrapper to pass multiColumn to the route components
  8. export class WrappedSwitch extends React.PureComponent {
  9. render () {
  10. const { multiColumn, children } = this.props;
  11. return (
  12. <Switch>
  13. {React.Children.map(children, child => React.cloneElement(child, { multiColumn }))}
  14. </Switch>
  15. );
  16. }
  17. }
  18. WrappedSwitch.propTypes = {
  19. multiColumn: PropTypes.bool,
  20. children: PropTypes.node,
  21. };
  22. // Small Wrapper to extract the params from the route and pass
  23. // them to the rendered component, together with the content to
  24. // be rendered inside (the children)
  25. export class WrappedRoute extends React.Component {
  26. static propTypes = {
  27. component: PropTypes.func.isRequired,
  28. content: PropTypes.node,
  29. multiColumn: PropTypes.bool,
  30. componentParams: PropTypes.object,
  31. };
  32. static defaultProps = {
  33. componentParams: {},
  34. };
  35. renderComponent = ({ match }) => {
  36. const { component, content, multiColumn, componentParams } = this.props;
  37. return (
  38. <BundleContainer fetchComponent={component} loading={this.renderLoading} error={this.renderError}>
  39. {Component => <Component params={match.params} multiColumn={multiColumn} {...componentParams}>{content}</Component>}
  40. </BundleContainer>
  41. );
  42. }
  43. renderLoading = () => {
  44. const { multiColumn } = this.props;
  45. return <ColumnLoading multiColumn={multiColumn} />;
  46. }
  47. renderError = (props) => {
  48. return <BundleColumnError {...props} />;
  49. }
  50. render () {
  51. const { component: Component, content, ...rest } = this.props;
  52. return <Route {...rest} render={this.renderComponent} />;
  53. }
  54. }