Skip to content

Commit

Permalink
working sample
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprudhomme committed Nov 7, 2024
1 parent bea208b commit a0cb3f7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import StandaloneSearchBox from '@/components/standalone-search-box';
import Summary from '@/components/summary';
import {listingEngineDefinition} from '@/lib/commerce-engine';
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {defaultContext} from '@/utils/context';
import {headers} from 'next/headers';
import {notFound} from 'next/navigation';

Expand Down Expand Up @@ -51,9 +52,9 @@ export default async function Listing({params}: {params: {category: string}}) {
controllers: {
cart: {initialState: {items}},
context: {
language: 'en',
country: 'US',
currency: 'USD',
language: defaultContext.language,
country: defaultContext.country,
currency: defaultContext.currency,
view: {
url: `https://sports.barca.group/browse/promotions/${matchedCategory}`,
},
Expand All @@ -66,7 +67,7 @@ export default async function Listing({params}: {params: {category: string}}) {
staticState={staticState}
navigatorContext={navigatorContext.marshal}
>
<ContextDropdown />
<ContextDropdown useCase="listing" />
<div style={{display: 'flex', flexDirection: 'row'}}>
<div style={{flex: 1}}>
<FacetGenerator />
Expand Down
7 changes: 4 additions & 3 deletions packages/samples/headless-ssr-commerce/app/cart/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ContextDropdown from '@/components/context-dropdown';
import SearchProvider from '@/components/providers/search-provider';
import {searchEngineDefinition} from '@/lib/commerce-engine';
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {defaultContext} from '@/utils/context';
import {headers} from 'next/headers';

export default async function Search() {
Expand All @@ -19,9 +20,9 @@ export default async function Search() {
controllers: {
cart: {initialState: {items}},
context: {
language: 'en',
country: 'US',
currency: 'USD',
language: defaultContext.language,
country: defaultContext.country,
currency: defaultContext.currency,
view: {
url: 'https://sports.barca.group/cart',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ContextDropdown from '@/components/context-dropdown';
import ProductPage from '@/components/pages/product-page';
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';

Expand All @@ -23,9 +24,9 @@ export default async function ProductDescriptionPage({
controllers: {
cart: {initialState: {items}},
context: {
language: 'en',
country: 'US',
currency: 'USD',
language: defaultContext.language,
country: defaultContext.country,
currency: defaultContext.currency,
view: {
url: `https://sports.barca.group/products/${params.productId}`,
},
Expand Down
11 changes: 5 additions & 6 deletions packages/samples/headless-ssr-commerce/app/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import Summary from '@/components/summary';
import Triggers from '@/components/triggers/triggers';
import {searchEngineDefinition} from '@/lib/commerce-engine';
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {getContext} from '@/utils/context';
import {defaultContext} from '@/utils/context';
import {headers} from 'next/headers';

export default async function Search() {
// Sets the navigator context provider to use the newly created `navigatorContext` before fetching the app static state
const navigatorContext = new NextJsNavigatorContext(headers());
searchEngineDefinition.setNavigatorContextProvider(() => navigatorContext);

const context = await getContext();
// Fetches the cart items from an external service
const items = await externalCartAPI.getCart();

Expand All @@ -28,9 +27,9 @@ export default async function Search() {
controllers: {
cart: {initialState: {items}},
context: {
language: context.language,
country: context.country,
currency: context.currency,
language: defaultContext.language,
country: defaultContext.country,
currency: defaultContext.currency,
view: {
url: 'https://sports.barca.group/browse/search',
},
Expand All @@ -43,7 +42,7 @@ export default async function Search() {
staticState={staticState}
navigatorContext={navigatorContext.marshal}
>
<ContextDropdown />
<ContextDropdown useCase="search" />

<div style={{display: 'flex', flexDirection: 'row'}}>
<div style={{flex: 1}}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
'use client';

import {useContext} from '@/lib/commerce-engine';
import {ContextOptions} from '@coveo/headless-react/ssr-commerce';
import {useContext, useEngine} from '@/lib/commerce-engine';
import {
CommerceEngine,
ContextOptions,
loadProductListingActions,
loadSearchActions,
} from '@coveo/headless-react/ssr-commerce';

// A hardcoded list of storefront associations for switching app context by language, country, and currency.
// Found in the admin console under "Storefront Associations," this list is static for demonstration purposes.
// In a real application, these values would likely come from sources like environment variables or an API.
const storefrontAssociations = [
'en-CA-CAD',
'fr-CA-CAD',
'en-GB-GBP',
'en-US-USD',
];

export default function ContextDropdown() {
export default function ContextDropdown({
useCase,
}: {
useCase?: 'listing' | 'search';
}) {
const {state, controller} = useContext();
// const engine = useEngine();
// const {executeSearch} = loadSearchActions(engine as CommerceEngine);
// const {fetchProductListing} = loadProductListingActions(
// engine as CommerceEngine
// );
// const {fetchProductListing} = loadProductListingActions(
// engine as CommerceEngine
// );
const engine = useEngine();

return (
<div>
<p>AAA</p>
Context slider {state.country} {state.currency} {state.language}
<p></p>
Context dropdown :
<select
value={`${state.language}-${state.country}-${state.currency}`}
onChange={(e) => {
const [language, country, currency] = e.target.value.split('-');
controller?.setLanguage(language);
controller?.setCountry(country);
controller?.setCurrency(currency as ContextOptions['currency']);
// engine?.dispatch(executeSearch());
// engine?.dispatch(fetchProductListing());

useCase === 'search'
? engine?.dispatch(
loadSearchActions(engine as CommerceEngine).executeSearch()
)
: useCase === 'listing' &&
engine?.dispatch(
loadProductListingActions(
engine as CommerceEngine
).fetchProductListing()
);
}}
>
{storefrontAssociations.map((association) => (
Expand All @@ -42,7 +56,7 @@ export default function ContextDropdown() {
</option>
))}
</select>
;
<p></p>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import {
ListingHydratedState,
ListingStaticState,
} from '@/lib/commerce-engine';
import {
loadSearchActions,
NavigatorContext,
} from '@coveo/headless-react/ssr-commerce';
import {NavigatorContext} from '@coveo/headless-react/ssr-commerce';
import {PropsWithChildren, useEffect, useState} from 'react';

interface ListingPageProps {
Expand Down Expand Up @@ -48,9 +45,6 @@ export default function ListingProvider({
})
.then(({engine, controllers}) => {
setHydratedState({engine, controllers});
engine.addReducers({});
const {executeSearch} = loadSearchActions(engine);
engine.dispatch(executeSearch());
// Refreshing recommendations in the browser after hydrating the state in the client-side
// Recommendation refresh in the server is not supported yet.
// controllers.popularBoughtRecs.refresh(); // FIXME: does not work
Expand Down
22 changes: 10 additions & 12 deletions packages/samples/headless-ssr-commerce/utils/context.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use server';
import {ContextState} from '@coveo/headless-react/ssr-commerce';

import {ContextOptions} from '@coveo/headless/ssr-commerce';
import {cookies} from 'next/headers';

export async function getContext() {
return {
language: cookies().get('language')?.value || 'en',
country: cookies().get('country')?.value || 'US',
currency:
(cookies().get('currency')?.value as ContextOptions['currency']) || 'USD',
};
}
export const defaultContext: {
language: string;
country: string;
currency: ContextState['currency'];
} = {
language: 'en',
country: 'US',
currency: 'USD',
};

0 comments on commit a0cb3f7

Please sign in to comment.