Skip to content

Commit

Permalink
change the way cart items are passed
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lakhdar committed Nov 4, 2024
1 parent 4ee80ab commit afbe39d
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 8 deletions.
6 changes: 6 additions & 0 deletions packages/headless/src/app/commerce-ssr-engine/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ export type UniversalControllerDefinitionWithoutProps<
> = ControllerDefinitionWithoutProps<CommerceEngine, TController> &
UniversalController;

export type UniversalControllerDefinitionWithProps<
TController extends Controller,
TProps,
> = ControllerDefinitionWithProps<CommerceEngine, TController, TProps> &
UniversalController;

export type SearchAndListingControllerDefinitionWithoutProps<
TController extends Controller,
> = ControllerDefinitionWithoutProps<CommerceEngine, TController> &
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Cart> {}
extends UniversalControllerDefinitionWithProps<Cart, CartBuildProps> {}

/**
* Defines a `Cart` controller instance.
Expand All @@ -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}),
};
}
1 change: 1 addition & 0 deletions packages/headless/src/ssr-commerce.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
8 changes: 7 additions & 1 deletion packages/samples/headless-ssr-commerce/app/listing/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 (
<>
<h2>Product description page</h2>
Expand Down
8 changes: 7 additions & 1 deletion packages/samples/headless-ssr-commerce/app/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 (
<SearchProvider
staticState={staticState}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export default function ProductPage(props: IProductPageProps) {
standaloneEngineDefinition
.hydrateStaticState({
searchAction: staticState.searchAction,
controllers: {
cart: {
initialState: {items: staticState.controllers.cart.state.items},
},
},
})
.then(({engine, controllers}) => {
setHydratedState({engine, controllers});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
6 changes: 6 additions & 0 deletions packages/samples/headless-ssr-commerce/lib/cart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {CartItem} from '@coveo/headless-react/ssr-commerce';

export default async function getCart(): Promise<CartItem[]> {
'use server';
return [{name: 'item1', price: 10, quantity: 1, productId: 'xxx'}];
}

0 comments on commit afbe39d

Please sign in to comment.