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-react): add standalone provider in sample #4674

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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,6 +1,8 @@
import * as externalCartAPI from '@/actions/external-cart-api';
import ContextDropdown from '@/components/context-dropdown';
import ProductPage from '@/components/pages/product-page';
import StandaloneProvider from '@/components/providers/standalone-provider';
import StandaloneSearchBox from '@/components/standalone-search-box';
import {searchEngineDefinition} from '@/lib/commerce-engine';
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {defaultContext} from '@/utils/context';
Expand Down Expand Up @@ -34,17 +36,21 @@ export default async function ProductDescriptionPage({
},
});
return (
<>
<StandaloneProvider
staticState={staticState}
navigatorContext={navigatorContext.marshal}
>
<h2>Product description page</h2>
<ContextDropdown />
<StandaloneSearchBox />
<Suspense fallback={<p>Loading...</p>}>
<ProductPage
staticState={staticState}
navigatorContext={navigatorContext.marshal}
productId={params.productId}
/>
</Suspense>
</>
</StandaloneProvider>
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use client';

import {
StandaloneHydratedState,
StandaloneStaticState,
standaloneEngineDefinition,
} from '@/lib/commerce-engine';
import {NavigatorContext} from '@coveo/headless-react/ssr-commerce';
import {PropsWithChildren, useEffect, useState} from 'react';

interface StandalonePageProps {
staticState: StandaloneStaticState;
navigatorContext: NavigatorContext;
}

export default function StandaloneProvider({
staticState,
navigatorContext,
children,
}: PropsWithChildren<StandalonePageProps>) {
const [hydratedState, setHydratedState] = useState<
StandaloneHydratedState | undefined
>(undefined);

// Setting the navigator context provider also in client-side before hydrating the application
standaloneEngineDefinition.setNavigatorContextProvider(
() => navigatorContext
);

useEffect(() => {
standaloneEngineDefinition
.hydrateStaticState({
searchAction: staticState.searchAction,
controllers: {
cart: {
initialState: {items: staticState.controllers.cart.state.items},
},
context: staticState.controllers.context.state,
},
})
.then(({engine, controllers}) => {
setHydratedState({engine, controllers});

// 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();
});
}, [staticState]);

if (hydratedState) {
return (
<standaloneEngineDefinition.HydratedStateProvider
engine={hydratedState.engine}
controllers={hydratedState.controllers}
>
{/* // 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}</>
</standaloneEngineDefinition.HydratedStateProvider>
);
} else {
return (
<standaloneEngineDefinition.StaticStateProvider
controllers={staticState.controllers}
>
{/* // 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}</>
</standaloneEngineDefinition.StaticStateProvider>
);
}
}
Loading