import PropTypes from 'prop-types'; import React from 'react'; import ImmutablePropTypes from 'react-immutable-proptypes'; import { connect } from 'react-redux'; import { Redirect, Route } from 'react-router-dom'; import { getSettings } from 'soapbox/actions/settings'; import { isStaff, isAdmin } from 'soapbox/utils/accounts'; import BundleColumnError from '../components/bundle_column_error'; import ColumnForbidden from '../components/column_forbidden'; import ColumnLoading from '../components/column_loading'; import BundleContainer from '../containers/bundle_container'; import ColumnsAreaContainer from '../containers/columns_area_container'; const mapStateToProps = state => { const me = state.get('me'); return { account: state.getIn(['accounts', me]), settings: getSettings(state), }; }; 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, settings: ImmutablePropTypes.map.isRequired, publicRoute: PropTypes.bool, staffOnly: PropTypes.bool, adminOnly: PropTypes.bool, developerOnly: 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, settings, publicRoute, developerOnly, staffOnly, adminOnly, ...rest } = this.props; const authorized = [ account || publicRoute, developerOnly ? settings.get('isDeveloper') : true, 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 };