Files
PeerNotes/frontend/src/App.tsx

49 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-03-24 18:02:40 -04:00
import "./styles/App.css";
2024-04-11 16:48:58 -04:00
import { Link, useNavigate } from 'react-router-dom';
import { Outlet } from "react-router-dom";
2024-04-11 16:48:58 -04:00
import { SessionContext, destroySessionCookie, getSessionCookie } from "./contexts/session";
import { useState, useEffect, useContext } from "react";
2024-03-12 16:21:30 -04:00
2024-03-24 18:02:40 -04:00
export default function App() {
2024-04-11 16:48:58 -04:00
const [session, setSession] = useState(getSessionCookie());
const navigate = useNavigate();
// Redirect to login if session is undefined
useEffect(
() => {
setSession(getSessionCookie());
if (session === undefined) {
navigate("/login");
}
},
[session, navigate]
);
2024-03-12 16:21:30 -04:00
return (
<>
2024-04-11 16:48:58 -04:00
<SessionContext.Provider value={session}>
<Navbar />
<div className="App" style={{ display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', height: '90vh' , gap: 15}}>
<Outlet />
2024-04-11 16:48:58 -04:00
</div>
</SessionContext.Provider>
2024-03-12 16:21:30 -04:00
</>
)
}
2024-03-24 18:02:40 -04:00
function Navbar() {
2024-04-11 16:48:58 -04:00
const session = useContext(SessionContext);
2024-03-12 16:21:30 -04:00
2024-04-11 16:48:58 -04:00
const links = session ? (
<>
<Link to="/">Home</Link>
<Link to="/login" onClick={destroySessionCookie}>Logout</Link>
</>
): (
<Link to="/login">Login</Link>
)
2024-03-24 18:02:40 -04:00
return (
<nav>
2024-04-11 16:48:58 -04:00
{links}
2024-03-24 18:02:40 -04:00
</nav>
);
}