Skip to content

Commit

Permalink
clean up and add to headless-react
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprudhomme committed Nov 15, 2024
1 parent f82b8a3 commit f0ac743
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 102 deletions.
11 changes: 11 additions & 0 deletions packages/headless-react/src/ssr-commerce/commerce-engine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ export function defineCommerceEngine<
>;
type ListingContext = ContextStateType<SolutionType.listing>;
type SearchContext = ContextStateType<SolutionType.search>;
type RecommendationContext = ContextStateType<SolutionType.recommendation>;
type StandaloneContext = ContextStateType<SolutionType.standalone>;

const {
listingEngineDefinition,
searchEngineDefinition,
recommendationEngineDefinition,
standaloneEngineDefinition,
} = defineBaseCommerceEngine({...options});
return {
Expand All @@ -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(
Expand Down
105 changes: 7 additions & 98 deletions packages/headless/src/app/commerce-engine/commerce-engine.ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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: {
Expand All @@ -559,7 +468,7 @@ export function defineCommerceEngine<
>,
recommendationEngineDefinition: {
build: recommendationBuildFactory(),
fetchStaticState: recommendationFetchStaticStateFactory3(),
fetchStaticState: recommendationFetchStaticStateFactory(),
hydrateStaticState: hydrateStaticStateFactory(
SolutionType.recommendation
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down Expand Up @@ -59,7 +59,7 @@ export type FetchStaticStateWithList<
>
): Promise<EngineStaticState<TSearchAction, TControllersStaticState>>;

fromBuildResult: FromBuildResult<
fromBuildResult: FromBuildResultWithList<
TEngine,
TControllers,
FetchStaticStateOptions,
Expand Down
12 changes: 12 additions & 0 deletions packages/headless/src/app/ssr-engine/types/from-build-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ export interface FromBuildResult<
options: FromBuildResultOptions<TEngine, TControllers> & TOptions
): Promise<TReturn>;
}

export interface FromBuildResultWithList<
TEngine extends CoreEngine | CoreEngineNext,
TControllers extends ControllersMap,
TOptions,
TReturn,
> {
(
c: (keyof TControllers)[],
options: FromBuildResultOptions<TEngine, TControllers> & TOptions
): Promise<TReturn>;
}
3 changes: 2 additions & 1 deletion packages/headless/src/ssr-commerce.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ 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,
InferHydratedState,
InferBuildResult,
HydrateStaticState,
FetchStaticState,
FetchStaticStateWithList,
} from './app/commerce-ssr-engine/types/core-engine.js';
export type {LoggerOptions} from './app/logger.js';
export type {
Expand Down

0 comments on commit f0ac743

Please sign in to comment.