-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(headless-react): add standalone provider in sample (#4674)
Add missing provider on PDP https://coveord.atlassian.net/browse/KIT-3739 --------- Co-authored-by: Alex Prudhomme <78121423+alexprudhomme@users.noreply.github.com>
- Loading branch information
1 parent
5de23b7
commit 8fb043f
Showing
2 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/samples/headless-ssr-commerce/components/providers/standalone-provider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} | ||
} |