Skip to content

Commit

Permalink
clean controller build condition
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lakhdar committed Nov 18, 2024
1 parent f342da5 commit 677fd45
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 27 deletions.
35 changes: 9 additions & 26 deletions packages/headless/src/app/commerce-ssr-engine/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,17 @@ export function buildControllerDefinitions<
TSolutionType
> {
const controllerMap = mapObject(definitionsMap, (definition, key) => {
const unavailableInSearchSolutionType =
'search' in definition &&
definition['search'] === false &&
solutionType === SolutionType['search'];

const unavailableInListingSolutionType =
'listing' in definition &&
definition['listing'] === false &&
solutionType === SolutionType['listing'];

const unavailableInStandaloneSolutionType =
solutionType === SolutionType['standalone'] && 'standalone' in definition
? definition['standalone'] === false
: false;

const unavailabeInRecs =
// TODO: use this disjunction pattern for all other conditions
(solutionType === SolutionType['recommendation'] &&
!('recommendation' in definition)) ||
('recommendation' in definition &&
definition['recommendation'] === false &&
solutionType === SolutionType['recommendation']);
const unavailableInSolutionType = (type: SolutionType) =>
(!(type in definition) && solutionType === SolutionType[type]) ||
(type in definition &&
definition[type as keyof typeof definition] === false &&
solutionType === SolutionType[type]);

if (
unavailableInSearchSolutionType ||
unavailableInListingSolutionType ||
unavailableInStandaloneSolutionType ||
unavailabeInRecs
unavailableInSolutionType(SolutionType.search) ||
unavailableInSolutionType(SolutionType.listing) ||
unavailableInSolutionType(SolutionType.standalone) ||
unavailableInSolutionType(SolutionType.recommendation)
) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function filterRecommendationControllers<
TEngine extends CoreEngine | CoreEngineNext,
TControllerDefinitions extends ControllerDefinitionsMap<TEngine, Controller>,
>(
controllers: Record<string, Controller>, // TODO: or InferControllersMapFromDefinition<TControllerDefinitions, SolutionType>
controllers: Record<string, Controller>,
controllerDefinitions: TControllerDefinitions,
logger: Logger
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export function defineBreadcrumbManager<
>(options?: TOptions) {
ensureAtLeastOneSolutionType(options);
return {
listing: true,
search: true,
...options,
build: (engine, solutionType) =>
solutionType === SolutionType.listing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export function defineFacetGenerator<
>(options?: TOptions) {
ensureAtLeastOneSolutionType(options);
return {
listing: true,
search: true,
...options,
build: (engine, solutionType) =>
buildFacetGenerator(engine, {props: {solutionType: solutionType!}}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export function definePagination<
>(props?: PaginationProps & TOptions) {
ensureAtLeastOneSolutionType(props);
return {
listing: true,
search: true,
...props,
build: (engine, solutionType) =>
solutionType === SolutionType.listing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export function defineParameterManager<
>(options?: TOptions) {
ensureAtLeastOneSolutionType(options);
return {
listing: true,
search: true,
...options,
buildWithProps: (engine, props, solutionType) => {
if (solutionType === SolutionType.listing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export function defineSort<
>(props?: SortProps & TOptions) {
ensureAtLeastOneSolutionType(props);
return {
listing: true,
search: true,
...props,
build: (engine, solutionType) =>
solutionType === SolutionType.listing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export function defineQuerySummary<
>(options?: TOptions) {
ensureAtLeastOneSolutionType(options);
return {
listing: true,
search: true,
...options,
build: (engine, solutionType) =>
solutionType === SolutionType.listing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export function defineSummary<
>(options?: TOptions) {
ensureAtLeastOneSolutionType(options);
return {
listing: true,
search: true,
...options,
build: (engine, solutionType) =>
solutionType === SolutionType.listing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export function defineProductList<
>(options?: TOptions) {
ensureAtLeastOneSolutionType(options);
return {
listing: true,
search: true,
...options,
build: (engine, solutionType) =>
solutionType === SolutionType.listing
Expand Down

0 comments on commit 677fd45

Please sign in to comment.