feat: enhance Header component with user profile fetching and admin button, update ThemeSwitcher to use Button component
This commit is contained in:
parent
c325305442
commit
37a34dc004
@ -1,6 +1,6 @@
|
|||||||
"use client";
|
|
||||||
import {
|
import {
|
||||||
Avatar,
|
Avatar,
|
||||||
|
Button,
|
||||||
Dropdown,
|
Dropdown,
|
||||||
DropdownItem,
|
DropdownItem,
|
||||||
DropdownMenu,
|
DropdownMenu,
|
||||||
@ -8,9 +8,13 @@ import {
|
|||||||
Navbar,
|
Navbar,
|
||||||
NavbarBrand,
|
NavbarBrand,
|
||||||
NavbarContent,
|
NavbarContent,
|
||||||
|
NavbarItem,
|
||||||
} from "@nextui-org/react";
|
} from "@nextui-org/react";
|
||||||
import { useSession } from "next-auth/react";
|
import { useSession } from "next-auth/react";
|
||||||
import { ThemeSwitcher } from "../ThemeSwitcher/ThemeSwitcher";
|
import { ThemeSwitcher } from "../ThemeSwitcher/ThemeSwitcher";
|
||||||
|
import { axiosInstance } from "@/app/lib/axios";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
|
||||||
const getInitials = (name: string) => {
|
const getInitials = (name: string) => {
|
||||||
if (!name) return "";
|
if (!name) return "";
|
||||||
@ -28,14 +32,36 @@ const getInitials = (name: string) => {
|
|||||||
|
|
||||||
export const Header = () => {
|
export const Header = () => {
|
||||||
const { data: session } = useSession();
|
const { data: session } = useSession();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const [userProfile, setUserProfile] = useState<{
|
||||||
|
id: string;
|
||||||
|
username: string;
|
||||||
|
role: "ADMIN" | "STUDENT";
|
||||||
|
}>();
|
||||||
|
|
||||||
const initials = session?.user?.name ? getInitials(session.user.name) : "";
|
const initials = session?.user?.name ? getInitials(session.user.name) : "";
|
||||||
|
|
||||||
|
const fetchUserProfile = async () => {
|
||||||
|
return await axiosInstance<{
|
||||||
|
id: string;
|
||||||
|
username: string;
|
||||||
|
role: "ADMIN" | "STUDENT";
|
||||||
|
}>("/@me");
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchUserProfile().then(r => {
|
||||||
|
setUserProfile(r.data);
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Navbar className="mb-2">
|
<Navbar className="mb-2">
|
||||||
<NavbarBrand>
|
<NavbarBrand>
|
||||||
<p className="font-bold text-inherit">Toogether</p>
|
<p className="font-bold text-inherit">Toogether</p>
|
||||||
</NavbarBrand>
|
</NavbarBrand>
|
||||||
|
|
||||||
<NavbarContent as="div" justify="end">
|
<NavbarContent as="div" justify="end">
|
||||||
<Dropdown placement="bottom-end">
|
<Dropdown placement="bottom-end">
|
||||||
<DropdownTrigger>
|
<DropdownTrigger>
|
||||||
@ -65,7 +91,20 @@ export const Header = () => {
|
|||||||
</DropdownItem>
|
</DropdownItem>
|
||||||
</DropdownMenu>
|
</DropdownMenu>
|
||||||
</Dropdown>
|
</Dropdown>
|
||||||
<ThemeSwitcher />
|
<NavbarItem>
|
||||||
|
<ThemeSwitcher />
|
||||||
|
</NavbarItem>
|
||||||
|
{
|
||||||
|
userProfile?.role === "ADMIN" ? (
|
||||||
|
<NavbarItem>
|
||||||
|
<Button
|
||||||
|
onPress={() => router.push("/admin")}
|
||||||
|
>
|
||||||
|
🔧
|
||||||
|
</Button>
|
||||||
|
</NavbarItem>
|
||||||
|
) : null
|
||||||
|
}
|
||||||
</NavbarContent>
|
</NavbarContent>
|
||||||
</Navbar>
|
</Navbar>
|
||||||
);
|
);
|
||||||
|
@ -1,24 +1,13 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import { Button } from "@nextui-org/react";
|
||||||
import { useTheme } from "next-themes";
|
import { useTheme } from "next-themes";
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
|
|
||||||
export const ThemeSwitcher = () => {
|
export const ThemeSwitcher = () => {
|
||||||
const [mounted, setMounted] = useState(false);
|
|
||||||
const { theme, setTheme } = useTheme();
|
const { theme, setTheme } = useTheme();
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setMounted(true);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (!mounted) return null;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<Button onPress={() => setTheme(theme === "light" ? "dark" : "light")}>
|
||||||
className={`backdrop-blur-sm rounded-lg ${theme === "light" ? "bg-black/10" : "bg-white/10"
|
{theme === "light" ? "🌑" : "☀️"}
|
||||||
} p-2`}
|
</Button>
|
||||||
onClick={() => setTheme(theme === "light" ? "dark" : "light")}
|
|
||||||
>
|
|
||||||
{theme === "light" ? "🌙" : "☀️"}
|
|
||||||
</button>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user