You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6 KiB

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