Skip to content

Commit

Permalink
fix product page and fix standalone provider
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprudhomme committed Nov 25, 2024
1 parent 3f1fd0f commit 31e5106
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {SearchAndListingControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common.js';
import {UniversalControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common.js';
import {
InstantProducts,
InstantProductsProps,
Expand All @@ -12,7 +12,7 @@ export type {
export type {InstantProducts, InstantProductsProps};

export interface InstantProductsDefinition
extends SearchAndListingControllerDefinitionWithoutProps<InstantProducts> {}
extends UniversalControllerDefinitionWithoutProps<InstantProducts> {}

/**
* Defines the `InstantProducts` controller for the purpose of server-side rendering.
Expand All @@ -28,6 +28,7 @@ export function defineInstantProducts(
return {
listing: true,
search: true,
standalone: true,
build: (engine) => buildInstantProducts(engine, props),
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {SearchAndListingControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common.js';
import {UniversalControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common.js';
import {
RecentQueriesList,
RecentQueriesListProps,
Expand All @@ -13,7 +13,7 @@ export type {
export type {RecentQueriesList, RecentQueriesListProps};

export interface RecentQueriesListDefinition
extends SearchAndListingControllerDefinitionWithoutProps<RecentQueriesList> {}
extends UniversalControllerDefinitionWithoutProps<RecentQueriesList> {}

/**
* Defines the `RecentQueriesList` controller for the purpose of server-side rendering.
Expand All @@ -30,6 +30,7 @@ export function defineRecentQueriesList(
return {
search: true,
listing: true,
standalone: true,
build: (engine) => buildRecentQueriesList(engine, props),
};
}
4 changes: 3 additions & 1 deletion packages/samples/headless-ssr-commerce/app/cart/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
RecommendationProvider,
} from '@/components/providers/providers';
import PopularBought from '@/components/recommendations/popular-bought';
import StandaloneSearchBox from '@/components/standalone-search-box';
import {
recommendationEngineDefinition,
searchEngineDefinition,
Expand Down Expand Up @@ -45,8 +46,9 @@ export default async function Search() {
staticState={staticState}
navigatorContext={navigatorContext.marshal}
>
<div style={{display: 'flex', flexDirection: 'row'}}>
<div style={{display: 'flex', flexDirection: 'column'}}>
<ContextDropdown />
<StandaloneSearchBox />
<Cart />
<RecommendationProvider
staticState={recsStaticState}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as externalCartAPI from '@/actions/external-cart-api';
import ContextDropdown from '@/components/context-dropdown';
import ProductPage from '@/components/pages/product-page';
import {StandaloneProvider} from '@/components/providers/providers';
import StandaloneSearchBox from '@/components/standalone-search-box';
import {searchEngineDefinition} from '@/lib/commerce-engine';
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {defaultContext} from '@/utils/context';
import {headers} from 'next/headers';
import {Suspense} from 'react';

export default async function ProductDescriptionPage({
params,
searchParams,
}: {
params: {productId: string};
searchParams: Promise<{[key: string]: string | string[] | undefined}>;
}) {
// Sets the navigator context provider to use the newly created `navigatorContext` before fetching the app static state
const navigatorContext = new NextJsNavigatorContext(headers());
Expand All @@ -35,6 +35,11 @@ export default async function ProductDescriptionPage({
},
},
});

const resolvedSearchParams = await searchParams;
const price = Number(resolvedSearchParams.price) ?? NaN;
const name = resolvedSearchParams.name ?? params.productId;

return (
<StandaloneProvider
staticState={staticState}
Expand All @@ -43,13 +48,10 @@ export default async function ProductDescriptionPage({
<h2>Product description page</h2>
<ContextDropdown />
<StandaloneSearchBox />
<Suspense fallback={<p>Loading...</p>}>
<ProductPage
staticState={staticState}
navigatorContext={navigatorContext.marshal}
productId={params.productId}
/>
</Suspense>
<p>
{name} ({params.productId}) - ${price}
</p>
<br />
</StandaloneProvider>
);
}
Expand Down

This file was deleted.

0 comments on commit 31e5106

Please sign in to comment.