diff --git a/packages/samples/headless-ssr-commerce/app/_components/summary.tsx b/packages/samples/headless-ssr-commerce/app/_components/summary.tsx
index f267bca3300..887f6d26182 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/summary.tsx
+++ b/packages/samples/headless-ssr-commerce/app/_components/summary.tsx
@@ -1,27 +1,9 @@
-import {
- Summary as HeadlessSummary,
- ProductListingSummaryState,
- SearchSummaryState,
- RecommendationsSummaryState,
-} from '@coveo/headless/ssr-commerce';
-import {useEffect, useState} from 'react';
+'use client';
-interface ISummaryProps {
- controller?: HeadlessSummary;
- staticState:
- | ProductListingSummaryState
- | SearchSummaryState
- | RecommendationsSummaryState;
-}
-
-export default function Summary(props: ISummaryProps) {
- const {controller, staticState} = props;
+import {useSummary} from '../_lib/commerce-engine';
- const [state, setState] = useState(staticState);
-
- useEffect(() => {
- controller?.subscribe(() => setState(controller.state));
- }, [controller]);
+export default function Summary() {
+ const {state} = useSummary();
const renderBaseSummary = () => {
const {firstProduct, lastProduct, totalNumberOfProducts} = state;
@@ -38,11 +20,12 @@ export default function Summary(props: ISummaryProps) {
return null;
}
- return (
-
- for {state.query}
-
- );
+ // TODO: add query to summary state
+ // return (
+ //
+ // for {state.query}
+ //
+ // );
};
const renderSummary = () => {
diff --git a/packages/samples/headless-ssr-commerce/app/_components/triggers/notify-trigger.tsx b/packages/samples/headless-ssr-commerce/app/_components/triggers/notify-trigger.tsx
index 35cadb8a3d8..035e88d49ee 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/triggers/notify-trigger.tsx
+++ b/packages/samples/headless-ssr-commerce/app/_components/triggers/notify-trigger.tsx
@@ -1,25 +1,11 @@
-import {
- NotifyTrigger as NotifyTriggerController,
- NotifyTriggerState,
-} from '@coveo/headless/commerce';
-import {useCallback, useEffect, useState} from 'react';
+'use client';
-interface NotifyTriggerProps {
- controller?: NotifyTriggerController;
- staticState: NotifyTriggerState;
-}
+import {useNotifyTrigger} from '@/app/_lib/commerce-engine';
+import {useCallback, useEffect} from 'react';
// The notify trigger query example in the searchuisamples org is 'notify me'.
-export default function NotifyTrigger({
- controller,
- staticState,
-}: NotifyTriggerProps) {
- const [state, setState] = useState(staticState);
-
- useEffect(
- () => controller?.subscribe(() => setState({...controller.state})),
- [controller]
- );
+export default function NotifyTrigger() {
+ const {state} = useNotifyTrigger();
const notify = useCallback(() => {
state.notifications.forEach((notification) => {
diff --git a/packages/samples/headless-ssr-commerce/app/_components/triggers/query-trigger.tsx b/packages/samples/headless-ssr-commerce/app/_components/triggers/query-trigger.tsx
index 01fe1a061af..9b052460347 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/triggers/query-trigger.tsx
+++ b/packages/samples/headless-ssr-commerce/app/_components/triggers/query-trigger.tsx
@@ -1,25 +1,10 @@
-import {
- QueryTrigger as QueryTriggerController,
- QueryTriggerState,
-} from '@coveo/headless/commerce';
-import {useEffect, useState} from 'react';
+'use client';
-interface QueryTriggerProps {
- controller?: QueryTriggerController;
- staticState: QueryTriggerState;
-}
+import {useQueryTrigger} from '@/app/_lib/commerce-engine';
// The query trigger query example in the searchuisamples org is 'query me'.
-export default function QueryTrigger({
- controller,
- staticState,
-}: QueryTriggerProps) {
- const [state, setState] = useState(staticState);
-
- useEffect(
- () => controller?.subscribe(() => setState({...controller.state})),
- [controller]
- );
+export default function QueryTrigger() {
+ const {state} = useQueryTrigger();
if (state.wasQueryModified) {
return (
diff --git a/packages/samples/headless-ssr-commerce/app/_components/triggers/redirection-trigger.tsx b/packages/samples/headless-ssr-commerce/app/_components/triggers/redirection-trigger.tsx
index 38455192d35..f97f7992407 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/triggers/redirection-trigger.tsx
+++ b/packages/samples/headless-ssr-commerce/app/_components/triggers/redirection-trigger.tsx
@@ -1,25 +1,11 @@
-import {
- RedirectionTrigger as RedirectionTriggerController,
- RedirectionTriggerState,
-} from '@coveo/headless/commerce';
-import {useCallback, useEffect, useState} from 'react';
+'use client';
-interface RedirectionTriggerProps {
- controller?: RedirectionTriggerController;
- staticState: RedirectionTriggerState;
-}
+import {useRedirectionTrigger} from '@/app/_lib/commerce-engine';
+import {useCallback, useEffect} from 'react';
// The redirection trigger query example in the searchuisamples org is 'redirect me'.
-export default function RedirectionTrigger({
- controller,
- staticState,
-}: RedirectionTriggerProps) {
- const [state, setState] = useState(staticState);
-
- useEffect(
- () => controller?.subscribe(() => setState({...controller.state})),
- [controller]
- );
+export default function RedirectionTrigger() {
+ const {state} = useRedirectionTrigger();
const redirect = useCallback(() => {
if (state.redirectTo) {
diff --git a/packages/samples/headless-ssr-commerce/app/_components/triggers/triggers.tsx b/packages/samples/headless-ssr-commerce/app/_components/triggers/triggers.tsx
index 8086cede817..6ac0b54befa 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/triggers/triggers.tsx
+++ b/packages/samples/headless-ssr-commerce/app/_components/triggers/triggers.tsx
@@ -1,45 +1,13 @@
-import {
- QueryTrigger as QueryTriggerController,
- QueryTriggerState,
- RedirectionTrigger as RedirectionTriggerController,
- RedirectionTriggerState,
- NotifyTrigger as NotifyTriggerController,
- NotifyTriggerState,
-} from '@coveo/headless/commerce';
import NotifyTrigger from './notify-trigger';
import QueryTrigger from './query-trigger';
import RedirectionTrigger from './redirection-trigger';
-interface TriggersProps {
- redirectionController?: RedirectionTriggerController;
- redirectionStaticState: RedirectionTriggerState;
- queryDontroller?: QueryTriggerController;
- queryStaticState: QueryTriggerState;
- notifyController?: NotifyTriggerController;
- notifyStaticState: NotifyTriggerState;
-}
-export default function Triggers({
- redirectionStaticState,
- redirectionController,
- queryStaticState,
- queryDontroller,
- notifyStaticState,
- notifyController,
-}: TriggersProps) {
+export default function Triggers() {
return (
<>
-
-
-
+
+
+
>
);
}
diff --git a/packages/samples/headless-ssr-commerce/app/_lib/commerce-engine-config.ts b/packages/samples/headless-ssr-commerce/app/_lib/commerce-engine-config.ts
index ce281375e97..4a02e7b0287 100644
--- a/packages/samples/headless-ssr-commerce/app/_lib/commerce-engine-config.ts
+++ b/packages/samples/headless-ssr-commerce/app/_lib/commerce-engine-config.ts
@@ -13,6 +13,7 @@ import {
defineRedirectionTrigger,
defineStandaloneSearchBox,
defineInstantProducts,
+ defineBreadcrumbManager,
defineSummary,
definePagination,
defineFacetGenerator,
@@ -21,8 +22,7 @@ import {
getSampleCommerceEngineConfiguration,
defineDidYouMean,
defineRecommendations, //defineParameterManager,
- defineBreadcrumbManager,
-} from '@coveo/headless/ssr-commerce';
+} from '@coveo/headless-react/ssr-commerce';
type CommerceEngineConfig = CommerceEngineDefinitionOptions<
ControllerDefinitionsMap
diff --git a/packages/samples/headless-ssr-commerce/app/_lib/commerce-engine.ts b/packages/samples/headless-ssr-commerce/app/_lib/commerce-engine.ts
index 0d08774ac59..91ea7d56473 100644
--- a/packages/samples/headless-ssr-commerce/app/_lib/commerce-engine.ts
+++ b/packages/samples/headless-ssr-commerce/app/_lib/commerce-engine.ts
@@ -2,17 +2,40 @@ import {
defineCommerceEngine,
InferStaticState,
InferHydratedState,
-} from '@coveo/headless/ssr-commerce';
+} from '@coveo/headless-react/ssr-commerce';
import engineConfig from './commerce-engine-config';
-const engineDefinition = defineCommerceEngine(engineConfig);
+export const engineDefinition = defineCommerceEngine(engineConfig);
export const {
listingEngineDefinition,
searchEngineDefinition,
standaloneEngineDefinition,
+ useEngine,
} = engineDefinition;
+export const {
+ useCart,
+ useContext,
+ useProductList,
+ useDidYouMean,
+ useInstantProducts,
+ useNotifyTrigger,
+ usePagination,
+ usePopularBoughtRecs,
+ usePopularViewedRecs,
+ useProductView,
+ useQueryTrigger,
+ useRecentQueriesList,
+ useRedirectionTrigger,
+ useSearchBox,
+ useSort,
+ useStandaloneSearchBox,
+ useSummary,
+ useFacetGenerator,
+ useBreadcrumbManager,
+} = engineDefinition.controllers;
+
export type ListingStaticState = InferStaticState<
typeof listingEngineDefinition
>;
diff --git a/packages/samples/headless-ssr-commerce/app/_lib/navigatorContextProvider.ts b/packages/samples/headless-ssr-commerce/app/_lib/navigatorContextProvider.ts
index cb5ffa03b7e..47921af03fc 100644
--- a/packages/samples/headless-ssr-commerce/app/_lib/navigatorContextProvider.ts
+++ b/packages/samples/headless-ssr-commerce/app/_lib/navigatorContextProvider.ts
@@ -1,4 +1,4 @@
-import {NavigatorContext} from '@coveo/headless/ssr-commerce';
+import {NavigatorContext} from '@coveo/headless-react/ssr-commerce';
import type {ReadonlyHeaders} from 'next/dist/server/web/spec-extension/adapters/headers';
/**
diff --git a/packages/samples/headless-ssr-commerce/app/layout.tsx b/packages/samples/headless-ssr-commerce/app/layout.tsx
index 63a608acbe9..e1db209ed3c 100644
--- a/packages/samples/headless-ssr-commerce/app/layout.tsx
+++ b/packages/samples/headless-ssr-commerce/app/layout.tsx
@@ -2,8 +2,7 @@ import Link from 'next/link';
export const metadata = {
title: 'Headless SSR examples',
- description:
- 'Examples of using framework agnostic @coveo/headless/ssr-commerce',
+ description: 'Examples of using framework @coveo/headless-react/ssr-commerce',
};
export default function RootLayout({children}: {children: React.ReactNode}) {
diff --git a/packages/samples/headless-ssr-commerce/app/listing/page.tsx b/packages/samples/headless-ssr-commerce/app/listing/page.tsx
index 1e6bfff2c94..e03abd76d59 100644
--- a/packages/samples/headless-ssr-commerce/app/listing/page.tsx
+++ b/packages/samples/headless-ssr-commerce/app/listing/page.tsx
@@ -1,5 +1,14 @@
import {headers} from 'next/headers';
+import BreadcrumbManager from '../_components/breadcrumb-manager';
+import Cart from '../_components/cart';
+import FacetGenerator from '../_components/facets/facet-generator';
import ListingPage from '../_components/pages/listing-page';
+import Pagination from '../_components/pagination';
+import ProductList from '../_components/product-list';
+import {Recommendations} from '../_components/recommendation-list';
+import Sort from '../_components/sort';
+import StandaloneSearchBox from '../_components/standalone-search-box';
+import Summary from '../_components/summary';
import {listingEngineDefinition} from '../_lib/commerce-engine';
import {NextJsNavigatorContext} from '../_lib/navigatorContextProvider';
@@ -20,7 +29,34 @@ export default async function Listing() {
+ >
+
+
+
+
+
+
+
+
+ {/* The ShowMore and Pagination components showcase two frequent ways to implement pagination. */}
+
+ {/* */}
+
+
+
+
My Cart
+
+
+
+
+
+
+
+
);
}
diff --git a/packages/samples/headless-ssr-commerce/app/recommendation/page.tsx b/packages/samples/headless-ssr-commerce/app/recommendation/page.tsx
deleted file mode 100644
index 3a27fdbfe0f..00000000000
--- a/packages/samples/headless-ssr-commerce/app/recommendation/page.tsx
+++ /dev/null
@@ -1,29 +0,0 @@
-import {headers} from 'next/headers';
-import Recommendation from '../_components/pages/recommendation';
-import {standaloneEngineDefinition} from '../_lib/commerce-engine';
-import {NextJsNavigatorContext} from '../_lib/navigatorContextProvider';
-
-/**
- * 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 RecommendationPage() {
- // Sets the navigator context provider to use the newly created `navigatorContext` before fetching the app static state
- const navigatorContext = new NextJsNavigatorContext(headers());
- standaloneEngineDefinition.setNavigatorContextProvider(
- () => navigatorContext
- );
-
- // Fetches the static state of the app with initial state (when applicable)
- const staticState = await standaloneEngineDefinition.fetchStaticState();
-
- return (
-
- );
-}
-
-export const dynamic = 'force-dynamic';
diff --git a/packages/samples/headless-ssr-commerce/app/search/page.tsx b/packages/samples/headless-ssr-commerce/app/search/page.tsx
index 6c8a1a5c0a2..554898e951b 100644
--- a/packages/samples/headless-ssr-commerce/app/search/page.tsx
+++ b/packages/samples/headless-ssr-commerce/app/search/page.tsx
@@ -1,5 +1,13 @@
import {headers} from 'next/headers';
+import BreadcrumbManager from '../_components/breadcrumb-manager';
+import FacetGenerator from '../_components/facets/facet-generator';
import SearchPage from '../_components/pages/search-page';
+import ProductList from '../_components/product-list';
+import {Recommendations} from '../_components/recommendation-list';
+import SearchBox from '../_components/search-box';
+import ShowMore from '../_components/show-more';
+import Summary from '../_components/summary';
+import Triggers from '../_components/triggers/triggers';
import {searchEngineDefinition} from '../_lib/commerce-engine';
import {NextJsNavigatorContext} from '../_lib/navigatorContextProvider';
@@ -14,7 +22,30 @@ export default async function Search() {
+ >
+
+
+
+
+
+
+
+
+ {/* The ShowMore and Pagination components showcase two frequent ways to implement pagination. */}
+ {/* */}
+
+
+
+
+ {/* popularBoughtRecs */}
+ {/* TODO: need to find a better way to target a recommendation slot id */}
+
+
+
+
);
}
diff --git a/packages/samples/headless-ssr-commerce/package.json b/packages/samples/headless-ssr-commerce/package.json
index 6fe3afc5e2f..2d2da69090e 100644
--- a/packages/samples/headless-ssr-commerce/package.json
+++ b/packages/samples/headless-ssr-commerce/package.json
@@ -1,6 +1,6 @@
{
"name": "@coveo/headless-ssr-commerce-samples",
- "description": "Examples of framework agnostic @coveo/headless/ssr-commerce utils using Next.js app router",
+ "description": "Examples of framework @coveo/headless-react/ssr-commerce utils using Next.js app router",
"version": "0.0.0",
"private": true,
"scripts": {
@@ -11,7 +11,7 @@
"build:next": "next build"
},
"dependencies": {
- "@coveo/headless": "3.5.0",
+ "@coveo/headless-react": "2.0.8",
"next": "14.2.5",
"react": "^18.2.0",
"react-dom": "^18.2.0"
From 868593c651fe9f500c0956b0974cbbeb81edce4f Mon Sep 17 00:00:00 2001
From: ylakhdar
Date: Thu, 31 Oct 2024 11:46:12 -0400
Subject: [PATCH 05/17] add todo
https://coveord.atlassian.net/browse/KIT-3699
---
.../samples/headless-ssr-commerce/app/_components/sort.tsx | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/packages/samples/headless-ssr-commerce/app/_components/sort.tsx b/packages/samples/headless-ssr-commerce/app/_components/sort.tsx
index 796b2656933..e6db096652f 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/sort.tsx
+++ b/packages/samples/headless-ssr-commerce/app/_components/sort.tsx
@@ -10,6 +10,7 @@ export default function Sort() {
return null;
}
+ // TODO: uncomment when KIT-3700: is fixed
// const getSortLabel = (criterion: SortCriterion) => {
// switch (criterion.by) {
// case SortBy.Relevancy:
@@ -35,7 +36,7 @@ export default function Sort() {
value={JSON.stringify(sort)}
onSelect={() => controller?.sortBy(sort)}
>
- {/* TODO: there is a type mismatch with the sort criterion FIXME:!!*/}
+ {/* TODO:KIT-3700: there is a type mismatch with the sort criterion FIXME:!!*/}
{/* {getSortLabel(sort)} */}
))}
From 3a376d7385123a9b053ee6e42d44228425ee18d1 Mon Sep 17 00:00:00 2001
From: ylakhdar
Date: Thu, 31 Oct 2024 11:50:50 -0400
Subject: [PATCH 06/17] add TODOs:
https://coveord.atlassian.net/browse/KIT-3699
---
.../app/_components/pages/search-page.tsx | 4 ++--
.../app/_components/recommendation-list.tsx | 3 +--
packages/samples/headless-ssr-commerce/app/search/page.tsx | 2 +-
3 files changed, 4 insertions(+), 5 deletions(-)
diff --git a/packages/samples/headless-ssr-commerce/app/_components/pages/search-page.tsx b/packages/samples/headless-ssr-commerce/app/_components/pages/search-page.tsx
index 1b07385a4d1..e67a82fa0a2 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/pages/search-page.tsx
+++ b/packages/samples/headless-ssr-commerce/app/_components/pages/search-page.tsx
@@ -45,7 +45,7 @@ export default function SearchPage({
engine={hydratedState.engine}
controllers={hydratedState.controllers}
>
- {/* // TODO: FIXME: Type 'React.ReactNode' is not assignable to type 'import(".../node_modules/@types/react/index").ReactNode'.
+ {/* // TODO: KIT-3701: Type 'React.ReactNode' is not assignable to type 'import(".../node_modules/@types/react/index").ReactNode'.
Type 'bigint' is not assignable to type 'ReactNode'.*/}
<>{children}>
@@ -55,7 +55,7 @@ export default function SearchPage({
- {/* // TODO: FIXME: Type 'React.ReactNode' is not assignable to type 'import(".../node_modules/@types/react/index").ReactNode'.
+ {/* // TODO: KIT-3701: Type 'React.ReactNode' is not assignable to type 'import(".../node_modules/@types/react/index").ReactNode'.
Type 'bigint' is not assignable to type 'ReactNode'.*/}
<>{children}>
diff --git a/packages/samples/headless-ssr-commerce/app/_components/recommendation-list.tsx b/packages/samples/headless-ssr-commerce/app/_components/recommendation-list.tsx
index 3c3166ae173..5e71137880b 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/recommendation-list.tsx
+++ b/packages/samples/headless-ssr-commerce/app/_components/recommendation-list.tsx
@@ -6,9 +6,8 @@ import {FunctionComponent} from 'react';
import {usePopularBoughtRecs} from '../_lib/commerce-engine';
export const Recommendations: FunctionComponent = () => {
- // TODO: find a way to make the recommendation generic
+ // TODO: KIT-3503: refresh recs server side
const {state, controller} = usePopularBoughtRecs();
- // TODO: recommendation are not refreshed server-side FIXME:
const router = useRouter();
diff --git a/packages/samples/headless-ssr-commerce/app/search/page.tsx b/packages/samples/headless-ssr-commerce/app/search/page.tsx
index 554898e951b..529e980932d 100644
--- a/packages/samples/headless-ssr-commerce/app/search/page.tsx
+++ b/packages/samples/headless-ssr-commerce/app/search/page.tsx
@@ -41,7 +41,7 @@ export default async function Search() {
{/* popularBoughtRecs */}
- {/* TODO: need to find a better way to target a recommendation slot id */}
+ {/* TODO: KIT-3503: need to revisit the way recommendations are added*/}
{children}
diff --git a/packages/samples/headless-ssr-commerce/app/listing/page.tsx b/packages/samples/headless-ssr-commerce/app/listing/page.tsx
index e03abd76d59..561c4882511 100644
--- a/packages/samples/headless-ssr-commerce/app/listing/page.tsx
+++ b/packages/samples/headless-ssr-commerce/app/listing/page.tsx
@@ -1,16 +1,16 @@
+import BreadcrumbManager from '@/components/breadcrumb-manager';
+import Cart from '@/components/cart';
+import FacetGenerator from '@/components/facets/facet-generator';
+import Pagination from '@/components/pagination';
+import ProductList from '@/components/product-list';
+import ListingProvider from '@/components/providers/listing-provider';
+import {Recommendations} from '@/components/recommendation-list';
+import Sort from '@/components/sort';
+import StandaloneSearchBox from '@/components/standalone-search-box';
+import Summary from '@/components/summary';
+import {listingEngineDefinition} from '@/lib/commerce-engine';
+import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {headers} from 'next/headers';
-import BreadcrumbManager from '../_components/breadcrumb-manager';
-import Cart from '../_components/cart';
-import FacetGenerator from '../_components/facets/facet-generator';
-import ListingPage from '../_components/pages/listing-page';
-import Pagination from '../_components/pagination';
-import ProductList from '../_components/product-list';
-import {Recommendations} from '../_components/recommendation-list';
-import Sort from '../_components/sort';
-import StandaloneSearchBox from '../_components/standalone-search-box';
-import Summary from '../_components/summary';
-import {listingEngineDefinition} from '../_lib/commerce-engine';
-import {NextJsNavigatorContext} from '../_lib/navigatorContextProvider';
/**
* This file defines a List component that uses the Coveo Headless SSR commerce library to manage its state.
@@ -26,15 +26,18 @@ export default async function Listing() {
const staticState = await listingEngineDefinition.fetchStaticState();
return (
-
-
+
);
}
diff --git a/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx b/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx
index b797295046b..75a3c183033 100644
--- a/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx
+++ b/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx
@@ -1,6 +1,6 @@
-import ProductPage from '@/app/_components/pages/product-page';
-import {searchEngineDefinition} from '@/app/_lib/commerce-engine';
-import {NextJsNavigatorContext} from '@/app/_lib/navigatorContextProvider';
+import ProductPage from '@/components/pages/product-page';
+import {searchEngineDefinition} from '@/lib/commerce-engine';
+import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {headers} from 'next/headers';
import {Suspense} from 'react';
diff --git a/packages/samples/headless-ssr-commerce/app/search/page.tsx b/packages/samples/headless-ssr-commerce/app/search/page.tsx
index 529e980932d..a3f7fa01698 100644
--- a/packages/samples/headless-ssr-commerce/app/search/page.tsx
+++ b/packages/samples/headless-ssr-commerce/app/search/page.tsx
@@ -1,15 +1,15 @@
+import BreadcrumbManager from '@/components/breadcrumb-manager';
+import FacetGenerator from '@/components/facets/facet-generator';
+import ProductList from '@/components/product-list';
+import SearchProvider from '@/components/providers/search-provider';
+import {Recommendations} from '@/components/recommendation-list';
+import SearchBox from '@/components/search-box';
+import ShowMore from '@/components/show-more';
+import Summary from '@/components/summary';
+import Triggers from '@/components/triggers/triggers';
+import {searchEngineDefinition} from '@/lib/commerce-engine';
+import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {headers} from 'next/headers';
-import BreadcrumbManager from '../_components/breadcrumb-manager';
-import FacetGenerator from '../_components/facets/facet-generator';
-import SearchPage from '../_components/pages/search-page';
-import ProductList from '../_components/product-list';
-import {Recommendations} from '../_components/recommendation-list';
-import SearchBox from '../_components/search-box';
-import ShowMore from '../_components/show-more';
-import Summary from '../_components/summary';
-import Triggers from '../_components/triggers/triggers';
-import {searchEngineDefinition} from '../_lib/commerce-engine';
-import {NextJsNavigatorContext} from '../_lib/navigatorContextProvider';
export default async function Search() {
// Sets the navigator context provider to use the newly created `navigatorContext` before fetching the app static state
@@ -19,16 +19,18 @@ export default async function Search() {
// Fetches the static state of the app with initial state (when applicable)
const staticState = await searchEngineDefinition.fetchStaticState();
return (
-
+
+
+
-
{/* The ShowMore and Pagination components showcase two frequent ways to implement pagination. */}
@@ -39,13 +41,13 @@ export default async function Search() {
-
+
{/* popularBoughtRecs */}
{/* TODO: KIT-3503: need to revisit the way recommendations are added*/}
-
+
);
}
diff --git a/packages/samples/headless-ssr-commerce/app/_components/breadcrumb-manager.tsx b/packages/samples/headless-ssr-commerce/components/breadcrumb-manager.tsx
similarity index 96%
rename from packages/samples/headless-ssr-commerce/app/_components/breadcrumb-manager.tsx
rename to packages/samples/headless-ssr-commerce/components/breadcrumb-manager.tsx
index 01a5f8713c9..76f90f2ea3b 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/breadcrumb-manager.tsx
+++ b/packages/samples/headless-ssr-commerce/components/breadcrumb-manager.tsx
@@ -6,7 +6,7 @@ import {
CategoryFacetValue,
RegularFacetValue,
} from '@coveo/headless-react/ssr-commerce';
-import {useBreadcrumbManager} from '../_lib/commerce-engine';
+import {useBreadcrumbManager} from '../lib/commerce-engine';
export default function BreadcrumbManager() {
const {state, controller} = useBreadcrumbManager();
diff --git a/packages/samples/headless-ssr-commerce/app/_components/cart.tsx b/packages/samples/headless-ssr-commerce/components/cart.tsx
similarity index 94%
rename from packages/samples/headless-ssr-commerce/app/_components/cart.tsx
rename to packages/samples/headless-ssr-commerce/components/cart.tsx
index 55274cb7b38..964fe776d68 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/cart.tsx
+++ b/packages/samples/headless-ssr-commerce/components/cart.tsx
@@ -1,8 +1,8 @@
'use client';
import {CartItem} from '@coveo/headless-react/ssr-commerce';
-import {useCart, useContext} from '../_lib/commerce-engine';
-import {formatCurrency} from '../_utils/format-currency';
+import {useCart, useContext} from '../lib/commerce-engine';
+import {formatCurrency} from '../utils/format-currency';
export default function Cart() {
const {state, controller} = useCart();
diff --git a/packages/samples/headless-ssr-commerce/app/_components/did-you-mean.tsx b/packages/samples/headless-ssr-commerce/components/did-you-mean.tsx
similarity index 100%
rename from packages/samples/headless-ssr-commerce/app/_components/did-you-mean.tsx
rename to packages/samples/headless-ssr-commerce/components/did-you-mean.tsx
diff --git a/packages/samples/headless-ssr-commerce/app/_components/facets/category-facet.tsx b/packages/samples/headless-ssr-commerce/components/facets/category-facet.tsx
similarity index 100%
rename from packages/samples/headless-ssr-commerce/app/_components/facets/category-facet.tsx
rename to packages/samples/headless-ssr-commerce/components/facets/category-facet.tsx
diff --git a/packages/samples/headless-ssr-commerce/app/_components/facets/date-facet.tsx b/packages/samples/headless-ssr-commerce/components/facets/date-facet.tsx
similarity index 100%
rename from packages/samples/headless-ssr-commerce/app/_components/facets/date-facet.tsx
rename to packages/samples/headless-ssr-commerce/components/facets/date-facet.tsx
diff --git a/packages/samples/headless-ssr-commerce/app/_components/facets/facet-generator.tsx b/packages/samples/headless-ssr-commerce/components/facets/facet-generator.tsx
similarity index 96%
rename from packages/samples/headless-ssr-commerce/app/_components/facets/facet-generator.tsx
rename to packages/samples/headless-ssr-commerce/components/facets/facet-generator.tsx
index 0d6dcbb26bf..17d545c1a46 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/facets/facet-generator.tsx
+++ b/packages/samples/headless-ssr-commerce/components/facets/facet-generator.tsx
@@ -1,6 +1,6 @@
'use client';
-import {useFacetGenerator} from '@/app/_lib/commerce-engine';
+import {useFacetGenerator} from '@/lib/commerce-engine';
import CategoryFacet from './category-facet';
import DateFacet from './date-facet';
import NumericFacet from './numeric-facet';
diff --git a/packages/samples/headless-ssr-commerce/app/_components/facets/numeric-facet.tsx b/packages/samples/headless-ssr-commerce/components/facets/numeric-facet.tsx
similarity index 100%
rename from packages/samples/headless-ssr-commerce/app/_components/facets/numeric-facet.tsx
rename to packages/samples/headless-ssr-commerce/components/facets/numeric-facet.tsx
diff --git a/packages/samples/headless-ssr-commerce/app/_components/facets/regular-facet.tsx b/packages/samples/headless-ssr-commerce/components/facets/regular-facet.tsx
similarity index 100%
rename from packages/samples/headless-ssr-commerce/app/_components/facets/regular-facet.tsx
rename to packages/samples/headless-ssr-commerce/components/facets/regular-facet.tsx
diff --git a/packages/samples/headless-ssr-commerce/app/_components/instant-product.tsx b/packages/samples/headless-ssr-commerce/components/instant-product.tsx
similarity index 92%
rename from packages/samples/headless-ssr-commerce/app/_components/instant-product.tsx
rename to packages/samples/headless-ssr-commerce/components/instant-product.tsx
index 87f184b7e7c..6ee21eaf3ee 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/instant-product.tsx
+++ b/packages/samples/headless-ssr-commerce/components/instant-product.tsx
@@ -1,6 +1,6 @@
import {Product} from '@coveo/headless-react/ssr-commerce';
import {useRouter} from 'next/navigation';
-import {useInstantProducts} from '../_lib/commerce-engine';
+import {useInstantProducts} from '../lib/commerce-engine';
export default function InstantProducts() {
const router = useRouter();
diff --git a/packages/samples/headless-ssr-commerce/app/_components/pages/product-page.tsx b/packages/samples/headless-ssr-commerce/components/pages/product-page.tsx
similarity index 98%
rename from packages/samples/headless-ssr-commerce/app/_components/pages/product-page.tsx
rename to packages/samples/headless-ssr-commerce/components/pages/product-page.tsx
index 0cb4ea535c0..549d7d49225 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/pages/product-page.tsx
+++ b/packages/samples/headless-ssr-commerce/components/pages/product-page.tsx
@@ -4,7 +4,7 @@ import {
standaloneEngineDefinition,
StandaloneHydratedState,
StandaloneStaticState,
-} from '@/app/_lib/commerce-engine';
+} from '@/lib/commerce-engine';
import {NavigatorContext} from '@coveo/headless-react/ssr-commerce';
import {useSearchParams} from 'next/navigation';
import {useEffect, useState} from 'react';
diff --git a/packages/samples/headless-ssr-commerce/app/_components/pagination.tsx b/packages/samples/headless-ssr-commerce/components/pagination.tsx
similarity index 95%
rename from packages/samples/headless-ssr-commerce/app/_components/pagination.tsx
rename to packages/samples/headless-ssr-commerce/components/pagination.tsx
index b8acfd7bb27..12b44d69d5b 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/pagination.tsx
+++ b/packages/samples/headless-ssr-commerce/components/pagination.tsx
@@ -1,6 +1,6 @@
'use client';
-import {usePagination} from '../_lib/commerce-engine';
+import {usePagination} from '../lib/commerce-engine';
export default function Pagination() {
const {state, controller} = usePagination();
diff --git a/packages/samples/headless-ssr-commerce/app/_components/product-list.tsx b/packages/samples/headless-ssr-commerce/components/product-list.tsx
similarity index 93%
rename from packages/samples/headless-ssr-commerce/app/_components/product-list.tsx
rename to packages/samples/headless-ssr-commerce/components/product-list.tsx
index 8c5b9a6701e..950e029ddd9 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/product-list.tsx
+++ b/packages/samples/headless-ssr-commerce/components/product-list.tsx
@@ -2,7 +2,7 @@
import {Product} from '@coveo/headless-react/ssr-commerce';
import {useRouter} from 'next/navigation';
-import {useProductList} from '../_lib/commerce-engine';
+import {useProductList} from '../lib/commerce-engine';
export default function ProductList() {
const {state, controller} = useProductList();
diff --git a/packages/samples/headless-ssr-commerce/app/_components/pages/listing-page.tsx b/packages/samples/headless-ssr-commerce/components/providers/listing-provider.tsx
similarity index 96%
rename from packages/samples/headless-ssr-commerce/app/_components/pages/listing-page.tsx
rename to packages/samples/headless-ssr-commerce/components/providers/listing-provider.tsx
index 4cfc2e95754..6fc7a15681a 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/pages/listing-page.tsx
+++ b/packages/samples/headless-ssr-commerce/components/providers/listing-provider.tsx
@@ -6,14 +6,14 @@ import {
listingEngineDefinition,
ListingHydratedState,
ListingStaticState,
-} from '../../_lib/commerce-engine';
+} from '../../lib/commerce-engine';
interface ListingPageProps {
staticState: ListingStaticState;
navigatorContext: NavigatorContext;
}
-export default function ListingPage({
+export default function ListingProvider({
staticState,
navigatorContext,
children,
diff --git a/packages/samples/headless-ssr-commerce/app/_components/pages/search-page.tsx b/packages/samples/headless-ssr-commerce/components/providers/search-provider.tsx
similarity index 96%
rename from packages/samples/headless-ssr-commerce/app/_components/pages/search-page.tsx
rename to packages/samples/headless-ssr-commerce/components/providers/search-provider.tsx
index e67a82fa0a2..67123dcac14 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/pages/search-page.tsx
+++ b/packages/samples/headless-ssr-commerce/components/providers/search-provider.tsx
@@ -6,14 +6,14 @@ import {
SearchHydratedState,
SearchStaticState,
searchEngineDefinition,
-} from '../../_lib/commerce-engine';
+} from '../../lib/commerce-engine';
interface SearchPageProps {
staticState: SearchStaticState;
navigatorContext: NavigatorContext;
}
-export default function SearchPage({
+export default function SearchProvider({
staticState,
navigatorContext,
children,
diff --git a/packages/samples/headless-ssr-commerce/app/_components/recent-queries.tsx b/packages/samples/headless-ssr-commerce/components/recent-queries.tsx
similarity index 87%
rename from packages/samples/headless-ssr-commerce/app/_components/recent-queries.tsx
rename to packages/samples/headless-ssr-commerce/components/recent-queries.tsx
index 323b4cfb62e..d146477a5e3 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/recent-queries.tsx
+++ b/packages/samples/headless-ssr-commerce/components/recent-queries.tsx
@@ -1,7 +1,4 @@
-import {
- useInstantProducts,
- useRecentQueriesList,
-} from '../_lib/commerce-engine';
+import {useInstantProducts, useRecentQueriesList} from '../lib/commerce-engine';
export default function RecentQueries() {
const {state, controller} = useRecentQueriesList();
diff --git a/packages/samples/headless-ssr-commerce/app/_components/recommendation-list.tsx b/packages/samples/headless-ssr-commerce/components/recommendation-list.tsx
similarity index 94%
rename from packages/samples/headless-ssr-commerce/app/_components/recommendation-list.tsx
rename to packages/samples/headless-ssr-commerce/components/recommendation-list.tsx
index 5e71137880b..6c9cb9ee96e 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/recommendation-list.tsx
+++ b/packages/samples/headless-ssr-commerce/components/recommendation-list.tsx
@@ -3,7 +3,7 @@
import {Product} from '@coveo/headless-react/ssr-commerce';
import {useRouter} from 'next/navigation';
import {FunctionComponent} from 'react';
-import {usePopularBoughtRecs} from '../_lib/commerce-engine';
+import {usePopularBoughtRecs} from '../lib/commerce-engine';
export const Recommendations: FunctionComponent = () => {
// TODO: KIT-3503: refresh recs server side
diff --git a/packages/samples/headless-ssr-commerce/app/_components/search-box.tsx b/packages/samples/headless-ssr-commerce/components/search-box.tsx
similarity index 98%
rename from packages/samples/headless-ssr-commerce/app/_components/search-box.tsx
rename to packages/samples/headless-ssr-commerce/components/search-box.tsx
index 8926119fc4b..ff9a5cbb6f8 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/search-box.tsx
+++ b/packages/samples/headless-ssr-commerce/components/search-box.tsx
@@ -5,7 +5,7 @@ import {
useInstantProducts,
useRecentQueriesList,
useSearchBox,
-} from '../_lib/commerce-engine';
+} from '../lib/commerce-engine';
import InstantProducts from './instant-product';
import RecentQueries from './recent-queries';
diff --git a/packages/samples/headless-ssr-commerce/app/_components/show-more.tsx b/packages/samples/headless-ssr-commerce/components/show-more.tsx
similarity index 91%
rename from packages/samples/headless-ssr-commerce/app/_components/show-more.tsx
rename to packages/samples/headless-ssr-commerce/components/show-more.tsx
index e3bb103b18f..a4952345fd8 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/show-more.tsx
+++ b/packages/samples/headless-ssr-commerce/components/show-more.tsx
@@ -1,6 +1,6 @@
'use client';
-import {usePagination, useSummary} from '../_lib/commerce-engine';
+import {usePagination, useSummary} from '../lib/commerce-engine';
export default function ShowMore() {
const {state, controller} = usePagination();
diff --git a/packages/samples/headless-ssr-commerce/app/_components/sort.tsx b/packages/samples/headless-ssr-commerce/components/sort.tsx
similarity index 96%
rename from packages/samples/headless-ssr-commerce/app/_components/sort.tsx
rename to packages/samples/headless-ssr-commerce/components/sort.tsx
index e6db096652f..ceeb43d935d 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/sort.tsx
+++ b/packages/samples/headless-ssr-commerce/components/sort.tsx
@@ -1,7 +1,7 @@
'use client';
// import {SortBy, SortCriterion} from '@coveo/headless-react/ssr-commerce';
-import {useSort} from '../_lib/commerce-engine';
+import {useSort} from '../lib/commerce-engine';
export default function Sort() {
const {state, controller} = useSort();
diff --git a/packages/samples/headless-ssr-commerce/app/_components/standalone-search-box.tsx b/packages/samples/headless-ssr-commerce/components/standalone-search-box.tsx
similarity index 98%
rename from packages/samples/headless-ssr-commerce/app/_components/standalone-search-box.tsx
rename to packages/samples/headless-ssr-commerce/components/standalone-search-box.tsx
index 5e7b1d61f9a..b3410f9833d 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/standalone-search-box.tsx
+++ b/packages/samples/headless-ssr-commerce/components/standalone-search-box.tsx
@@ -6,7 +6,7 @@ import {
useInstantProducts,
useRecentQueriesList,
useStandaloneSearchBox,
-} from '../_lib/commerce-engine';
+} from '../lib/commerce-engine';
import InstantProducts from './instant-product';
import RecentQueries from './recent-queries';
diff --git a/packages/samples/headless-ssr-commerce/app/_components/summary.tsx b/packages/samples/headless-ssr-commerce/components/summary.tsx
similarity index 93%
rename from packages/samples/headless-ssr-commerce/app/_components/summary.tsx
rename to packages/samples/headless-ssr-commerce/components/summary.tsx
index 887f6d26182..2e8924908e7 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/summary.tsx
+++ b/packages/samples/headless-ssr-commerce/components/summary.tsx
@@ -1,6 +1,6 @@
'use client';
-import {useSummary} from '../_lib/commerce-engine';
+import {useSummary} from '../lib/commerce-engine';
export default function Summary() {
const {state} = useSummary();
diff --git a/packages/samples/headless-ssr-commerce/app/_components/triggers/notify-trigger.tsx b/packages/samples/headless-ssr-commerce/components/triggers/notify-trigger.tsx
similarity index 88%
rename from packages/samples/headless-ssr-commerce/app/_components/triggers/notify-trigger.tsx
rename to packages/samples/headless-ssr-commerce/components/triggers/notify-trigger.tsx
index 035e88d49ee..dde3b5a609a 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/triggers/notify-trigger.tsx
+++ b/packages/samples/headless-ssr-commerce/components/triggers/notify-trigger.tsx
@@ -1,6 +1,6 @@
'use client';
-import {useNotifyTrigger} from '@/app/_lib/commerce-engine';
+import {useNotifyTrigger} from '@/lib/commerce-engine';
import {useCallback, useEffect} from 'react';
// The notify trigger query example in the searchuisamples org is 'notify me'.
diff --git a/packages/samples/headless-ssr-commerce/app/_components/triggers/query-trigger.tsx b/packages/samples/headless-ssr-commerce/components/triggers/query-trigger.tsx
similarity index 85%
rename from packages/samples/headless-ssr-commerce/app/_components/triggers/query-trigger.tsx
rename to packages/samples/headless-ssr-commerce/components/triggers/query-trigger.tsx
index 9b052460347..f3027648a71 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/triggers/query-trigger.tsx
+++ b/packages/samples/headless-ssr-commerce/components/triggers/query-trigger.tsx
@@ -1,6 +1,6 @@
'use client';
-import {useQueryTrigger} from '@/app/_lib/commerce-engine';
+import {useQueryTrigger} from '@/lib/commerce-engine';
// The query trigger query example in the searchuisamples org is 'query me'.
export default function QueryTrigger() {
diff --git a/packages/samples/headless-ssr-commerce/app/_components/triggers/redirection-trigger.tsx b/packages/samples/headless-ssr-commerce/components/triggers/redirection-trigger.tsx
similarity index 87%
rename from packages/samples/headless-ssr-commerce/app/_components/triggers/redirection-trigger.tsx
rename to packages/samples/headless-ssr-commerce/components/triggers/redirection-trigger.tsx
index f97f7992407..84f060eebbe 100644
--- a/packages/samples/headless-ssr-commerce/app/_components/triggers/redirection-trigger.tsx
+++ b/packages/samples/headless-ssr-commerce/components/triggers/redirection-trigger.tsx
@@ -1,6 +1,6 @@
'use client';
-import {useRedirectionTrigger} from '@/app/_lib/commerce-engine';
+import {useRedirectionTrigger} from '@/lib/commerce-engine';
import {useCallback, useEffect} from 'react';
// The redirection trigger query example in the searchuisamples org is 'redirect me'.
diff --git a/packages/samples/headless-ssr-commerce/app/_components/triggers/triggers.tsx b/packages/samples/headless-ssr-commerce/components/triggers/triggers.tsx
similarity index 100%
rename from packages/samples/headless-ssr-commerce/app/_components/triggers/triggers.tsx
rename to packages/samples/headless-ssr-commerce/components/triggers/triggers.tsx
diff --git a/packages/samples/headless-ssr-commerce/app/_lib/commerce-engine-config.ts b/packages/samples/headless-ssr-commerce/lib/commerce-engine-config.ts
similarity index 100%
rename from packages/samples/headless-ssr-commerce/app/_lib/commerce-engine-config.ts
rename to packages/samples/headless-ssr-commerce/lib/commerce-engine-config.ts
diff --git a/packages/samples/headless-ssr-commerce/app/_lib/commerce-engine.ts b/packages/samples/headless-ssr-commerce/lib/commerce-engine.ts
similarity index 100%
rename from packages/samples/headless-ssr-commerce/app/_lib/commerce-engine.ts
rename to packages/samples/headless-ssr-commerce/lib/commerce-engine.ts
diff --git a/packages/samples/headless-ssr-commerce/app/_lib/navigatorContextProvider.ts b/packages/samples/headless-ssr-commerce/lib/navigatorContextProvider.ts
similarity index 100%
rename from packages/samples/headless-ssr-commerce/app/_lib/navigatorContextProvider.ts
rename to packages/samples/headless-ssr-commerce/lib/navigatorContextProvider.ts
diff --git a/packages/samples/headless-ssr-commerce/app/_utils/format-currency.ts b/packages/samples/headless-ssr-commerce/utils/format-currency.ts
similarity index 100%
rename from packages/samples/headless-ssr-commerce/app/_utils/format-currency.ts
rename to packages/samples/headless-ssr-commerce/utils/format-currency.ts
From 3f5d38ef25070bcb7f00769cc2ffb29fa956df10 Mon Sep 17 00:00:00 2001
From: Alex Prudhomme <78121423+alexprudhomme@users.noreply.github.com>
Date: Mon, 4 Nov 2024 07:44:24 -0500
Subject: [PATCH 08/17] better imports
https://coveord.atlassian.net/browse/KIT-3681
---
.../headless-ssr-commerce/components/breadcrumb-manager.tsx | 2 +-
packages/samples/headless-ssr-commerce/components/cart.tsx | 2 +-
.../headless-ssr-commerce/components/instant-product.tsx | 2 +-
.../samples/headless-ssr-commerce/components/pagination.tsx | 2 +-
.../headless-ssr-commerce/components/product-list.tsx | 2 +-
.../components/providers/listing-provider.tsx | 6 +++---
.../components/providers/search-provider.tsx | 6 +++---
.../headless-ssr-commerce/components/recent-queries.tsx | 2 +-
.../components/recommendation-list.tsx | 2 +-
.../samples/headless-ssr-commerce/components/search-box.tsx | 4 ++--
.../samples/headless-ssr-commerce/components/show-more.tsx | 2 +-
packages/samples/headless-ssr-commerce/components/sort.tsx | 2 +-
.../components/standalone-search-box.tsx | 6 +++---
.../samples/headless-ssr-commerce/components/summary.tsx | 2 +-
14 files changed, 21 insertions(+), 21 deletions(-)
diff --git a/packages/samples/headless-ssr-commerce/components/breadcrumb-manager.tsx b/packages/samples/headless-ssr-commerce/components/breadcrumb-manager.tsx
index 76f90f2ea3b..205087426a8 100644
--- a/packages/samples/headless-ssr-commerce/components/breadcrumb-manager.tsx
+++ b/packages/samples/headless-ssr-commerce/components/breadcrumb-manager.tsx
@@ -1,12 +1,12 @@
'use client';
+import {useBreadcrumbManager} from '@/lib/commerce-engine';
import {
NumericFacetValue,
DateFacetValue,
CategoryFacetValue,
RegularFacetValue,
} from '@coveo/headless-react/ssr-commerce';
-import {useBreadcrumbManager} from '../lib/commerce-engine';
export default function BreadcrumbManager() {
const {state, controller} = useBreadcrumbManager();
diff --git a/packages/samples/headless-ssr-commerce/components/cart.tsx b/packages/samples/headless-ssr-commerce/components/cart.tsx
index 964fe776d68..67166642c54 100644
--- a/packages/samples/headless-ssr-commerce/components/cart.tsx
+++ b/packages/samples/headless-ssr-commerce/components/cart.tsx
@@ -1,7 +1,7 @@
'use client';
+import {useCart, useContext} from '@/lib/commerce-engine';
import {CartItem} from '@coveo/headless-react/ssr-commerce';
-import {useCart, useContext} from '../lib/commerce-engine';
import {formatCurrency} from '../utils/format-currency';
export default function Cart() {
diff --git a/packages/samples/headless-ssr-commerce/components/instant-product.tsx b/packages/samples/headless-ssr-commerce/components/instant-product.tsx
index 6ee21eaf3ee..671a985498e 100644
--- a/packages/samples/headless-ssr-commerce/components/instant-product.tsx
+++ b/packages/samples/headless-ssr-commerce/components/instant-product.tsx
@@ -1,6 +1,6 @@
+import {useInstantProducts} from '@/lib/commerce-engine';
import {Product} from '@coveo/headless-react/ssr-commerce';
import {useRouter} from 'next/navigation';
-import {useInstantProducts} from '../lib/commerce-engine';
export default function InstantProducts() {
const router = useRouter();
diff --git a/packages/samples/headless-ssr-commerce/components/pagination.tsx b/packages/samples/headless-ssr-commerce/components/pagination.tsx
index 12b44d69d5b..da73f72b095 100644
--- a/packages/samples/headless-ssr-commerce/components/pagination.tsx
+++ b/packages/samples/headless-ssr-commerce/components/pagination.tsx
@@ -1,6 +1,6 @@
'use client';
-import {usePagination} from '../lib/commerce-engine';
+import {usePagination} from '@/lib/commerce-engine';
export default function Pagination() {
const {state, controller} = usePagination();
diff --git a/packages/samples/headless-ssr-commerce/components/product-list.tsx b/packages/samples/headless-ssr-commerce/components/product-list.tsx
index 950e029ddd9..cc9f8b1ce33 100644
--- a/packages/samples/headless-ssr-commerce/components/product-list.tsx
+++ b/packages/samples/headless-ssr-commerce/components/product-list.tsx
@@ -1,8 +1,8 @@
'use client';
+import {useProductList} from '@/lib/commerce-engine';
import {Product} from '@coveo/headless-react/ssr-commerce';
import {useRouter} from 'next/navigation';
-import {useProductList} from '../lib/commerce-engine';
export default function ProductList() {
const {state, controller} = useProductList();
diff --git a/packages/samples/headless-ssr-commerce/components/providers/listing-provider.tsx b/packages/samples/headless-ssr-commerce/components/providers/listing-provider.tsx
index 6fc7a15681a..c3ff82b4eaf 100644
--- a/packages/samples/headless-ssr-commerce/components/providers/listing-provider.tsx
+++ b/packages/samples/headless-ssr-commerce/components/providers/listing-provider.tsx
@@ -1,12 +1,12 @@
'use client';
-import {NavigatorContext} from '@coveo/headless-react/ssr-commerce';
-import {PropsWithChildren, useEffect, useState} from 'react';
import {
listingEngineDefinition,
ListingHydratedState,
ListingStaticState,
-} from '../../lib/commerce-engine';
+} from '@/lib/commerce-engine';
+import {NavigatorContext} from '@coveo/headless-react/ssr-commerce';
+import {PropsWithChildren, useEffect, useState} from 'react';
interface ListingPageProps {
staticState: ListingStaticState;
diff --git a/packages/samples/headless-ssr-commerce/components/providers/search-provider.tsx b/packages/samples/headless-ssr-commerce/components/providers/search-provider.tsx
index 67123dcac14..a4f3ab8fc85 100644
--- a/packages/samples/headless-ssr-commerce/components/providers/search-provider.tsx
+++ b/packages/samples/headless-ssr-commerce/components/providers/search-provider.tsx
@@ -1,12 +1,12 @@
'use client';
-import {NavigatorContext} from '@coveo/headless-react/ssr-commerce';
-import {PropsWithChildren, useEffect, useState} from 'react';
import {
SearchHydratedState,
SearchStaticState,
searchEngineDefinition,
-} from '../../lib/commerce-engine';
+} from '@/lib/commerce-engine';
+import {NavigatorContext} from '@coveo/headless-react/ssr-commerce';
+import {PropsWithChildren, useEffect, useState} from 'react';
interface SearchPageProps {
staticState: SearchStaticState;
diff --git a/packages/samples/headless-ssr-commerce/components/recent-queries.tsx b/packages/samples/headless-ssr-commerce/components/recent-queries.tsx
index d146477a5e3..c69125cf924 100644
--- a/packages/samples/headless-ssr-commerce/components/recent-queries.tsx
+++ b/packages/samples/headless-ssr-commerce/components/recent-queries.tsx
@@ -1,4 +1,4 @@
-import {useInstantProducts, useRecentQueriesList} from '../lib/commerce-engine';
+import {useInstantProducts, useRecentQueriesList} from '@/lib/commerce-engine';
export default function RecentQueries() {
const {state, controller} = useRecentQueriesList();
diff --git a/packages/samples/headless-ssr-commerce/components/recommendation-list.tsx b/packages/samples/headless-ssr-commerce/components/recommendation-list.tsx
index 6c9cb9ee96e..ae9b38ca613 100644
--- a/packages/samples/headless-ssr-commerce/components/recommendation-list.tsx
+++ b/packages/samples/headless-ssr-commerce/components/recommendation-list.tsx
@@ -1,9 +1,9 @@
'use client';
+import {usePopularBoughtRecs} from '@/lib/commerce-engine';
import {Product} from '@coveo/headless-react/ssr-commerce';
import {useRouter} from 'next/navigation';
import {FunctionComponent} from 'react';
-import {usePopularBoughtRecs} from '../lib/commerce-engine';
export const Recommendations: FunctionComponent = () => {
// TODO: KIT-3503: refresh recs server side
diff --git a/packages/samples/headless-ssr-commerce/components/search-box.tsx b/packages/samples/headless-ssr-commerce/components/search-box.tsx
index ff9a5cbb6f8..27d37b249eb 100644
--- a/packages/samples/headless-ssr-commerce/components/search-box.tsx
+++ b/packages/samples/headless-ssr-commerce/components/search-box.tsx
@@ -1,11 +1,11 @@
'use client';
-import {useState} from 'react';
import {
useInstantProducts,
useRecentQueriesList,
useSearchBox,
-} from '../lib/commerce-engine';
+} from '@/lib/commerce-engine';
+import {useState} from 'react';
import InstantProducts from './instant-product';
import RecentQueries from './recent-queries';
diff --git a/packages/samples/headless-ssr-commerce/components/show-more.tsx b/packages/samples/headless-ssr-commerce/components/show-more.tsx
index a4952345fd8..8778fde4147 100644
--- a/packages/samples/headless-ssr-commerce/components/show-more.tsx
+++ b/packages/samples/headless-ssr-commerce/components/show-more.tsx
@@ -1,6 +1,6 @@
'use client';
-import {usePagination, useSummary} from '../lib/commerce-engine';
+import {usePagination, useSummary} from '@/lib/commerce-engine';
export default function ShowMore() {
const {state, controller} = usePagination();
diff --git a/packages/samples/headless-ssr-commerce/components/sort.tsx b/packages/samples/headless-ssr-commerce/components/sort.tsx
index ceeb43d935d..fbef2533ab3 100644
--- a/packages/samples/headless-ssr-commerce/components/sort.tsx
+++ b/packages/samples/headless-ssr-commerce/components/sort.tsx
@@ -1,7 +1,7 @@
'use client';
// import {SortBy, SortCriterion} from '@coveo/headless-react/ssr-commerce';
-import {useSort} from '../lib/commerce-engine';
+import {useSort} from '@/lib/commerce-engine';
export default function Sort() {
const {state, controller} = useSort();
diff --git a/packages/samples/headless-ssr-commerce/components/standalone-search-box.tsx b/packages/samples/headless-ssr-commerce/components/standalone-search-box.tsx
index b3410f9833d..890c571e8ff 100644
--- a/packages/samples/headless-ssr-commerce/components/standalone-search-box.tsx
+++ b/packages/samples/headless-ssr-commerce/components/standalone-search-box.tsx
@@ -1,12 +1,12 @@
'use client';
-import {useRouter} from 'next/navigation';
-import {useEffect, useState} from 'react';
import {
useInstantProducts,
useRecentQueriesList,
useStandaloneSearchBox,
-} from '../lib/commerce-engine';
+} from '@/lib/commerce-engine';
+import {useRouter} from 'next/navigation';
+import {useEffect, useState} from 'react';
import InstantProducts from './instant-product';
import RecentQueries from './recent-queries';
diff --git a/packages/samples/headless-ssr-commerce/components/summary.tsx b/packages/samples/headless-ssr-commerce/components/summary.tsx
index 2e8924908e7..a8871e7a3c1 100644
--- a/packages/samples/headless-ssr-commerce/components/summary.tsx
+++ b/packages/samples/headless-ssr-commerce/components/summary.tsx
@@ -1,6 +1,6 @@
'use client';
-import {useSummary} from '../lib/commerce-engine';
+import {useSummary} from '@/lib/commerce-engine';
export default function Summary() {
const {state} = useSummary();
From 4ee80abfc3c7287ecc86d7fffbdd1480f611cba3 Mon Sep 17 00:00:00 2001
From: Alex Prudhomme <78121423+alexprudhomme@users.noreply.github.com>
Date: Mon, 4 Nov 2024 10:51:40 -0500
Subject: [PATCH 09/17] cart in progress
https://coveord.atlassian.net/browse/KIT-3681
---
.../app/listing/page.tsx | 7 +
.../headless-ssr-commerce/components/cart.tsx | 2 +-
.../lib/commerce-engine-builder.ts | 158 ++++++++++++++++++
.../lib/commerce-engine-config.ts | 1 +
4 files changed, 167 insertions(+), 1 deletion(-)
create mode 100644 packages/samples/headless-ssr-commerce/lib/commerce-engine-builder.ts
diff --git a/packages/samples/headless-ssr-commerce/app/listing/page.tsx b/packages/samples/headless-ssr-commerce/app/listing/page.tsx
index 561c4882511..13fb122cfa6 100644
--- a/packages/samples/headless-ssr-commerce/app/listing/page.tsx
+++ b/packages/samples/headless-ssr-commerce/app/listing/page.tsx
@@ -25,6 +25,13 @@ export default async function Listing() {
// Fetches the static state of the app with initial state (when applicable)
const staticState = await listingEngineDefinition.fetchStaticState();
+ //At this point in the app, this is the only part that is in the server side
+
+ // I cant do this here, I need to define them in an API route.
+ // const listingDefinition = await getEngineDefinition('listing');
+
+ // const hooks = await getHooks();
+
return (
+>;
+
+export async function getEngineDefinition(useCase: 'listing' | 'search') {
+ //TODO: create a cart API route that returns the cart state
+ const cartInitialState = {
+ items: [
+ {
+ productId: 'SP01057_00001',
+ quantity: 1,
+ name: 'Kayaker Canoe',
+ price: 800,
+ },
+ ],
+ };
+
+ const config = {
+ configuration: {
+ ...getSampleCommerceEngineConfiguration(),
+ context: {
+ language: 'en',
+ country: 'US',
+ currency: 'USD',
+ view: {
+ url: 'https://sports.barca.group/browse/promotions/ui-kit-testing',
+ },
+ },
+ },
+ controllers: {
+ summary: defineSummary(),
+ productList: defineProductList(),
+ popularViewedRecs: defineRecommendations({
+ options: {
+ slotId: 'd73afbd2-8521-4ee6-a9b8-31f064721e73',
+ },
+ }),
+ popularBoughtRecs: defineRecommendations({
+ options: {
+ slotId: 'af4fb7ba-6641-4b67-9cf9-be67e9f30174',
+ },
+ }),
+ cart: defineCart({initialState: cartInitialState}),
+ searchBox: defineSearchBox(),
+ context: defineContext(),
+ recentQueriesList: defineRecentQueriesList(),
+ notifyTrigger: defineNotifyTrigger(),
+ queryTrigger: defineQueryTrigger(),
+ redirectionTrigger: defineRedirectionTrigger(),
+ standaloneSearchBox: defineStandaloneSearchBox({
+ options: {redirectionUrl: '/search'},
+ }),
+ instantProducts: defineInstantProducts({options: {}}),
+ pagination: definePagination({options: {pageSize: 9}}),
+ sort: defineSort(),
+ productView: defineProductView(),
+ didYouMean: defineDidYouMean(),
+ facetGenerator: defineFacetGenerator(),
+ breadcrumbManager: defineBreadcrumbManager(),
+ },
+ } satisfies CommerceEngineConfig;
+
+ const engineDefinition = defineCommerceEngine(config);
+
+ return useCase === 'listing'
+ ? engineDefinition.listingEngineDefinition
+ : engineDefinition.searchEngineDefinition;
+}
+
+export async function getHooks() {
+ const cartInitialState = {
+ items: [
+ {
+ productId: 'SP01057_00001',
+ quantity: 1,
+ name: 'Kayaker Canoe',
+ price: 800,
+ },
+ ],
+ };
+
+ const config = {
+ configuration: {
+ ...getSampleCommerceEngineConfiguration(),
+ context: {
+ language: 'en',
+ country: 'US',
+ currency: 'USD',
+ view: {
+ url: 'https://sports.barca.group/browse/promotions/ui-kit-testing',
+ },
+ },
+ },
+ controllers: {
+ summary: defineSummary(),
+ productList: defineProductList(),
+ popularViewedRecs: defineRecommendations({
+ options: {
+ slotId: 'd73afbd2-8521-4ee6-a9b8-31f064721e73',
+ },
+ }),
+ popularBoughtRecs: defineRecommendations({
+ options: {
+ slotId: 'af4fb7ba-6641-4b67-9cf9-be67e9f30174',
+ },
+ }),
+ cart: defineCart({initialState: cartInitialState}),
+ searchBox: defineSearchBox(),
+ context: defineContext(),
+ recentQueriesList: defineRecentQueriesList(),
+ notifyTrigger: defineNotifyTrigger(),
+ queryTrigger: defineQueryTrigger(),
+ redirectionTrigger: defineRedirectionTrigger(),
+ standaloneSearchBox: defineStandaloneSearchBox({
+ options: {redirectionUrl: '/search'},
+ }),
+ instantProducts: defineInstantProducts({options: {}}),
+ pagination: definePagination({options: {pageSize: 9}}),
+ sort: defineSort(),
+ productView: defineProductView(),
+ didYouMean: defineDidYouMean(),
+ facetGenerator: defineFacetGenerator(),
+ breadcrumbManager: defineBreadcrumbManager(),
+ },
+ } satisfies CommerceEngineConfig;
+
+ const engineDefinition = defineCommerceEngine(config);
+
+ return engineDefinition.controllers;
+}
diff --git a/packages/samples/headless-ssr-commerce/lib/commerce-engine-config.ts b/packages/samples/headless-ssr-commerce/lib/commerce-engine-config.ts
index 4a02e7b0287..bb7a63b83ee 100644
--- a/packages/samples/headless-ssr-commerce/lib/commerce-engine-config.ts
+++ b/packages/samples/headless-ssr-commerce/lib/commerce-engine-config.ts
@@ -28,6 +28,7 @@ type CommerceEngineConfig = CommerceEngineDefinitionOptions<
ControllerDefinitionsMap
>;
+// This must be a async function
export default {
configuration: {
...getSampleCommerceEngineConfiguration(),
From afbe39d19bc673385f56b132af1995dddfd3b48d Mon Sep 17 00:00:00 2001
From: ylakhdar
Date: Mon, 4 Nov 2024 12:09:51 -0500
Subject: [PATCH 10/17] change the way cart items are passed
https://coveord.atlassian.net/browse/KIT-3681
---
.../src/app/commerce-ssr-engine/types/common.ts | 6 ++++++
.../commerce/context/cart/headless-cart.ssr.ts | 15 ++++++++++-----
packages/headless/src/ssr-commerce.index.ts | 1 +
.../headless-ssr-commerce/app/listing/page.tsx | 8 +++++++-
.../app/products/[productId]/page.tsx | 8 +++++++-
.../headless-ssr-commerce/app/search/page.tsx | 8 +++++++-
.../components/pages/product-page.tsx | 5 +++++
.../components/providers/listing-provider.tsx | 5 +++++
.../components/providers/search-provider.tsx | 5 +++++
.../samples/headless-ssr-commerce/lib/cart.ts | 6 ++++++
10 files changed, 59 insertions(+), 8 deletions(-)
create mode 100644 packages/samples/headless-ssr-commerce/lib/cart.ts
diff --git a/packages/headless/src/app/commerce-ssr-engine/types/common.ts b/packages/headless/src/app/commerce-ssr-engine/types/common.ts
index fdf21309c0e..b773dd642f0 100644
--- a/packages/headless/src/app/commerce-ssr-engine/types/common.ts
+++ b/packages/headless/src/app/commerce-ssr-engine/types/common.ts
@@ -210,6 +210,12 @@ export type UniversalControllerDefinitionWithoutProps<
> = ControllerDefinitionWithoutProps &
UniversalController;
+export type UniversalControllerDefinitionWithProps<
+ TController extends Controller,
+ TProps,
+> = ControllerDefinitionWithProps &
+ UniversalController;
+
export type SearchAndListingControllerDefinitionWithoutProps<
TController extends Controller,
> = ControllerDefinitionWithoutProps &
diff --git a/packages/headless/src/controllers/commerce/context/cart/headless-cart.ssr.ts b/packages/headless/src/controllers/commerce/context/cart/headless-cart.ssr.ts
index 997ccba91d2..903c64708c6 100644
--- a/packages/headless/src/controllers/commerce/context/cart/headless-cart.ssr.ts
+++ b/packages/headless/src/controllers/commerce/context/cart/headless-cart.ssr.ts
@@ -1,11 +1,15 @@
-import {UniversalControllerDefinitionWithoutProps} from '../../../../app/commerce-ssr-engine/types/common.js';
-import {Cart, buildCart, CartProps, CartInitialState} from './headless-cart.js';
+import {UniversalControllerDefinitionWithProps} from '../../../../app/commerce-ssr-engine/types/common.js';
+import {Cart, buildCart, CartInitialState} from './headless-cart.js';
export type {CartState, CartItem, CartProps} from './headless-cart.js';
export type {Cart, CartInitialState};
+export interface CartBuildProps {
+ initialState: CartInitialState;
+}
+
export interface CartDefinition
- extends UniversalControllerDefinitionWithoutProps {}
+ extends UniversalControllerDefinitionWithProps {}
/**
* Defines a `Cart` controller instance.
@@ -15,11 +19,12 @@ export interface CartDefinition
*
* @internal
*/
-export function defineCart(props: CartProps = {}): CartDefinition {
+export function defineCart(): CartDefinition {
return {
listing: true,
search: true,
standalone: true,
- build: (engine) => buildCart(engine, props),
+ buildWithProps: (engine, props) =>
+ buildCart(engine, {initialState: props.initialState}),
};
}
diff --git a/packages/headless/src/ssr-commerce.index.ts b/packages/headless/src/ssr-commerce.index.ts
index 2ff89354831..f98661b8fef 100644
--- a/packages/headless/src/ssr-commerce.index.ts
+++ b/packages/headless/src/ssr-commerce.index.ts
@@ -192,6 +192,7 @@ export type {
CartItem,
CartProps,
CartState,
+ CartBuildProps,
CartDefinition,
} from './controllers/commerce/context/cart/headless-cart.ssr.js';
export {defineCart} from './controllers/commerce/context/cart/headless-cart.ssr.js';
diff --git a/packages/samples/headless-ssr-commerce/app/listing/page.tsx b/packages/samples/headless-ssr-commerce/app/listing/page.tsx
index 13fb122cfa6..9f5a3ed44ec 100644
--- a/packages/samples/headless-ssr-commerce/app/listing/page.tsx
+++ b/packages/samples/headless-ssr-commerce/app/listing/page.tsx
@@ -8,6 +8,7 @@ import {Recommendations} from '@/components/recommendation-list';
import Sort from '@/components/sort';
import StandaloneSearchBox from '@/components/standalone-search-box';
import Summary from '@/components/summary';
+import getCart from '@/lib/cart';
import {listingEngineDefinition} from '@/lib/commerce-engine';
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {headers} from 'next/headers';
@@ -22,8 +23,13 @@ export default async function Listing() {
const navigatorContext = new NextJsNavigatorContext(headers());
listingEngineDefinition.setNavigatorContextProvider(() => navigatorContext);
+ // Fetches the cart items from an external service
+ const items = await getCart();
+
// Fetches the static state of the app with initial state (when applicable)
- const staticState = await listingEngineDefinition.fetchStaticState();
+ const staticState = await listingEngineDefinition.fetchStaticState({
+ controllers: {cart: {initialState: {items}}},
+ });
//At this point in the app, this is the only part that is in the server side
diff --git a/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx b/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx
index 75a3c183033..335be6acc55 100644
--- a/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx
+++ b/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx
@@ -1,4 +1,5 @@
import ProductPage from '@/components/pages/product-page';
+import getCart from '@/lib/cart';
import {searchEngineDefinition} from '@/lib/commerce-engine';
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {headers} from 'next/headers';
@@ -13,8 +14,13 @@ export default async function ProductDescriptionPage({
const navigatorContext = new NextJsNavigatorContext(headers());
searchEngineDefinition.setNavigatorContextProvider(() => navigatorContext);
+ // Fetches the cart items from an external service
+ const items = await getCart();
+
// Fetches the static state of the app with initial state (when applicable)
- const staticState = await searchEngineDefinition.fetchStaticState();
+ const staticState = await searchEngineDefinition.fetchStaticState({
+ controllers: {cart: {initialState: {items}}},
+ });
return (
<>
Product description page
diff --git a/packages/samples/headless-ssr-commerce/app/search/page.tsx b/packages/samples/headless-ssr-commerce/app/search/page.tsx
index a3f7fa01698..133620203f2 100644
--- a/packages/samples/headless-ssr-commerce/app/search/page.tsx
+++ b/packages/samples/headless-ssr-commerce/app/search/page.tsx
@@ -7,6 +7,7 @@ import SearchBox from '@/components/search-box';
import ShowMore from '@/components/show-more';
import Summary from '@/components/summary';
import Triggers from '@/components/triggers/triggers';
+import getCart from '@/lib/cart';
import {searchEngineDefinition} from '@/lib/commerce-engine';
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {headers} from 'next/headers';
@@ -16,8 +17,13 @@ export default async function Search() {
const navigatorContext = new NextJsNavigatorContext(headers());
searchEngineDefinition.setNavigatorContextProvider(() => navigatorContext);
+ // Fetches the cart items from an external service
+ const items = await getCart();
+
// Fetches the static state of the app with initial state (when applicable)
- const staticState = await searchEngineDefinition.fetchStaticState();
+ const staticState = await searchEngineDefinition.fetchStaticState({
+ controllers: {cart: {initialState: {items}}},
+ });
return (
{
setHydratedState({engine, controllers});
diff --git a/packages/samples/headless-ssr-commerce/components/providers/listing-provider.tsx b/packages/samples/headless-ssr-commerce/components/providers/listing-provider.tsx
index c3ff82b4eaf..a7a75551d47 100644
--- a/packages/samples/headless-ssr-commerce/components/providers/listing-provider.tsx
+++ b/packages/samples/headless-ssr-commerce/components/providers/listing-provider.tsx
@@ -29,6 +29,11 @@ export default function ListingProvider({
listingEngineDefinition
.hydrateStaticState({
searchAction: staticState.searchAction,
+ controllers: {
+ cart: {
+ initialState: {items: staticState.controllers.cart.state.items},
+ },
+ },
})
.then(({engine, controllers}) => {
setHydratedState({engine, controllers});
diff --git a/packages/samples/headless-ssr-commerce/components/providers/search-provider.tsx b/packages/samples/headless-ssr-commerce/components/providers/search-provider.tsx
index a4f3ab8fc85..fe864f70a0d 100644
--- a/packages/samples/headless-ssr-commerce/components/providers/search-provider.tsx
+++ b/packages/samples/headless-ssr-commerce/components/providers/search-provider.tsx
@@ -29,6 +29,11 @@ export default function SearchProvider({
searchEngineDefinition
.hydrateStaticState({
searchAction: staticState.searchAction,
+ controllers: {
+ cart: {
+ initialState: {items: staticState.controllers.cart.state.items},
+ },
+ },
})
.then(({engine, controllers}) => {
setHydratedState({engine, controllers});
diff --git a/packages/samples/headless-ssr-commerce/lib/cart.ts b/packages/samples/headless-ssr-commerce/lib/cart.ts
new file mode 100644
index 00000000000..4ce95770737
--- /dev/null
+++ b/packages/samples/headless-ssr-commerce/lib/cart.ts
@@ -0,0 +1,6 @@
+import {CartItem} from '@coveo/headless-react/ssr-commerce';
+
+export default async function getCart(): Promise {
+ 'use server';
+ return [{name: 'item1', price: 10, quantity: 1, productId: 'xxx'}];
+}
From fc579a5e8fce3bf53b736d4096bd8be3375d94c8 Mon Sep 17 00:00:00 2001
From: Alex Prudhomme <78121423+alexprudhomme@users.noreply.github.com>
Date: Mon, 4 Nov 2024 14:45:09 -0500
Subject: [PATCH 11/17] chore: implement a better cart in headless-ssr-commerce
sample
https://coveord.atlassian.net/browse/KIT-3681
---
.../headless-ssr-commerce/actions/cart.ts | 50 ++++++
.../headless-ssr-commerce/app/cart/page.tsx | 33 ++++
.../headless-ssr-commerce/app/layout.tsx | 1 +
.../app/listing/page.tsx | 9 +-
.../app/products/[productId]/page.tsx | 2 +-
.../headless-ssr-commerce/app/search/page.tsx | 2 +-
.../headless-ssr-commerce/components/cart.tsx | 36 ++--
.../components/instant-product.tsx | 7 +-
.../components/product-list.tsx | 7 +-
.../samples/headless-ssr-commerce/lib/cart.ts | 6 -
.../lib/commerce-engine-builder.ts | 158 ------------------
.../lib/commerce-engine-config.ts | 1 -
.../headless-ssr-commerce/utils/cart.ts | 40 +++++
13 files changed, 154 insertions(+), 198 deletions(-)
create mode 100644 packages/samples/headless-ssr-commerce/actions/cart.ts
create mode 100644 packages/samples/headless-ssr-commerce/app/cart/page.tsx
delete mode 100644 packages/samples/headless-ssr-commerce/lib/cart.ts
delete mode 100644 packages/samples/headless-ssr-commerce/lib/commerce-engine-builder.ts
create mode 100644 packages/samples/headless-ssr-commerce/utils/cart.ts
diff --git a/packages/samples/headless-ssr-commerce/actions/cart.ts b/packages/samples/headless-ssr-commerce/actions/cart.ts
new file mode 100644
index 00000000000..e7f9b98cabf
--- /dev/null
+++ b/packages/samples/headless-ssr-commerce/actions/cart.ts
@@ -0,0 +1,50 @@
+'use server';
+
+import {CartItem} from '@coveo/headless-react/ssr-commerce';
+import {cookies} from 'next/headers';
+
+function getCartFromCookies(): CartItem[] {
+ const cartCookie = cookies().get('headless-cart');
+ return cartCookie ? JSON.parse(cartCookie.value) : [];
+}
+
+function setCartInCookies(cart: CartItem[]) {
+ cookies().set('headless-cart', JSON.stringify(cart), {
+ path: '/',
+ maxAge: 60 * 60 * 24,
+ });
+}
+
+export async function getCart(): Promise {
+ return getCartFromCookies();
+}
+
+export async function addItemToCart(newItem: CartItem): Promise {
+ const cart = getCartFromCookies();
+ cart.push(newItem);
+ setCartInCookies(cart);
+ return cart;
+}
+
+export async function updateItemQuantity(
+ updatedItem: CartItem
+): Promise {
+ let cart = getCartFromCookies();
+ const existingItem = cart.find(
+ (item) => item.productId === updatedItem.productId
+ );
+ if (existingItem) {
+ if (updatedItem.quantity === 0) {
+ cart = cart.filter((item) => item.productId !== updatedItem.productId);
+ } else {
+ existingItem.quantity = updatedItem.quantity;
+ }
+ }
+ setCartInCookies(cart);
+ return cart;
+}
+
+export async function clearCart(): Promise {
+ setCartInCookies([]);
+ return [];
+}
diff --git a/packages/samples/headless-ssr-commerce/app/cart/page.tsx b/packages/samples/headless-ssr-commerce/app/cart/page.tsx
new file mode 100644
index 00000000000..8187ecb6d6c
--- /dev/null
+++ b/packages/samples/headless-ssr-commerce/app/cart/page.tsx
@@ -0,0 +1,33 @@
+import {getCart} from '@/actions/cart';
+import Cart from '@/components/cart';
+import SearchProvider from '@/components/providers/search-provider';
+import {searchEngineDefinition} from '@/lib/commerce-engine';
+import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
+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);
+
+ // Fetches the cart items from an external service
+ const items = await getCart();
+
+ // Fetches the static state of the app with initial state (when applicable)
+ const staticState = await searchEngineDefinition.fetchStaticState({
+ controllers: {cart: {initialState: {items}}},
+ });
+
+ return (
+
+