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

70 lines
1.6 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
2020-04-28 06:33:58 +01:00
import api from '../../../api'
2020-02-22 23:26:23 +00:00
import { Route } from 'react-router-dom'
import BundleColumnError from '../../../components/bundle_column_error'
import Bundle from './bundle'
import { me } from '../../../initial_state'
2019-07-02 08:10:25 +01:00
class WrappedRoute extends React.PureComponent {
2019-07-02 08:10:25 +01:00
renderComponent = ({ match }) => {
2020-02-22 23:26:23 +00:00
const {
component,
content,
componentParams,
page: Page
} = this.props
2019-07-02 08:10:25 +01:00
return (
2020-02-22 23:26:23 +00:00
<Bundle fetchComponent={component} error={this.renderError}>
2019-07-02 08:10:25 +01:00
{Component =>
(
2020-02-22 23:26:23 +00:00
<Page params={match.params} {...componentParams}>
2019-07-02 08:10:25 +01:00
<Component params={match.params} {...componentParams}>
{content}
</Component>
2020-02-22 23:26:23 +00:00
</Page>
2019-07-02 08:10:25 +01:00
)
}
</Bundle>
2020-02-22 23:26:23 +00:00
)
2019-07-02 08:10:25 +01:00
}
renderError = (props) => {
2020-02-22 23:26:23 +00:00
return <BundleColumnError {...props} />
2019-07-02 08:10:25 +01:00
}
2020-01-29 16:45:17 +00:00
render() {
2020-02-22 23:26:23 +00:00
const {
component: Component,
content,
publicRoute,
...rest
} = this.props
2019-07-02 08:10:25 +01:00
if (!publicRoute && !me) {
2020-02-22 23:26:23 +00:00
const actualUrl = encodeURIComponent(this.props.computedMatch.url)
2019-07-02 08:10:25 +01:00
return <Route path={this.props.path} component={() => {
2020-02-22 23:26:23 +00:00
window.location.href = `/auth/sign_in?redirect_uri=${actualUrl}`
return null
2020-01-29 16:45:17 +00:00
}} />
2019-07-02 08:10:25 +01:00
}
2020-02-22 23:26:23 +00:00
return <Route {...rest} render={this.renderComponent} />
2019-07-02 08:10:25 +01:00
}
}
WrappedRoute.propTypes = {
component: PropTypes.func.isRequired,
page: PropTypes.func.isRequired,
content: PropTypes.node,
componentParams: PropTypes.object,
publicRoute: PropTypes.bool,
}
WrappedRoute.defaultProps = {
componentParams: {},
}
export default WrappedRoute