Skip to content

Commit

Permalink
chore(headless-ssr-commerce-samples): add multiple listing pages
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprudhomme committed Nov 5, 2024
1 parent 98b8db7 commit 1d8f07f
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {UniversalControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common.js';
import {UniversalControllerDefinitionWithProps} from '../../../app/commerce-ssr-engine/types/common.js';
import {
Context,
buildContext,
ContextProps,
ContextOptions,
View,
UserLocation,
Expand All @@ -12,7 +11,7 @@ export type {ContextState, Context, ContextProps} from './headless-context.js';
export type {View, UserLocation, ContextOptions};

export interface ContextDefinition
extends UniversalControllerDefinitionWithoutProps<Context> {}
extends UniversalControllerDefinitionWithProps<Context, ContextOptions> {}

/**
* Defines a `Context` controller instance.
Expand All @@ -22,11 +21,11 @@ export interface ContextDefinition
*
* @internal
*/
export function defineContext(props: ContextProps = {}): ContextDefinition {
export function defineContext(): ContextDefinition {
return {
listing: true,
search: true,
standalone: true,
build: (engine) => buildContext(engine, props),
buildWithProps: (engine, props) => buildContext(engine, {options: props}),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,31 @@ import Summary from '@/components/summary';
import {listingEngineDefinition} from '@/lib/commerce-engine';
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {headers} from 'next/headers';
import {notFound} from 'next/navigation';

//TODO: add comments
const categoryMap: {[key: string]: string} = {
'surf-accessories': 'surf-accessories',
'accessories/towels': 'accessories-towels',
'clothing/pants': 'clothing-pants',
};

/**
* This file defines a List component that uses the Coveo Headless SSR commerce library to manage its state.
*
* The Listing function is the entry point for server-side rendering (SSR).
*/
export default async function Listing() {
export default async function Listing({params}: {params: {category: string}}) {
const {category} = params;

const matchedCategory = Object.keys(categoryMap).find(
(key: string) => categoryMap[key] === category
);

if (!matchedCategory) {
notFound();
}

// Sets the navigator context provider to use the newly created `navigatorContext` before fetching the app static state
const navigatorContext = new NextJsNavigatorContext(headers());
listingEngineDefinition.setNavigatorContextProvider(() => navigatorContext);
Expand All @@ -28,7 +46,17 @@ export default async function Listing() {

// Fetches the static state of the app with initial state (when applicable)
const staticState = await listingEngineDefinition.fetchStaticState({
controllers: {cart: {initialState: {items}}},
controllers: {
cart: {initialState: {items}},
context: {
language: 'en',
country: 'US',
currency: 'USD',
view: {
url: `https://sports.barca.group/browse/promotions/${matchedCategory}`,
},
},
},
});

return (
Expand Down
4 changes: 3 additions & 1 deletion packages/samples/headless-ssr-commerce/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export default function RootLayout({children}: {children: React.ReactNode}) {
<h1>Coveo Headless Commerce Next.js</h1>
<div style={{display: 'flex', alignItems: 'center', gap: '10px'}}>
<Link href={'/search'}>Search Page</Link>
<Link href={'/listing'}>Listing Page</Link>
<Link href={'/surf-accessories'}>Listing Page Surf</Link>
<Link href={'/accessories-towels'}>Listing Page Towels</Link>
<Link href={'/clothing-pants'}>Listing Page Pants</Link>
<Link href={'/cart'}>Cart Page</Link>
</div>
{children}
Expand Down
13 changes: 12 additions & 1 deletion packages/samples/headless-ssr-commerce/app/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,19 @@ export default async function Search() {

// Fetches the static state of the app with initial state (when applicable)
const staticState = await searchEngineDefinition.fetchStaticState({
controllers: {cart: {initialState: {items}}},
controllers: {
cart: {initialState: {items}},
context: {
language: 'en',
country: 'US',
currency: 'USD',
view: {
url: 'https://sports.barca.group/browse/search',
},
},
},
});

return (
<SearchProvider
staticState={staticState}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {useCart, useProductList} from '@/lib/commerce-engine';
import {addToCart} from '@/utils/cart';
import {Product} from '@coveo/headless-react/ssr-commerce';
import Image from 'next/image';
import {useRouter} from 'next/navigation';

export default function ProductList() {
Expand All @@ -27,6 +28,12 @@ export default function ProductList() {
onClick={() => onProductClick(product)}
>
{product.ec_name}
<Image
src={product.ec_images[0]}
alt={product.ec_name!}
width={50}
height={50}
/>
</button>
<button onClick={() => addToCart(cartController!, product)}>
Add to cart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ type CommerceEngineConfig = CommerceEngineDefinitionOptions<
export default {
configuration: {
...getSampleCommerceEngineConfiguration(),
context: {
language: 'en',
country: 'US',
currency: 'USD',
view: {
url: 'https://sports.barca.group/browse/promotions/ui-kit-testing',
},
},
},
controllers: {
summary: defineSummary(),
Expand Down
3 changes: 3 additions & 0 deletions packages/samples/headless-ssr-commerce/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const nextConfig = {
},
];
},
images: {
domains: ['images.barca.group'],
},
};

export default nextConfig;

0 comments on commit 1d8f07f

Please sign in to comment.