another one
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
import { notFound } from "next/navigation"
|
||||
import { ProductDetail } from "@/components/product-detail"
|
||||
import { SAMPLE_PRODUCTS } from "@/lib/sample-products"
|
||||
import { cookies } from "next/headers"
|
||||
|
||||
async function getProduct(id: string, isDraft: boolean) {
|
||||
const product = SAMPLE_PRODUCTS.find((p) => p.id === Number.parseInt(id))
|
||||
async function getProduct(id: string) {
|
||||
try {
|
||||
const response = await fetch(`http://localhost:8080/product/${id}`, {
|
||||
cache: "no-store",
|
||||
})
|
||||
|
||||
if (!product) {
|
||||
if (!response.ok) {
|
||||
if (response.status === 404) {
|
||||
return null
|
||||
}
|
||||
throw new Error("Failed to fetch product")
|
||||
}
|
||||
|
||||
return response.json()
|
||||
} catch (error) {
|
||||
console.error("Error fetching product:", error)
|
||||
return null
|
||||
}
|
||||
|
||||
if (isDraft) {
|
||||
return {
|
||||
...product,
|
||||
title: `[Draft] ${product.title}`,
|
||||
price: product.price * 0.9,
|
||||
}
|
||||
}
|
||||
|
||||
return product
|
||||
}
|
||||
|
||||
interface ProductPageProps {
|
||||
@@ -26,12 +26,7 @@ interface ProductPageProps {
|
||||
}
|
||||
|
||||
export default async function ProductPage({ params }: ProductPageProps) {
|
||||
// Get cookies first
|
||||
const cookieStore = await cookies()
|
||||
const isDraftMode = cookieStore.get("__prerender_bypass")?.value === process.env.PRERENDER_BYPASS_TOKEN
|
||||
|
||||
// Then get product
|
||||
const product = await getProduct(params.id, isDraftMode)
|
||||
const product = await getProduct(params.id)
|
||||
|
||||
if (!product) {
|
||||
notFound()
|
||||
@@ -40,7 +35,6 @@ export default async function ProductPage({ params }: ProductPageProps) {
|
||||
return (
|
||||
<div className="container mx-auto px-4 py-8">
|
||||
<ProductDetail product={product} />
|
||||
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user