import React from 'react';
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 { selectGuests } from '../slices/auth/adminSlice';

const authenticate = () => {
  const guests = useSelector(selectGuests);
  return useMemo(() => ({ guests }), [guests]);
};

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

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

export default Rsvp;
