the-tip-top-frontend/app/employe/layout.tsx
soufiane 765a944c11 feat: add user dropdown menu in admin and employee headers
- Add dropdown menu with user avatar, name, and email in admin header
- Add dropdown menu with user avatar, name, and email in employee header
- Create admin profile page at /admin/profil
- Create employee profile page at /employe/profil
- Remove Profil link from admin sidebar (now in dropdown)
- Fix header public display: hide for admin/employee on all pages
- Use router.replace() instead of router.push() for login redirects to avoid history pollution
- Simplify employe dashboard page: remove redundant auth checks handled by layout
- Add click-outside-to-close functionality for dropdown menus

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-25 01:24:21 +01:00

183 lines
6.0 KiB
TypeScript

"use client";
import { useAuth } from "@/contexts/AuthContext";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { Loading } from "@/components/ui/Loading";
import toast from "react-hot-toast";
import { LogOut, Ticket, BarChart3, User, ChevronDown } from "lucide-react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import Logo from "@/components/Logo";
export default function EmployeLayout({
children,
}: {
children: React.ReactNode;
}) {
const { user, isAuthenticated, isLoading, logout } = useAuth();
const router = useRouter();
const pathname = usePathname();
const [dropdownOpen, setDropdownOpen] = useState(false);
useEffect(() => {
if (!isLoading && !isAuthenticated) {
router.push("/login");
return;
}
if (isAuthenticated && user?.role !== "EMPLOYEE") {
router.push("/");
toast.error("Accès refusé : rôle employé requis");
return;
}
}, [isLoading, isAuthenticated, user, router]);
// Close dropdown when clicking outside
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
const target = event.target as HTMLElement;
if (!target.closest('.user-dropdown')) {
setDropdownOpen(false);
}
};
if (dropdownOpen) {
document.addEventListener('mousedown', handleClickOutside);
}
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [dropdownOpen]);
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 !== "EMPLOYEE") {
return null;
}
const navItems = [
{
label: "Validation des Tickets",
href: "/employe/verification",
icon: <Ticket className="w-5 h-5" />,
},
{
label: "Dashboard",
href: "/employe/dashboard",
icon: <BarChart3 className="w-5 h-5" />,
},
];
const isActive = (href: string) => pathname === href;
return (
<div className="flex min-h-screen bg-gray-50">
{/* Sidebar */}
<aside className="w-64 bg-white shadow-lg border-r border-gray-200 flex flex-col">
{/* Logo/Header */}
<div className="p-6 border-b border-gray-200">
<h2 className="text-xl font-bold text-gray-900">Thé Tip Top</h2>
<p className="text-sm text-gray-500 mt-1">Espace Employé</p>
</div>
{/* Navigation */}
<nav className="flex-1 p-4 space-y-2">
{navItems.map((item) => (
<Link
key={item.href}
href={item.href}
className={`
flex items-center gap-3 px-4 py-3 rounded-lg transition-colors
${
isActive(item.href)
? "bg-green-600 text-white"
: "text-gray-700 hover:bg-gray-100"
}
`}
>
{item.icon}
<span className="font-medium">{item.label}</span>
</Link>
))}
</nav>
</aside>
{/* Main Content */}
<div className="flex-1 flex flex-col">
{/* 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 - Espace Employé
</h1>
</div>
</div>
{/* User Dropdown */}
<div className="relative user-dropdown">
<button
onClick={() => setDropdownOpen(!dropdownOpen)}
className="flex items-center gap-3 px-4 py-2 rounded-lg hover:bg-gray-100 transition-colors"
>
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-full bg-green-600 flex items-center justify-center text-white font-bold">
{user?.firstName?.charAt(0)}{user?.lastName?.charAt(0)}
</div>
<div className="text-left">
<p className="text-sm font-bold text-gray-900">
{user?.firstName} {user?.lastName}
</p>
<p className="text-xs text-gray-500">{user?.email}</p>
</div>
</div>
<ChevronDown className={`w-4 h-4 text-gray-500 transition-transform ${dropdownOpen ? 'rotate-180' : ''}`} />
</button>
{/* Dropdown Menu */}
{dropdownOpen && (
<div className="absolute right-0 mt-2 w-64 bg-white rounded-lg shadow-lg border border-gray-200 py-2 z-50">
<Link
href="/employe/profil"
onClick={() => setDropdownOpen(false)}
className="flex items-center gap-3 px-4 py-3 hover:bg-gray-100 transition-colors"
>
<User className="w-4 h-4 text-gray-600" />
<span className="text-sm font-medium text-gray-700">Profil</span>
</Link>
<div className="border-t border-gray-200 my-1"></div>
<button
onClick={handleLogout}
className="w-full flex items-center gap-3 px-4 py-3 hover:bg-red-50 transition-colors text-left"
>
<LogOut className="w-4 h-4 text-red-600" />
<span className="text-sm font-medium text-red-600">Déconnexion</span>
</button>
</div>
)}
</div>
</div>
</header>
{/* Page Content */}
<main className="flex-1 overflow-auto">{children}</main>
</div>
</div>
);
}