From 71dbcad13bf2f1ed8bf617e6bcd6bcf5ce72263f Mon Sep 17 00:00:00 2001 From: Zynthasius39 Date: Sat, 11 Jan 2025 03:00:56 +0400 Subject: [PATCH] Dynamic page title --- src/Components.tsx | 33 +++++++++++++++++++++++++++++-- src/components/App.tsx | 17 +++++++++++----- src/components/DrawerList.tsx | 37 ++--------------------------------- src/components/Navbar.tsx | 2 +- 4 files changed, 46 insertions(+), 43 deletions(-) diff --git a/src/Components.tsx b/src/Components.tsx index 20e394d..2a6912e 100644 --- a/src/Components.tsx +++ b/src/Components.tsx @@ -1,4 +1,16 @@ -import { Button, styled, Switch, SwitchProps } from "@mui/material"; +import { Button, styled, Switch } from "@mui/material"; +import { + AnnouncementTwoTone, + ArticleTwoTone, + CalendarMonthTwoTone, + DashboardTwoTone, + DoorSlidingTwoTone, + FolderTwoTone, + GradeTwoTone, + GradingTwoTone, + SettingsTwoTone, + SummarizeTwoTone, +} from "@mui/icons-material"; export const MaterialUISwitch = styled(Switch)(({ theme }) => ({ width: 62, @@ -125,4 +137,21 @@ export const PrimaryButton = styled(Button, { ":disabled": { backgroundColor: theme.palette.primaryButton.dark, } -})); \ No newline at end of file +})); + +export const spMenu = [ + { name: "Dashboard", icon: , href: "/" }, + { + name: "Announces", + icon: , + href: "/announces", + }, + { name: "Departments", icon: , href: "" }, + { name: "Course", icon: , href: "" }, + { name: "Grades", icon: , href: "/grades" }, + { name: "Transcript", icon: , href: "" }, + { name: "Attendance", icon: , href: "" }, + { name: "Gate", icon: , href: "" }, + { name: "Documents", icon: , href: "" }, + { name: "Settings", icon: , href: "" }, +]; \ No newline at end of file diff --git a/src/components/App.tsx b/src/components/App.tsx index 0e4626d..7f64704 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -1,17 +1,19 @@ -import { useEffect, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import { Box, Stack } from "@mui/material"; import Navbar from "./Navbar"; -import { Outlet, useNavigate } from "react-router-dom"; +import { Outlet, useLocation, useNavigate } from "react-router-dom"; import DrawerList from "./DrawerList"; import { useTheme } from "../utils/Theme"; import { useAuth } from "../utils/Auth"; import { verify } from "../utils/Api"; +import { spMenu } from "../Components"; export default function App() { const { authed, imageURL, name, logout, verifiedAuth } = useAuth(); const { theme } = useTheme(); const navigate = useNavigate(); - const [page] = useState("Dashboard"); + const [page, setPage] = useState("Dashboard"); + const path = useLocation(); const getComponent = async () => { if (!await verify()) { @@ -22,11 +24,16 @@ export default function App() { } } + const updatePage = () => { + setPage(spMenu.find(m => m.href === path.pathname)?.name || ""); + } + useEffect(() => { + updatePage(); if (!authed) { getComponent(); } - }, [name, imageURL]); + }, [name, imageURL, path]); return ( - + diff --git a/src/components/DrawerList.tsx b/src/components/DrawerList.tsx index 2ccc4b5..86a4bbe 100644 --- a/src/components/DrawerList.tsx +++ b/src/components/DrawerList.tsx @@ -1,16 +1,3 @@ -import { - AnnouncementTwoTone, - ArticleTwoTone, - CalendarMonthTwoTone, - DashboardTwoTone, - DoorSlidingTwoTone, - FolderTwoTone, - GradeTwoTone, - GradingTwoTone, - LogoutTwoTone, - SettingsTwoTone, - SummarizeTwoTone, -} from "@mui/icons-material"; import { Stack, List, @@ -20,30 +7,14 @@ import { Divider, ListItemIcon, Box, - Avatar, } from "@mui/material"; import logo_dark from "../assets/beu_dark.svg"; import { NavLink, useLocation, useNavigate } from "react-router-dom"; import { useTheme } from "../utils/Theme"; import { useCallback } from "react"; import { useAuth } from "../utils/Auth"; - -const spMenu = [ - { name: "Dashboard", icon: , href: "/" }, - { - name: "Announces", - icon: , - href: "/announces", - }, - { name: "Departments", icon: , href: "" }, - { name: "Course", icon: , href: "" }, - { name: "Grades", icon: , href: "/grades" }, - { name: "Transcript", icon: , href: "" }, - { name: "Attendance", icon: , href: "" }, - { name: "Gate", icon: , href: "" }, - { name: "Documents", icon: , href: "" }, - { name: "Settings", icon: , href: "" }, -]; +import { spMenu } from "../Components"; +import { LogoutTwoTone } from "@mui/icons-material"; const DrawerList = () => { const { logout, setImage } = useAuth(); @@ -120,9 +91,7 @@ const DrawerList = () => { }} > - {/* */} {icon} - {/* */} {name} @@ -132,9 +101,7 @@ const DrawerList = () => { - {/* */} - {/* */} LogOut diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index 05ee200..7b302d0 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -87,7 +87,7 @@ const Navbar = (props: { name: string; page: string }) => { {props.page}