File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
packages/samples/headless-ssr-commerce Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 1
1
import * as externalCartAPI from '@/actions/external-cart-api' ;
2
2
import ContextDropdown from '@/components/context-dropdown' ;
3
+ import ProductViewer from '@/components/product-viewer' ;
3
4
import {
4
5
RecommendationProvider ,
5
6
StandaloneProvider ,
@@ -64,7 +65,9 @@ export default async function ProductDescriptionPage({
64
65
65
66
const resolvedSearchParams = await searchParams ;
66
67
const price = Number ( resolvedSearchParams . price ) ?? NaN ;
67
- const name = resolvedSearchParams . name ?? params . productId ;
68
+ const name = Array . isArray ( resolvedSearchParams . name )
69
+ ? params . productId
70
+ : ( resolvedSearchParams . name ?? params . productId ) ;
68
71
69
72
return (
70
73
< StandaloneProvider
@@ -74,6 +77,7 @@ export default async function ProductDescriptionPage({
74
77
< h2 > Product description page</ h2 >
75
78
< ContextDropdown />
76
79
< StandaloneSearchBox />
80
+ < ProductViewer productId = { params . productId } name = { name } price = { price } />
77
81
< p >
78
82
{ name } ({ params . productId } ) - ${ price }
79
83
</ p >
Original file line number Diff line number Diff line change
1
+ 'use client' ;
2
+
3
+ import { useProductView } from '@/lib/commerce-engine' ;
4
+ import { useEffect } from 'react' ;
5
+
6
+ interface Product {
7
+ productId : string ;
8
+ name : string ;
9
+ price : number ;
10
+ }
11
+
12
+ export default function ProductViewer ( { productId, name, price} : Product ) {
13
+ const { methods} = useProductView ( ) ;
14
+ let productViewEventEmitted = false ;
15
+
16
+ useEffect ( ( ) => {
17
+ if ( methods && ! productViewEventEmitted ) {
18
+ methods ?. view ( { productId, name, price} ) ;
19
+ productViewEventEmitted = true ;
20
+ }
21
+ } , [ ] ) ;
22
+
23
+ return null ;
24
+ }
You can’t perform that action at this time.
0 commit comments