import { useMemo } from 'react';
import { useLocation, Navigate, Outlet } from 'react-router-dom';
import { useSelector } from 'react-redux';
import CssBaseline from '@mui/material/CssBaseline';
import NavBar from './NavBar';
import { selectCurrentUser } from '../features/auth/authSlice';

const authenticate = () => {
  const user = useSelector(selectCurrentUser);
  return useMemo(() => ({ user }), [user]);
};

function Rsvp() {
  const auth = authenticate();
  const location = useLocation();

  return auth.user ? (
    <>
      <CssBaseline />
      <NavBar />
      <Outlet context={auth} />
    </>
  ) : (
    <Navigate to="/guest-login" state={{ from: location }} replace />
  );
}

export default Rsvp;
