- Create reusable UserDropdown component for user menu - Create useClickOutside hook for click-outside detection - Refactor admin/layout.tsx and employe/layout.tsx to use shared components - Reduces code duplication between layouts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
"use client";
|
|
|
|
import Sidebar from "@/components/admin/Sidebar";
|
|
import { useAuth } from "@/contexts/AuthContext";
|
|
import { useRouter } from "next/navigation";
|
|
import { useEffect } from "react";
|
|
import { Loading } from "@/components/ui/Loading";
|
|
import toast from "react-hot-toast";
|
|
import Logo from "@/components/Logo";
|
|
import UserDropdown from "@/components/UserDropdown";
|
|
|
|
export default function AdminLayout({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode;
|
|
}) {
|
|
const { user, isAuthenticated, isLoading, logout } = useAuth();
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
if (!isLoading && !isAuthenticated) {
|
|
router.push("/login");
|
|
return;
|
|
}
|
|
|
|
if (isAuthenticated && user?.role !== "ADMIN") {
|
|
router.push("/");
|
|
toast.error("Accès refusé : rôle administrateur requis");
|
|
return;
|
|
}
|
|
}, [isLoading, isAuthenticated, user, router]);
|
|
|
|
const handleLogout = async () => {
|
|
await logout();
|
|
router.push("/login");
|
|
};
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-gray-50">
|
|
<Loading size="lg" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!isAuthenticated || user?.role !== "ADMIN") {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<div className="flex min-h-screen bg-gray-50">
|
|
<Sidebar />
|
|
<div className="flex-1 flex flex-col">
|
|
{/* Admin Header */}
|
|
<header className="bg-white shadow-sm border-b border-gray-200 px-6 py-4">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-4">
|
|
<Logo size="md" showText={false} />
|
|
<div>
|
|
<h1 className="text-2xl font-bold text-gray-900">Thé Tip Top - Administration</h1>
|
|
</div>
|
|
</div>
|
|
|
|
<UserDropdown
|
|
user={user}
|
|
profilePath="/admin/profil"
|
|
onLogout={handleLogout}
|
|
accentColor="blue"
|
|
/>
|
|
</div>
|
|
</header>
|
|
|
|
{/* Main Content */}
|
|
<main className="flex-1 overflow-auto">{children}</main>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|