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.

69 lines
1.9 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Switch, Route } from 'react-router-dom';
  4. import ColumnLoading from 'flavours/glitch/features/ui/components/column_loading';
  5. import BundleColumnError from 'flavours/glitch/features/ui/components/bundle_column_error';
  6. import BundleContainer from 'flavours/glitch/features/ui/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 Wraper 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. return <ColumnLoading />;
  45. }
  46. renderError = (props) => {
  47. return <BundleColumnError {...props} />;
  48. }
  49. render () {
  50. const { component: Component, content, ...rest } = this.props;
  51. return <Route {...rest} render={this.renderComponent} />;
  52. }
  53. }