Skip to content

Commit a917741

Browse files
committed
feat: update api call for new category structure
1 parent cc1cc2e commit a917741

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

src/app/(main)/[...category]/page.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ type Props = {
88
}
99

1010
export async function generateMetadata({ params }: Props): Promise<Metadata> {
11-
const handle = params.category.join("/")
12-
13-
const { product_categories } = await getCategoryByHandle(handle).catch(
14-
(err) => {
15-
notFound()
16-
}
17-
)
11+
const { product_categories } = await getCategoryByHandle(
12+
params.category
13+
).catch((err) => {
14+
notFound()
15+
})
1816

1917
const category = product_categories[0]
2018

@@ -25,12 +23,11 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
2523
}
2624

2725
export default async function CategoryPage({ params }: Props) {
28-
const handle = params.category.join("/")
29-
const { product_categories } = await getCategoryByHandle(handle).catch(
30-
(err) => {
31-
notFound()
32-
}
33-
)
26+
const { product_categories } = await getCategoryByHandle(
27+
params.category
28+
).catch((err) => {
29+
notFound()
30+
})
3431

3532
return <CategoryTemplate categories={product_categories} />
3633
}

src/lib/data/index.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,17 @@ export async function getCategoriesList(
323323

324324
/**
325325
* Fetches a category by handle, using the Medusa API or the Medusa Product Module, depending on the feature flag.
326-
* @param handle (string) - The handle of the category to retrieve
326+
* @param categoryHandle (string) - The handle of the category to retrieve
327327
* @returns collections (array) - An array of categories (should only be one)
328328
* @returns response (object) - An object containing the products and the number of products in the category
329329
* @returns nextPage (number) - The offset of the next page of products
330330
*/
331-
export async function getCategoryByHandle(handle: string): Promise<{
331+
export async function getCategoryByHandle(categoryHandle: string[]): Promise<{
332332
product_categories: ProductCategoryWithChildren[]
333-
parent: ProductCategoryWithChildren
334333
}> {
335334
if (PRODUCT_MODULE_ENABLED) {
336335
DEBUG && console.log("PRODUCT_MODULE_ENABLED")
337-
const data = await fetch(`${API_BASE_URL}/api/categories/${handle}`)
336+
const data = await fetch(`${API_BASE_URL}/api/categories/${categoryHandle}`)
338337
.then((res) => res.json())
339338
.catch((err) => {
340339
throw err
@@ -344,19 +343,29 @@ export async function getCategoryByHandle(handle: string): Promise<{
344343
}
345344

346345
DEBUG && console.log("PRODUCT_MODULE_DISABLED")
347-
const data = await medusaRequest("GET", "/product-categories", {
348-
query: {
349-
handle,
350-
},
346+
347+
const handles = categoryHandle.map((handle: string, index: number) => {
348+
return categoryHandle.slice(0, index + 1).join("/")
351349
})
352-
.then((res) => res.body)
353-
.catch((err) => {
354-
throw err
350+
351+
const product_categories = [] as ProductCategoryWithChildren[]
352+
353+
for (const handle of handles) {
354+
await medusaRequest("GET", "/product-categories", {
355+
query: {
356+
handle,
357+
},
355358
})
359+
.then(({ body }) => {
360+
product_categories.push(body.product_categories[0])
361+
})
362+
.catch((err) => {
363+
throw err
364+
})
365+
}
356366

357367
return {
358-
product_categories: data.product_categories,
359-
parent: data.product_categories[0].parent_category,
368+
product_categories,
360369
}
361370
}
362371

@@ -399,7 +408,7 @@ export async function getProductsByCategoryHandle({
399408
}
400409

401410
DEBUG && console.log("PRODUCT_MODULE_DISABLED")
402-
const { id } = await getCategoryByHandle(handle).then(
411+
const { id } = await getCategoryByHandle([handle]).then(
403412
(res) => res.product_categories[0]
404413
)
405414

0 commit comments

Comments
 (0)