logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

react_router_helpers.js (1859B)


  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 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. }