import React from 'react'; import { connect } from 'react-redux'; import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; import { Redirect, Route } from 'react-router-dom'; import ColumnsAreaContainer from '../containers/columns_area_container'; import ColumnLoading from '../components/column_loading'; import ColumnForbidden from '../components/column_forbidden'; import BundleColumnError from '../components/bundle_column_error'; import BundleContainer from '../containers/bundle_container'; import { isStaff, isAdmin } from 'soapbox/utils/accounts'; const mapStateToProps = state => { const me = state.get('me'); return { account: state.getIn(['accounts', me]), }; }; class WrappedRoute extends React.Component { static propTypes = { component: PropTypes.func.isRequired, page: PropTypes.oneOfType([PropTypes.object, PropTypes.func]), content: PropTypes.node, componentParams: PropTypes.object, layout: PropTypes.object, account: ImmutablePropTypes.map, publicRoute: PropTypes.bool, staffOnly: PropTypes.bool, adminOnly: PropTypes.bool, }; static defaultProps = { componentParams: {}, }; renderComponent = ({ match }) => { const { component, content, componentParams, layout, page: Page } = this.props; if (Page) { return ( {Component => ( {content} ) } ); } return ( {Component => ( {content} ) } ); } renderLoading = () => { return ( ); } renderForbidden = () => { return ( ); } renderError = (props) => { return ( ); } loginRedirect = () => { const actualUrl = encodeURIComponent(`${this.props.computedMatch.url}${this.props.location.search}`); // eslint-disable-line react/prop-types return ; } render() { const { component: Component, content, account, publicRoute, staffOnly, adminOnly, ...rest } = this.props; const authorized = [ account || publicRoute, staffOnly ? account && isStaff(account) : true, adminOnly ? account && isAdmin(account) : true, ].every(c => c); if (!authorized) { if (!account) { return this.loginRedirect(); } else { return this.renderForbidden(); } } return ; } } const wrappedRoute = connect(mapStateToProps)(WrappedRoute); export { wrappedRoute as WrappedRoute };