feat: add product count endpoint
This commit is contained in:
parent
dafd5ca196
commit
443ad59077
|
|
@ -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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() {
|
|||
<div className="hidden items-center gap-6 text-sm md:flex">
|
||||
<div className="flex items-center gap-2 text-muted-foreground">
|
||||
<Laptop className="h-4 w-4" />
|
||||
<span>5 Brand</span>
|
||||
<span>{productCount} Brand</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-muted-foreground">
|
||||
<Database className="h-4 w-4" />
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ export const useHeader = () => {
|
|||
const [open, setOpen] = useState(false);
|
||||
const session = useSession();
|
||||
const [mounted, setMounted] = useState(false);
|
||||
const [productCount, setProductCount] = useState<number | null>(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,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue