| [40ac7a9] | 1 | import React, { useEffect, useState } from "react";
|
|---|
| 2 | import AppBar from "@mui/material/AppBar";
|
|---|
| 3 | import Toolbar from "@mui/material/Toolbar";
|
|---|
| 4 | import Typography from "@mui/material/Typography";
|
|---|
| 5 | import Button from "@mui/material/Button";
|
|---|
| 6 | import InputBase from "@mui/material/InputBase";
|
|---|
| 7 | import Box from "@mui/material/Box";
|
|---|
| 8 | import { styled } from "@mui/material/styles";
|
|---|
| 9 | import LogoUrl from '../assets/projectlogo.png';
|
|---|
| [67dfe57] | 10 | import {onGetAuthState} from "../pages/+Layout.telefunc";
|
|---|
| [40ac7a9] | 11 |
|
|---|
| 12 |
|
|---|
| 13 | type AuthState = { isLoggedIn: boolean; username: string | null };
|
|---|
| 14 |
|
|---|
| 15 | export default function Navbar() {
|
|---|
| 16 | const [auth, setAuth] = useState<AuthState | null>(null);
|
|---|
| 17 |
|
|---|
| 18 | const handleLogout = async (e: React.MouseEvent) => { // funkcija za logout da go resetira auth state, za da ne go pise username-ot u navbar, plus te vrakja na login nazad
|
|---|
| 19 | e.preventDefault();
|
|---|
| 20 |
|
|---|
| 21 | setAuth({ isLoggedIn: false, username: null });
|
|---|
| 22 | window.location.href = "/api/auth/signout?callbackUrl=/auth/login";
|
|---|
| 23 | };
|
|---|
| 24 |
|
|---|
| 25 | useEffect(() => {
|
|---|
| 26 | let active = true;
|
|---|
| [67dfe57] | 27 | onGetAuthState()
|
|---|
| [40ac7a9] | 28 | .then((data) => active && setAuth({ isLoggedIn: data.isLoggedIn, username: data.username }))
|
|---|
| 29 | .catch(() => active && setAuth({ isLoggedIn: false, username: null }));
|
|---|
| 30 | return () => { active = false; };
|
|---|
| 31 | }, []);
|
|---|
| 32 |
|
|---|
| 33 | const onHoverNav = {
|
|---|
| 34 | color: 'inherit',
|
|---|
| 35 | '&:hover': {
|
|---|
| 36 | backgroundColor: '#ff8201',
|
|---|
| 37 | color: 'white',
|
|---|
| 38 | fontWeight: 'bold',
|
|---|
| 39 | }
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | return (
|
|---|
| 43 | <AppBar position="static" color="default" enableColorOnDark>
|
|---|
| 44 | <Toolbar>
|
|---|
| 45 | <Box sx={{ display: 'flex', alignItems: 'center', mr: 4 }}>
|
|---|
| 46 | <Box
|
|---|
| 47 | component="img"
|
|---|
| 48 | src={LogoUrl}
|
|---|
| 49 | alt="PC Forge Logo"
|
|---|
| 50 | sx={{ height: 40, mr: 2, cursor: 'pointer' }}
|
|---|
| 51 | />
|
|---|
| 52 | <Typography
|
|---|
| 53 | variant="h6"
|
|---|
| 54 | component="a"
|
|---|
| 55 | href="/"
|
|---|
| 56 | sx={{ textDecoration: "none", color: "inherit", fontWeight: "bold" }}
|
|---|
| 57 | >
|
|---|
| 58 | PC Forge
|
|---|
| 59 | </Typography>
|
|---|
| 60 | </Box>
|
|---|
| 61 |
|
|---|
| 62 | <Box sx={{ display: { xs: "none", md: "flex" }, gap: 2}}>
|
|---|
| 63 | <Button color="inherit" href="/forge" sx={onHoverNav}>Forge</Button>
|
|---|
| 64 | <Button color="inherit" href="/components" sx={onHoverNav}>Components</Button>
|
|---|
| 65 | <Button color="inherit" href="/completed-builds" sx={onHoverNav}>Completed Builds</Button>
|
|---|
| 66 | </Box>
|
|---|
| 67 |
|
|---|
| 68 | <Box sx={{ flexGrow: 1 }} />
|
|---|
| 69 |
|
|---|
| 70 | <Box sx={{ display: 'flex', gap: 1 }}>
|
|---|
| 71 | {auth?.isLoggedIn ? (
|
|---|
| 72 | <>
|
|---|
| 73 | <Button color="inherit" href="/dashboard/user">{auth.username}</Button>
|
|---|
| 74 | <Button color="inherit" onClick={handleLogout}>Logout</Button>
|
|---|
| 75 | </>
|
|---|
| 76 | ) : (
|
|---|
| 77 | <>
|
|---|
| 78 | <Button color="inherit" href="/auth/login">Login</Button>
|
|---|
| 79 | <Button color="inherit" href="/auth/register">Register</Button>
|
|---|
| 80 | </>
|
|---|
| 81 | )}
|
|---|
| 82 | </Box>
|
|---|
| 83 | </Toolbar>
|
|---|
| 84 | </AppBar>
|
|---|
| 85 | );
|
|---|
| 86 | }
|
|---|