feat: enhance Header component with user profile fetching and admin button, update ThemeSwitcher to use Button component

This commit is contained in:
Rémi 2025-01-02 22:11:09 +01:00
parent c325305442
commit 37a34dc004
2 changed files with 45 additions and 17 deletions

View File

@ -1,6 +1,6 @@
"use client";
import {
Avatar,
Button,
Dropdown,
DropdownItem,
DropdownMenu,
@ -8,9 +8,13 @@ import {
Navbar,
NavbarBrand,
NavbarContent,
NavbarItem,
} from "@nextui-org/react";
import { useSession } from "next-auth/react";
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) => {
if (!name) return "";
@ -28,14 +32,36 @@ const getInitials = (name: string) => {
export const Header = () => {
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 fetchUserProfile = async () => {
return await axiosInstance<{
id: string;
username: string;
role: "ADMIN" | "STUDENT";
}>("/@me");
}
useEffect(() => {
fetchUserProfile().then(r => {
setUserProfile(r.data);
});
}, []);
return (
<Navbar className="mb-2">
<NavbarBrand>
<p className="font-bold text-inherit">Toogether</p>
</NavbarBrand>
<NavbarContent as="div" justify="end">
<Dropdown placement="bottom-end">
<DropdownTrigger>
@ -65,7 +91,20 @@ export const Header = () => {
</DropdownItem>
</DropdownMenu>
</Dropdown>
<ThemeSwitcher />
<NavbarItem>
<ThemeSwitcher />
</NavbarItem>
{
userProfile?.role === "ADMIN" ? (
<NavbarItem>
<Button
onPress={() => router.push("/admin")}
>
🔧
</Button>
</NavbarItem>
) : null
}
</NavbarContent>
</Navbar>
);

View File

@ -1,24 +1,13 @@
"use client";
import { Button } from "@nextui-org/react";
import { useTheme } from "next-themes";
import { useEffect, useState } from "react";
export const ThemeSwitcher = () => {
const [mounted, setMounted] = useState(false);
const { theme, setTheme } = useTheme();
useEffect(() => {
setMounted(true);
}, []);
if (!mounted) return null;
return (
<button
className={`backdrop-blur-sm rounded-lg ${theme === "light" ? "bg-black/10" : "bg-white/10"
} p-2`}
onClick={() => setTheme(theme === "light" ? "dark" : "light")}
>
{theme === "light" ? "🌙" : "☀️"}
</button>
<Button onPress={() => setTheme(theme === "light" ? "dark" : "light")}>
{theme === "light" ? "🌑" : "☀️"}
</Button>
);
};