Files
PeerNotes/frontend/src/App.tsx

67 lines
1.6 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">
<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() {
const linkStyle = {
textDecoration: 'none',
color: 'black',
background: '#B1D4E0',
padding: '8px 16px',
borderRadius: '5px',
2024-04-21 00:21:25 -04:00
// margin: '0 5px'
};
2024-04-11 16:48:58 -04:00
const session = useContext(SessionContext);
const logout = () => {
destroySessionCookie();
window.location.reload();
}
2024-03-12 16:21:30 -04:00
2024-04-11 16:48:58 -04:00
const links = session ? (
<>
2024-04-21 00:21:25 -04:00
<div>
<img src="/PeerNotes.png" alt="PeerNotes Logo" style={{ height: '60px' }} />
<Link to="/register" style={linkStyle}>Upload</Link>
2024-04-21 00:21:25 -04:00
<Link to="/search" style={linkStyle}>Search</Link>
</div>
<div>
<a onClick={logout} style={linkStyle}>Logout</a>
</div>
2024-04-11 16:48:58 -04:00
</>
): (
<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>
);
}