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

75 lines
1.8 KiB
JavaScript
Raw Normal View History

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
2020-04-08 02:06:59 +01:00
export default class WrappedRoute extends PureComponent {
2019-07-02 08:10:25 +01:00
static propTypes = {
component: PropTypes.func.isRequired,
2020-02-22 23:26:23 +00:00
page: PropTypes.func.isRequired,
2019-07-02 08:10:25 +01:00
content: PropTypes.node,
componentParams: PropTypes.object,
publicRoute: PropTypes.bool,
2020-02-22 23:26:23 +00:00
}
2019-07-02 08:10:25 +01:00
static defaultProps = {
componentParams: {},
2020-02-22 23:26:23 +00:00
}
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
2020-04-28 06:33:58 +01:00
// : todo :
// api().get('/api/v1/accounts/verify_credentials')
// .then((res) => {
// console.log("res:", res)
// })
// .catch((err) => {
// console.log("err:", err)
// })
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
}
}