-
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.
- Loading branch information
Showing
5 changed files
with
91 additions
and
5 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
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
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
63 changes: 63 additions & 0 deletions
63
packages/samples/headless-ssr-commerce/components/providers/recommendation-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,63 @@ | ||
'use client'; | ||
|
||
import { | ||
recommendationEngineDefinition, | ||
RecommendationHydratedState, | ||
RecommendationStaticState, | ||
} from '@/lib/commerce-engine'; | ||
import {NavigatorContext} from '@coveo/headless-react/ssr-commerce'; | ||
import {PropsWithChildren, useEffect, useState} from 'react'; | ||
|
||
interface RecommendationPageProps { | ||
staticState: RecommendationStaticState; | ||
navigatorContext: NavigatorContext; | ||
} | ||
|
||
export default function RecommendationProvider({ | ||
staticState, | ||
navigatorContext, | ||
children, | ||
}: PropsWithChildren<RecommendationPageProps>) { | ||
const [hydratedState, setHydratedState] = useState< | ||
RecommendationHydratedState | undefined | ||
>(undefined); | ||
|
||
// Setting the navigator context provider also in client-side before hydrating the application | ||
recommendationEngineDefinition.setNavigatorContextProvider( | ||
() => navigatorContext | ||
); | ||
|
||
useEffect(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(recommendationEngineDefinition.hydrateStaticState as any)({ | ||
searchActions: staticState.searchActions, | ||
// controllers: { }, | ||
}).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(); // FIXME: does not work | ||
}); | ||
}, [staticState]); | ||
|
||
if (hydratedState) { | ||
return ( | ||
<recommendationEngineDefinition.HydratedStateProvider | ||
engine={hydratedState.engine} | ||
controllers={hydratedState.controllers} | ||
> | ||
<>{children}</> | ||
</recommendationEngineDefinition.HydratedStateProvider> | ||
); | ||
} else { | ||
return ( | ||
<recommendationEngineDefinition.StaticStateProvider | ||
controllers={staticState.controllers} | ||
> | ||
{/* // TODO: Add 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}</> | ||
</recommendationEngineDefinition.StaticStateProvider> | ||
); | ||
} | ||
} |
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