diff --git a/src/component/Navigation/Navigation.tsx b/src/component/Navigation/Navigation.tsx index 6374455..4b628c4 100644 --- a/src/component/Navigation/Navigation.tsx +++ b/src/component/Navigation/Navigation.tsx @@ -1,154 +1,174 @@ import React, { useEffect, useState } from "react"; import { - ChartPieIcon, - DocumentTextIcon, - EnvelopeIcon, - CommandLineIcon, - Bars3Icon, - SunIcon, - MoonIcon, + ChartPieIcon, + DocumentTextIcon, + EnvelopeIcon, + CommandLineIcon, + Bars3Icon, + SunIcon, + MoonIcon, } from "@heroicons/react/24/outline"; -import { Link } from "react-router-dom"; +import { Link, useNavigate } from "react-router-dom"; +import axios from "axios"; interface NavigationProps { - children: React.ReactElement; + children: React.ReactElement; } const Navigation: React.FC = ({ - children, + children, }: NavigationProps) => { - const [isDrawerOpen, setIsDrawerOpen] = useState(false); - const toggleDrawer = () => setIsDrawerOpen(!isDrawerOpen); + const [isDrawerOpen, setIsDrawerOpen] = useState(false); + const toggleDrawer = () => setIsDrawerOpen(!isDrawerOpen); + const [user, setUser] = useState(null); + const navigate = useNavigate(); - const [theme, setTheme] = useState( - localStorage.getItem("theme") ? localStorage.getItem("theme") : "dark", - ); + const [theme, setTheme] = useState( + localStorage.getItem("theme") ? localStorage.getItem("theme") : "dark", + ); - // update state on toggle - const handleToggle = (e) => { - if (theme === "light") { - setTheme("dark"); - } else { - setTheme("light"); - } - }; + const handleToggle = (e) => { + if (theme === "light") { + setTheme("dark"); + } else { + setTheme("light"); + } + }; - // set theme state in localstorage on mount & also update localstorage on state change - useEffect(() => { - localStorage.setItem("theme", theme); - const localTheme = localStorage.getItem("theme"); - // add custom data-theme attribute to html tag required to update theme using DaisyUI - document.querySelector("html").setAttribute("data-theme", localTheme); - }, [theme]); + useEffect(() => { + axios.get("/api/users/me").then((res) => { + if (res.data.username.trim() === "") { + navigate("/login"); + } + setUser(res.data); + }); + localStorage.setItem("theme", theme); + const localTheme = localStorage.getItem("theme"); + document.querySelector("html").setAttribute("data-theme", localTheme); + }, [theme]); - return ( -
+ return ( + <> + {user && ( + <> +
- -
-
-
- + +
+
+
+ +
+ +
+ {children} +
+ +
+
+
+ {user ? ( +
  • +
    + - -
    - {children} -
  • - -
    -
    -
    -
  • -
    -
    -
    - Tailwind CSS Navbar component -
    - Malopieds -
    -
      -
    • - - Profile - -
    • -
    • - - Settings - -
    • -
      -
    • - - Log Out - -
    • -
    -
    -
  • -
  • -
    - - - Admin - -
    -
  • -
  • -
    - - - Dashboard - -
    -
  • -
  • -
    - - - TPs - -
    -
  • -
  • -
    - - - Instances - -
    -
  • -
  • -
    - - Messages -
    -
  • -
    +
      +
    • + + Profile + +
    • +
    • + + Settings + +
    • +
      +
    • + + Log Out + +
    • +
    +
    + + ) : ( +
  • + + Log Out + +
  • + )} + {user && user.roles.includes("root") && ( +
  • +
    + + + Admin + +
    +
  • + )} + {user && ( + <> +
  • +
    + + + Dashboard + +
    +
  • +
  • +
    + + + TPs + +
    +
  • +
  • +
    + + + Instances + +
    +
  • +
  • +
    + + Messages +
    +
  • + + )}
    +
    -
    - ); +
    + + )} + + ); }; export default Navigation;