diff options
author | Claire <claire.github-309c@sitedethib.com> | 2022-10-11 10:41:15 +0200 |
---|---|---|
committer | Claire <claire.github-309c@sitedethib.com> | 2022-10-11 11:31:11 +0200 |
commit | d3879c07b1b2140bd19433ae06855894228bd90f (patch) | |
tree | 95d79cbf675206abaa937c3aaf964d323ec52acb /app/javascript/flavours/glitch/utils/react_router_helpers.js | |
parent | 1be97ce17fa6c5635b0922ec97378360d793db59 (diff) |
Rename flavours/glitch/util into flavours/glitch/utils
Diffstat (limited to 'app/javascript/flavours/glitch/utils/react_router_helpers.js')
-rw-r--r-- | app/javascript/flavours/glitch/utils/react_router_helpers.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/app/javascript/flavours/glitch/utils/react_router_helpers.js b/app/javascript/flavours/glitch/utils/react_router_helpers.js new file mode 100644 index 000000000..e36c512f3 --- /dev/null +++ b/app/javascript/flavours/glitch/utils/react_router_helpers.js @@ -0,0 +1,69 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import { Switch, Route } from 'react-router-dom'; + +import ColumnLoading from 'flavours/glitch/features/ui/components/column_loading'; +import BundleColumnError from 'flavours/glitch/features/ui/components/bundle_column_error'; +import BundleContainer from 'flavours/glitch/features/ui/containers/bundle_container'; + +// Small wrapper to pass multiColumn to the route components +export class WrappedSwitch extends React.PureComponent { + + render () { + const { multiColumn, children } = this.props; + + return ( + <Switch> + {React.Children.map(children, child => React.cloneElement(child, { multiColumn }))} + </Switch> + ); + } + +} + +WrappedSwitch.propTypes = { + multiColumn: PropTypes.bool, + children: PropTypes.node, +}; + +// Small Wraper to extract the params from the route and pass +// them to the rendered component, together with the content to +// be rendered inside (the children) +export class WrappedRoute extends React.Component { + + static propTypes = { + component: PropTypes.func.isRequired, + content: PropTypes.node, + multiColumn: PropTypes.bool, + componentParams: PropTypes.object, + } + + static defaultProps = { + componentParams: {}, + }; + + renderComponent = ({ match }) => { + const { component, content, multiColumn, componentParams } = this.props; + + return ( + <BundleContainer fetchComponent={component} loading={this.renderLoading} error={this.renderError}> + {Component => <Component params={match.params} multiColumn={multiColumn} {...componentParams}>{content}</Component>} + </BundleContainer> + ); + } + + renderLoading = () => { + return <ColumnLoading />; + } + + renderError = (props) => { + return <BundleColumnError {...props} />; + } + + render () { + const { component: Component, content, ...rest } = this.props; + + return <Route {...rest} render={this.renderComponent} />; + } + +} |