gab-social/app/javascript/gabsocial/features/ui/util/wrapped_route.js

78 lines
2.1 KiB
JavaScript
Raw Normal View History

import { Route } from 'react-router-dom';
2020-02-03 18:24:24 +00:00
import PageLayout from '../../../components/page_layout';
import BundleColumnError from '../../../components/bundle_column_error';
import Bundle from './bundle';
import { me } from '../../../initial_state';
import ColumnIndicator from '../../../components/column_indicator';
2019-07-02 08:10:25 +01:00
export default class WrappedRoute extends Component {
2019-07-02 08:10:25 +01:00
static propTypes = {
component: PropTypes.func.isRequired,
page: PropTypes.func,
content: PropTypes.node,
componentParams: PropTypes.object,
layout: PropTypes.object,
publicRoute: PropTypes.bool,
};
static defaultProps = {
componentParams: {},
};
renderComponent = ({ match }) => {
const { component, content, componentParams, layout, page: Page } = this.props;
if (Page) {
return (
<Bundle fetchComponent={component} loading={this.renderLoading} error={this.renderError}>
2019-07-02 08:10:25 +01:00
{Component =>
(
2020-01-29 16:45:17 +00:00
<Page params={match.params} {...componentParams}>
<Component params={match.params} {...componentParams}>
{content}
</Component>
</Page>
2019-07-02 08:10:25 +01:00
)
}
</Bundle>
2019-07-02 08:10:25 +01:00
);
}
return (
<Bundle fetchComponent={component} loading={this.renderLoading} error={this.renderError}>
2019-07-02 08:10:25 +01:00
{Component =>
(
2020-02-03 18:24:24 +00:00
<PageLayout layout={layout}>
2019-07-02 08:10:25 +01:00
<Component params={match.params} {...componentParams}>
{content}
</Component>
2020-02-03 18:24:24 +00:00
</PageLayout>
2019-07-02 08:10:25 +01:00
)
}
</Bundle>
2019-07-02 08:10:25 +01:00
);
}
renderLoading = () => {
2020-02-14 00:40:04 +00:00
return <div />
2019-07-02 08:10:25 +01:00
}
renderError = (props) => {
return <BundleColumnError {...props} />;
}
2020-01-29 16:45:17 +00:00
render() {
2019-07-02 08:10:25 +01:00
const { component: Component, content, publicRoute, ...rest } = this.props;
if (!publicRoute && !me) {
const actualUrl = encodeURIComponent(this.props.computedMatch.url);
return <Route path={this.props.path} component={() => {
2020-01-29 16:45:17 +00:00
window.location.href = `/auth/sign_in?redirect_uri=${actualUrl}`;
return null;
}} />
2019-07-02 08:10:25 +01:00
}
return <Route {...rest} render={this.renderComponent} />;
}
}