From 443ad590779d276dfaf0143aa5dabc22a630303a Mon Sep 17 00:00:00 2001 From: Mahen Date: Wed, 11 Feb 2026 07:51:17 +0700 Subject: [PATCH] feat: add product count endpoint --- src/app/api/product/route.ts | 25 ++++++++++++-------- src/components/dashboards/Header.tsx | 5 ++-- src/hooks/useHeader.ts | 34 +++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/app/api/product/route.ts b/src/app/api/product/route.ts index d061941..11c965c 100644 --- a/src/app/api/product/route.ts +++ b/src/app/api/product/route.ts @@ -5,16 +5,6 @@ export const dynamic = "force-dynamic"; export async function POST(request: Request) { try { - // const body = await request.json(); - - // const { name, brand } = body; - // if (!name || !brand) { - // return NextResponse.json( - // { error: "Missing required fields" }, - // { status: 400 }, - // ); - // } - const products = [ { name: "ZenBook 14", brand: "ASUS" }, { name: "Swift 3", brand: "Acer" }, @@ -40,3 +30,18 @@ export async function POST(request: Request) { ); } } + +export async function GET() { + try { + const count = await prisma.product.count(); + + return NextResponse.json({ count }, { status: 200 }); + } catch (error) { + console.error("GET /product/count error:", error); + + return NextResponse.json( + { message: "Internal server error" }, + { status: 500 }, + ); + } +} diff --git a/src/components/dashboards/Header.tsx b/src/components/dashboards/Header.tsx index 272cb2a..75f9663 100644 --- a/src/components/dashboards/Header.tsx +++ b/src/components/dashboards/Header.tsx @@ -20,7 +20,8 @@ import Link from "next/link"; import { useHeader } from "@/src/hooks/useHeader"; export function Header() { - const { open, setOpen, session, mounted } = useHeader(); + const { open, setOpen, session, mounted, productCount, loadingProductCount } = + useHeader(); if (!mounted) return null; return ( @@ -45,7 +46,7 @@ export function Header() {
- 5 Brand + {productCount} Brand
diff --git a/src/hooks/useHeader.ts b/src/hooks/useHeader.ts index ffc4aba..7cea149 100644 --- a/src/hooks/useHeader.ts +++ b/src/hooks/useHeader.ts @@ -6,6 +6,8 @@ export const useHeader = () => { const [open, setOpen] = useState(false); const session = useSession(); const [mounted, setMounted] = useState(false); + const [productCount, setProductCount] = useState(null); + const [loadingProductCount, setLoadingProductCount] = useState(false); const handleRefresh = () => { setIsRefreshing(true); @@ -14,7 +16,37 @@ export const useHeader = () => { useEffect(() => { setMounted(true); + const getProductCount = async () => { + try { + setLoadingProductCount(true); + + const res = await fetch("/api/product"); + if (!res.ok) throw new Error("Failed to fetch product count"); + + const data = await res.json(); + setProductCount(data.count); + } catch (error) { + console.error("Failed get product count:", error); + } finally { + setLoadingProductCount(false); + } + }; + + getProductCount(); }, []); - return { open, setOpen, session, isRefreshing, handleRefresh, mounted }; + useEffect(()=>{ + + }) + + return { + open, + setOpen, + session, + isRefreshing, + handleRefresh, + mounted, + productCount, + loadingProductCount, + }; };