Skip to content

Commit b2df1de

Browse files
committed
feat: product page
1 parent 6433303 commit b2df1de

File tree

12 files changed

+73
-132
lines changed

12 files changed

+73
-132
lines changed
File renamed without changes.
File renamed without changes.

src/app/_products/[handle]/layout.tsx

Whitespace-only changes.

src/app/_products/[handle]/loading.tsx

Whitespace-only changes.

src/app/_products/[handle]/page.tsx

Whitespace-only changes.

src/app/collections/route.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ export async function GET(request: NextRequest) {
3434
}
3535

3636
async function fetchCollection(handle: string) {
37-
return await medusaRequest("GET", `/collections?handle[]=${handle}`).then(
38-
({ body }) => ({
39-
id: body.collections[0].id,
40-
handle: body.collections[0].handle,
41-
title: body.collections[0].title,
42-
})
43-
)
37+
return await medusaRequest("GET", "/collections", {
38+
query: { "handle[]": handle },
39+
}).then(({ body }) => ({
40+
id: body.collections[0].id,
41+
handle: body.collections[0].handle,
42+
title: body.collections[0].title,
43+
}))
4444
}
4545

4646
async function fetchCollectionProducts({
@@ -54,16 +54,15 @@ async function fetchCollectionProducts({
5454
limit: string
5555
cart_id: string
5656
}) {
57-
const { products, count, offset } = await medusaRequest(
58-
"GET",
59-
`/products?collection_id[]=${id}`,
60-
{
57+
const { products, count, offset } = await medusaRequest("GET", "/products", {
58+
query: {
59+
"collection_id[]": id,
6160
cart_id,
6261
limit,
6362
offset: pageParam,
6463
expand: "variants,variants.prices",
65-
}
66-
).then((res) => res.body)
64+
},
65+
}).then((res) => res.body)
6766

6867
return {
6968
products,

src/app/products/[handle]/layout.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import PageLayout from "app/page-layout"
2+
3+
export default function ProductLayout({
4+
children,
5+
}: {
6+
children: React.ReactNode
7+
}) {
8+
return <PageLayout>{children}</PageLayout>
9+
}

src/app/products/[handle]/loading.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import SkeletonProductPage from "@modules/skeletons/templates/skeleton-product-page"
2+
3+
export default function Loading() {
4+
return <SkeletonProductPage />
5+
}

src/app/products/[handle]/page.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import medusaRequest from "@lib/medusa-fetch"
2+
import ProductTemplate from "@modules/products/templates"
3+
import { Metadata } from "next"
4+
5+
type Props = {
6+
params: { handle: string }
7+
}
8+
9+
export async function generateMetadata({ params }: Props): Promise<Metadata> {
10+
const { products } = await medusaRequest("GET", "/products", {
11+
query: {
12+
handle: params.handle,
13+
// expand: "variants,variants.prices,thumbnail",
14+
},
15+
}).then((res) => res.body)
16+
17+
const product = products[0]
18+
19+
return {
20+
title: `${products.title} | Acme Store`,
21+
description: `${product.title}`,
22+
openGraph: {
23+
title: `${product.title} | Acme Store`,
24+
description: `${product.title}`,
25+
images: product.thumbnail ? [product.thumbnail] : [],
26+
},
27+
}
28+
}
29+
30+
export default async function CollectionPage({ params }: Props) {
31+
const { products } = await medusaRequest("GET", "/products", {
32+
query: {
33+
handle: params.handle,
34+
// expand: "variants,variants.prices",
35+
},
36+
}).then((res) => res.body)
37+
38+
return <ProductTemplate product={products[0]} />
39+
}

src/lib/medusa-fetch/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const ENDPOINT =
66
export default async function medusaRequest(
77
method: string,
88
path = "",
9-
payload?: Record<string, string> | undefined
9+
payload?: Record<string, any> | undefined
1010
) {
1111
const options: RequestInit = {
1212
method,
@@ -18,15 +18,11 @@ export default async function medusaRequest(
1818
}
1919

2020
if (payload) {
21-
if (method === "POST") {
22-
options.body = JSON.stringify(payload)
21+
if ("body" in payload) {
22+
options.body = JSON.stringify(payload.body)
2323
}
24-
if (method === "GET") {
25-
// if the path has a query string, append the payload to it. if not, create a new query string
26-
const pathWithQuery = path.includes("?")
27-
? `${path}&${new URLSearchParams(payload).toString()}`
28-
: `${path}?${new URLSearchParams(payload).toString()}`
29-
path = pathWithQuery
24+
if ("query" in payload) {
25+
path = `${path}?${new URLSearchParams(payload.query).toString()}`
3026
}
3127
}
3228

0 commit comments

Comments
 (0)