From f0ac743f4e8e0afe595ad9348e626efeeaa25db5 Mon Sep 17 00:00:00 2001 From: Alex Prudhomme <78121423+alexprudhomme@users.noreply.github.com> Date: Fri, 15 Nov 2024 11:38:18 -0500 Subject: [PATCH] clean up and add to headless-react --- .../src/ssr-commerce/commerce-engine.tsx | 11 ++ .../commerce-engine/commerce-engine.ssr.ts | 105 ++---------------- .../commerce-ssr-engine/types/core-engine.ts | 2 +- .../ssr-engine/types/fetch-static-state.ts | 4 +- .../app/ssr-engine/types/from-build-result.ts | 12 ++ packages/headless/src/ssr-commerce.index.ts | 3 +- 6 files changed, 35 insertions(+), 102 deletions(-) diff --git a/packages/headless-react/src/ssr-commerce/commerce-engine.tsx b/packages/headless-react/src/ssr-commerce/commerce-engine.tsx index 76a0f755837..8807fa2c773 100644 --- a/packages/headless-react/src/ssr-commerce/commerce-engine.tsx +++ b/packages/headless-react/src/ssr-commerce/commerce-engine.tsx @@ -60,11 +60,13 @@ export function defineCommerceEngine< >; type ListingContext = ContextStateType; type SearchContext = ContextStateType; + type RecommendationContext = ContextStateType; type StandaloneContext = ContextStateType; const { listingEngineDefinition, searchEngineDefinition, + recommendationEngineDefinition, standaloneEngineDefinition, } = defineBaseCommerceEngine({...options}); return { @@ -89,6 +91,15 @@ export function defineCommerceEngine< singletonContext as SearchContext ), }, + recommendationEngineDefinition: { + ...recommendationEngineDefinition, + StaticStateProvider: buildStaticStateProvider( + singletonContext as RecommendationContext + ), + HydratedStateProvider: buildHydratedStateProvider( + singletonContext as RecommendationContext + ), + }, standaloneEngineDefinition: { ...standaloneEngineDefinition, StaticStateProvider: buildStaticStateProvider( diff --git a/packages/headless/src/app/commerce-engine/commerce-engine.ssr.ts b/packages/headless/src/app/commerce-engine/commerce-engine.ssr.ts index e50903b6350..89c1a764493 100644 --- a/packages/headless/src/app/commerce-engine/commerce-engine.ssr.ts +++ b/packages/headless/src/app/commerce-engine/commerce-engine.ssr.ts @@ -397,80 +397,36 @@ export function defineCommerceEngine< }; }; - // const recommendationFetchStaticStateFactory: () => RecommendationFetchStaticStateFunction = - // () => - // composeFunction( - // async (...params: RecommendationFetchStaticStateParameters) => { - // const buildResult = await recommendationBuildFactory()(...params); - // // I can't do it all, I need to split them all - // //What the hell, this function calls itself ? - // const staticState = - // await recommendationFetchStaticStateFactory().fromBuildResult({ - // buildResult, - // }); - // return staticState; - // }, - // { - // fromBuildResult: async ( - // ...params: RecommendationFetchStaticFromBuildResultsParameters - // ) => { - // const [ - // { - // buildResult: {engine, controllers}, - // }, - // ] = params; - - // // here build the filter and refresh them all - // // build every recommendation and refresh them all ? - // // buildRecommendations(engine).refresh(); - // recommendationFilter.refresh(controllers); - - // const searchAction = await engine.waitForRequestCompletedAction(); - - // return createStaticState({ - // searchAction, - // controllers, - // }) as EngineStaticState< - // UnknownAction, - // InferControllerStaticStateMapFromDefinitionsWithSolutionType< - // TControllerDefinitions, - // SolutionType.recommendation - // > - // >; - // }, - // } - // ); - - const recommendationFetchStaticStateFactory3 = () => { - // Primary function logic + const recommendationFetchStaticStateFactory = () => { const fetchStaticState = async ( ...params: RecommendationFetchStaticStateParameters ) => { const buildResult = await recommendationBuildFactory()(...params); - const staticState = await fromBuildResult({ + const [c] = params; + + const staticState = await fromBuildResult(c, { buildResult, }); return staticState; }; - // Attach fromBuildResult as a property to match the expected type fetchStaticState.fromBuildResult = fromBuildResult; return fetchStaticState; }; - // Define fromBuildResult separately const fromBuildResult = async ( ...params: RecommendationFetchStaticFromBuildResultsParameters ) => { const [ + c, { buildResult: {engine, controllers}, }, ] = params; - // Handle solutionType conditions and refresh logic + console.log(c); recommendationFilter.refresh(controllers); @@ -487,53 +443,6 @@ export function defineCommerceEngine< > >; }; - // const recommendationFetchStaticStateFactory2: ( - // solutionType: SolutionType - // ) => RecommendationFetchStaticStateFunction = (solutionType) => () => - // composeFunction( - // async (...params: RecommendationFetchStaticStateParameters) => { - // const buildResult = await recommendationBuildFactory()(...params); - - // const staticState = await recommendationFetchStaticStateFactory( - // solutionType - // ).fromBuildResult({ - // buildResult, - // }); - // return staticState; - // }, - // { - // fromBuildResult: async ( - // ...params: RecommendationFetchStaticFromBuildResultsParameters - // ) => { - // const [ - // { - // buildResult: {engine, controllers}, - // }, - // ] = params; - - // if (solutionType === SolutionType.listing) { - // buildProductListing(engine).executeFirstRequest(); - // } else if (solutionType === SolutionType.search) { - // buildSearch(engine).executeFirstSearch(); - // } else if (solutionType === SolutionType.recommendation) { - // recommendationFilter.refresh(controllers); - // } - - // const searchAction = await engine.waitForRequestCompletedAction(); - - // return createStaticState({ - // searchAction, - // controllers, - // }) as EngineStaticState< - // UnknownAction, - // InferControllerStaticStateMapFromDefinitionsWithSolutionType< - // TControllerDefinitions, - // SolutionType.recommendation - // > - // >; - // }, - // } - // ); return { listingEngineDefinition: { @@ -559,7 +468,7 @@ export function defineCommerceEngine< >, recommendationEngineDefinition: { build: recommendationBuildFactory(), - fetchStaticState: recommendationFetchStaticStateFactory3(), + fetchStaticState: recommendationFetchStaticStateFactory(), hydrateStaticState: hydrateStaticStateFactory( SolutionType.recommendation ), diff --git a/packages/headless/src/app/commerce-ssr-engine/types/core-engine.ts b/packages/headless/src/app/commerce-ssr-engine/types/core-engine.ts index 5e43667a39b..7e79213a355 100644 --- a/packages/headless/src/app/commerce-ssr-engine/types/core-engine.ts +++ b/packages/headless/src/app/commerce-ssr-engine/types/core-engine.ts @@ -17,7 +17,7 @@ import { InferControllerStaticStateMapFromDefinitionsWithSolutionType, } from './common.js'; -export type {HydrateStaticState, FetchStaticState}; +export type {HydrateStaticState, FetchStaticState, FetchStaticStateWithList}; export type EngineDefinitionOptions< TOptions extends {configuration: EngineConfiguration}, TControllers extends ControllerDefinitionsMap< diff --git a/packages/headless/src/app/ssr-engine/types/fetch-static-state.ts b/packages/headless/src/app/ssr-engine/types/fetch-static-state.ts index 936a7609082..45b8fedd8b1 100644 --- a/packages/headless/src/app/ssr-engine/types/fetch-static-state.ts +++ b/packages/headless/src/app/ssr-engine/types/fetch-static-state.ts @@ -8,7 +8,7 @@ import { EngineStaticState, OptionsTuple, } from './common.js'; -import {FromBuildResult} from './from-build-result.js'; +import {FromBuildResult, FromBuildResultWithList} from './from-build-result.js'; export type FetchStaticStateOptions = {}; @@ -59,7 +59,7 @@ export type FetchStaticStateWithList< > ): Promise>; - fromBuildResult: FromBuildResult< + fromBuildResult: FromBuildResultWithList< TEngine, TControllers, FetchStaticStateOptions, diff --git a/packages/headless/src/app/ssr-engine/types/from-build-result.ts b/packages/headless/src/app/ssr-engine/types/from-build-result.ts index 5851651b59b..a74ae82617a 100644 --- a/packages/headless/src/app/ssr-engine/types/from-build-result.ts +++ b/packages/headless/src/app/ssr-engine/types/from-build-result.ts @@ -18,3 +18,15 @@ export interface FromBuildResult< options: FromBuildResultOptions & TOptions ): Promise; } + +export interface FromBuildResultWithList< + TEngine extends CoreEngine | CoreEngineNext, + TControllers extends ControllersMap, + TOptions, + TReturn, +> { + ( + c: (keyof TControllers)[], + options: FromBuildResultOptions & TOptions + ): Promise; +} diff --git a/packages/headless/src/ssr-commerce.index.ts b/packages/headless/src/ssr-commerce.index.ts index 461befbd6f7..bb18cee938a 100644 --- a/packages/headless/src/ssr-commerce.index.ts +++ b/packages/headless/src/ssr-commerce.index.ts @@ -98,7 +98,7 @@ export type { InferControllerStaticStateMapFromDefinitionsWithSolutionType, InferControllerPropsMapFromDefinitions, } from './app/commerce-ssr-engine/types/common.js'; -export type {Build} from './app/ssr-engine/types/build.js'; +export type {Build, BuildWithList} from './app/ssr-engine/types/build.js'; export type { EngineDefinition, InferStaticState, @@ -106,6 +106,7 @@ export type { InferBuildResult, HydrateStaticState, FetchStaticState, + FetchStaticStateWithList, } from './app/commerce-ssr-engine/types/core-engine.js'; export type {LoggerOptions} from './app/logger.js'; export type {