2026-05-18 15:25:38 +02:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
|
|
import Link from 'next/link'
|
|
|
|
|
import { useState, useEffect } from 'react'
|
|
|
|
|
import { useRouter } from 'next/navigation'
|
2026-05-19 10:10:17 +02:00
|
|
|
import { useUser } from '@/context/UserContext'
|
2026-05-18 15:25:38 +02:00
|
|
|
|
|
|
|
|
export function Navbar() {
|
|
|
|
|
const [cartCount, setCartCount] = useState(0)
|
2026-05-19 10:10:17 +02:00
|
|
|
const { user, refreshUser } = useUser()
|
2026-05-18 15:25:38 +02:00
|
|
|
const router = useRouter()
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const cart = JSON.parse(localStorage.getItem('cart') || '[]')
|
|
|
|
|
const count = cart.reduce((sum: number, item: { quantity: number }) => sum + item.quantity, 0)
|
|
|
|
|
setCartCount(count)
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
async function handleLogout() {
|
|
|
|
|
await fetch('/api/auth/logout', { method: 'POST' })
|
2026-05-19 10:10:17 +02:00
|
|
|
await refreshUser()
|
2026-05-18 15:25:38 +02:00
|
|
|
router.push('/')
|
|
|
|
|
router.refresh()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<header className="bg-white border-b border-gray-200">
|
|
|
|
|
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
|
|
|
|
<div className="flex items-center justify-between h-16">
|
|
|
|
|
<Link href="/" className="text-xl font-bold text-gray-900">
|
|
|
|
|
ShopX
|
|
|
|
|
</Link>
|
|
|
|
|
|
|
|
|
|
<nav className="flex items-center gap-6 text-sm">
|
|
|
|
|
<Link href="/products" className="text-gray-600 hover:text-gray-900">
|
|
|
|
|
Products
|
|
|
|
|
</Link>
|
|
|
|
|
<Link href="/cart" className="text-gray-600 hover:text-gray-900 relative">
|
|
|
|
|
Cart
|
|
|
|
|
{cartCount > 0 && (
|
|
|
|
|
<span className="ml-1 bg-blue-600 text-white text-xs px-1.5 py-0.5 rounded-full">
|
|
|
|
|
{cartCount}
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</Link>
|
|
|
|
|
{user ? (
|
|
|
|
|
<>
|
|
|
|
|
<Link href="/account" className="text-gray-600 hover:text-gray-900">
|
|
|
|
|
Account
|
|
|
|
|
</Link>
|
|
|
|
|
{(user.role === 'ADMIN' || user.role === 'OWNER') && (
|
|
|
|
|
<Link href="/admin" className="text-gray-600 hover:text-gray-900">
|
|
|
|
|
Admin
|
|
|
|
|
</Link>
|
|
|
|
|
)}
|
|
|
|
|
<button
|
|
|
|
|
onClick={handleLogout}
|
|
|
|
|
className="text-gray-600 hover:text-gray-900"
|
|
|
|
|
>
|
|
|
|
|
Logout
|
|
|
|
|
</button>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<Link href="/login" className="text-gray-600 hover:text-gray-900">
|
|
|
|
|
Login
|
|
|
|
|
</Link>
|
|
|
|
|
<Link
|
|
|
|
|
href="/register"
|
|
|
|
|
className="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700 transition-colors"
|
|
|
|
|
>
|
|
|
|
|
Register
|
|
|
|
|
</Link>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</nav>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</header>
|
|
|
|
|
)
|
|
|
|
|
}
|