Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(headless-ssr-commerce-samples): add multiple listing pages #4635

Open
wants to merge 3 commits into
base: KIT-3681-4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
},
},
Comment on lines -34 to -41
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove this from the require definition since it should always be added during fetchStaticState ?

},
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'],
},
Comment on lines +12 to +14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just necessary to use nextjs <Image/>

};

export default nextConfig;
Loading