import { Route, Redirect } from 'react-router-dom';
import { useSelector } from 'react-redux';

import { IPublicProps } from '../../../../typescript/components/Routes/interfaces';
import { getToken } from '../../../../redux/authorization/selector'

function PublicRoute({
  children,
  restricted = false,
  ...routeProps
}: IPublicProps) {
  const token = useSelector(getToken)
  const shouldRedirect = token && restricted;

  return (
    <Route {...routeProps}>
      {shouldRedirect ? <Redirect to="/" /> : children}
    </Route>
  );
}


export default PublicRoute;
