File tree Expand file tree Collapse file tree 4 files changed +52
-19
lines changed Expand file tree Collapse file tree 4 files changed +52
-19
lines changed Original file line number Diff line number Diff line change
1
+ import { getProductByHandle } from "@lib/medusa-fetch/products"
1
2
import ProductTemplate from "@modules/products/templates"
2
3
import { Metadata } from "next"
3
4
import { notFound } from "next/navigation"
@@ -6,20 +7,13 @@ type Props = {
6
7
params : { handle : string }
7
8
}
8
9
9
- async function getProducts ( handle : string ) {
10
- const data = await fetch (
11
- `${ process . env . NEXT_PUBLIC_BASE_URL } /api/products/${ handle } `
12
- )
13
- . then ( ( res ) => res . json ( ) )
14
- . catch ( ( ) => {
15
- notFound ( )
16
- } )
17
-
18
- return data
19
- }
20
-
21
10
export async function generateMetadata ( { params } : Props ) : Promise < Metadata > {
22
- const { product } = await getProducts ( params . handle )
11
+ const { products } = await getProductByHandle ( params . handle ) . catch ( ( err ) => {
12
+ console . error ( err )
13
+ notFound ( )
14
+ } )
15
+
16
+ const product = products [ 0 ]
23
17
24
18
return {
25
19
title : `${ product . title } | Acme Store` ,
@@ -33,7 +27,12 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
33
27
}
34
28
35
29
export default async function CollectionPage ( { params } : Props ) {
36
- const { product } = await getProducts ( params . handle )
30
+ const { products } = await getProductByHandle ( params . handle ) . catch ( ( err ) => {
31
+ console . error ( err )
32
+ notFound ( )
33
+ } )
34
+
35
+ console . log ( products )
37
36
38
- return < ProductTemplate product = { product } />
37
+ return < ProductTemplate product = { products [ 0 ] } />
39
38
}
Original file line number Diff line number Diff line change @@ -8,7 +8,9 @@ export async function GET(
8
8
{ params } : { params : Record < string , any > }
9
9
) {
10
10
const { handle } = params
11
- const { cart_id } = Object . fromEntries ( request . nextUrl . searchParams )
11
+ const { cart_id, region_id } = Object . fromEntries (
12
+ request . nextUrl . searchParams
13
+ )
12
14
const productService = await initializeProductModule ( )
13
15
14
16
const data = await productService . list (
@@ -29,7 +31,7 @@ export async function GET(
29
31
}
30
32
)
31
33
32
- const productsWithPrices = await getPrices ( data , cart_id )
34
+ const productsWithPrices = await getPrices ( data , cart_id , region_id )
33
35
34
- return NextResponse . json ( { product : productsWithPrices [ 0 ] } )
36
+ return NextResponse . json ( { products : productsWithPrices } )
35
37
}
Original file line number Diff line number Diff line change
1
+ import medusaRequest from "./"
2
+
3
+ const PRODUCT_MODULE_ENABLED = process . env . FEATURE_PRODUCTMODULE_ENABLED
4
+
5
+ export async function getProductByHandle ( handle : string ) {
6
+ if ( PRODUCT_MODULE_ENABLED ) {
7
+ console . log ( "PRODUCT_MODULE_ENABLED" )
8
+ const data = await fetch (
9
+ `${ process . env . NEXT_PUBLIC_BASE_URL } /api/products/${ handle } `
10
+ )
11
+ . then ( ( res ) => res . json ( ) )
12
+ . catch ( ( err ) => {
13
+ throw err
14
+ } )
15
+
16
+ return data
17
+ }
18
+
19
+ console . log ( "PRODUCT_MODULE_DISABLED" )
20
+ const data = await medusaRequest ( "GET" , "/products" , {
21
+ query : {
22
+ handle,
23
+ } ,
24
+ } )
25
+
26
+ return {
27
+ products : data . body . products ,
28
+ status : data . status ,
29
+ ok : data . ok ,
30
+ }
31
+ }
Original file line number Diff line number Diff line change 1
1
{
2
2
"features" : {
3
- "search" : false
3
+ "search" : false ,
4
+ "productModule" : false
4
5
}
5
6
}
You can’t perform that action at this time.
0 commit comments