-
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.
feat(headless SSR): add define function for pagination, parameter man…
…ager, sort, summary, productView, and didYouMean (#4266) https://coveord.atlassian.net/browse/KIT-3392 DidYouMean and ParameterManager aren't in the sample yet. I created separate Jiras for those. --------- Co-authored-by: ylakhdar <[email protected]> Co-authored-by: Alex Prudhomme <[email protected]> Co-authored-by: Nico Labarre <[email protected]>
- Loading branch information
1 parent
6c7f390
commit 23b4d59
Showing
20 changed files
with
694 additions
and
70 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...eadless/src/controllers/commerce/core/pagination/headless-core-commerce-pagination.ssr.ts
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,36 @@ | ||
import {ensureAtLeastOneSolutionType} from '../../../../app/commerce-ssr-engine/common'; | ||
import { | ||
ControllerDefinitionOption, | ||
SolutionType, | ||
SubControllerDefinitionWithoutProps, | ||
} from '../../../../app/commerce-ssr-engine/types/common'; | ||
import {buildProductListing} from '../../product-listing/headless-product-listing'; | ||
import {buildSearch} from '../../search/headless-search'; | ||
import { | ||
Pagination, | ||
PaginationProps, | ||
PaginationState, | ||
} from './headless-core-commerce-pagination'; | ||
|
||
export type {Pagination, PaginationProps, PaginationState}; | ||
|
||
/** | ||
* Defines a `Pagination` controller instance. | ||
* | ||
* @param props - The configurable `Pagination` properties. | ||
* @returns The `Pagination` controller definition. | ||
* | ||
* @internal | ||
*/ | ||
export function definePagination< | ||
TOptions extends ControllerDefinitionOption | undefined, | ||
>(props?: PaginationProps, options?: TOptions) { | ||
ensureAtLeastOneSolutionType(options); | ||
return { | ||
...options, | ||
build: (engine, solutionType) => | ||
solutionType === SolutionType.listing | ||
? buildProductListing(engine).pagination(props) | ||
: buildSearch(engine).pagination(props), | ||
} as SubControllerDefinitionWithoutProps<Pagination, TOptions>; | ||
} |
105 changes: 105 additions & 0 deletions
105
...ss/src/controllers/commerce/core/parameter-manager/headless-core-parameter-manager.ssr.ts
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,105 @@ | ||
import {ensureAtLeastOneSolutionType} from '../../../../app/commerce-ssr-engine/common'; | ||
import { | ||
ControllerDefinitionOption, | ||
SolutionType, | ||
SubControllerDefinitionWithProps, | ||
} from '../../../../app/commerce-ssr-engine/types/common'; | ||
import {CoreEngineNext} from '../../../../app/engine'; | ||
import {commerceFacetSetReducer as commerceFacetSet} from '../../../../features/commerce/facets/facet-set/facet-set-slice'; | ||
import {manualNumericFacetReducer as manualNumericFacetSet} from '../../../../features/commerce/facets/numeric-facet/manual-numeric-facet-slice'; | ||
import {paginationReducer as commercePagination} from '../../../../features/commerce/pagination/pagination-slice'; | ||
import {Parameters} from '../../../../features/commerce/parameters/parameters-actions'; | ||
import {ProductListingParameters} from '../../../../features/commerce/product-listing-parameters/product-listing-parameters-actions'; | ||
import {queryReducer as query} from '../../../../features/commerce/query/query-slice'; | ||
import {CommerceSearchParameters} from '../../../../features/commerce/search-parameters/search-parameters-actions'; | ||
import {sortReducer as commerceSort} from '../../../../features/commerce/sort/sort-slice'; | ||
import {facetOrderReducer as facetOrder} from '../../../../features/facets/facet-order/facet-order-slice'; | ||
import {querySetReducer as querySet} from '../../../../features/query-set/query-set-slice'; | ||
import {loadReducerError} from '../../../../utils/errors'; | ||
import {buildProductListing} from '../../product-listing/headless-product-listing'; | ||
import {buildSearch} from '../../search/headless-search'; | ||
import { | ||
ParameterManager, | ||
ParameterManagerProps, | ||
ParameterManagerState, | ||
} from './headless-core-parameter-manager'; | ||
|
||
export type { | ||
ParameterManager, | ||
ParameterManagerProps, | ||
ParameterManagerState, | ||
Parameters, | ||
ProductListingParameters, | ||
CommerceSearchParameters, | ||
}; | ||
|
||
/** | ||
* Defines a `ParameterManager` controller instance. | ||
* | ||
* @returns The `ParameterManager` controller definition. | ||
* | ||
* @internal | ||
*/ | ||
export function defineParameterManager< | ||
TOptions extends ControllerDefinitionOption | undefined, | ||
>(options?: TOptions) { | ||
ensureAtLeastOneSolutionType(options); | ||
return { | ||
...options, | ||
buildWithProps: (engine, props, solutionType) => { | ||
if (solutionType === SolutionType.listing) { | ||
if (!loadCommerceProductListingParameterReducers(engine)) { | ||
throw loadReducerError; | ||
} | ||
return buildProductListing(engine).parameterManager(props); | ||
} else { | ||
if (!loadCommerceSearchParameterReducers(engine)) { | ||
throw loadReducerError; | ||
} | ||
return buildSearch(engine).parameterManager(props); | ||
} | ||
}, | ||
} as SubControllerDefinitionWithProps< | ||
ParameterManager<MappedParameterTypes<typeof options>>, | ||
TOptions, | ||
ParameterManagerProps<MappedParameterTypes<typeof options>> | ||
>; | ||
} | ||
|
||
type MappedParameterTypes< | ||
TOptions extends ControllerDefinitionOption | undefined, | ||
> = TOptions extends {listing: true; search: true} | undefined | ||
? ProductListingParameters | CommerceSearchParameters | ||
: TOptions extends {listing: true; search: false} | ||
? ProductListingParameters | ||
: TOptions extends {listing: false; search: true} | ||
? CommerceSearchParameters | ||
: never; | ||
|
||
function loadCommerceCommonParameterReducers( | ||
engine: CoreEngineNext | ||
): engine is CoreEngineNext<ParameterManager<Parameters>> { | ||
engine.addReducers({ | ||
commerceFacetSet, | ||
commerceSort, | ||
commercePagination, | ||
facetOrder, | ||
manualNumericFacetSet, | ||
}); | ||
return true; | ||
} | ||
|
||
function loadCommerceSearchParameterReducers( | ||
engine: CoreEngineNext | ||
): engine is CoreEngineNext<ParameterManager<CommerceSearchParameters>> { | ||
loadCommerceCommonParameterReducers(engine); | ||
engine.addReducers({query, querySet}); | ||
return true; | ||
} | ||
|
||
function loadCommerceProductListingParameterReducers( | ||
engine: CoreEngineNext | ||
): engine is CoreEngineNext<ParameterManager<ProductListingParameters>> { | ||
loadCommerceCommonParameterReducers(engine); | ||
return true; | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/headless/src/controllers/commerce/core/sort/headless-core-commerce-sort.ssr.ts
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,32 @@ | ||
import {ensureAtLeastOneSolutionType} from '../../../../app/commerce-ssr-engine/common'; | ||
import { | ||
ControllerDefinitionOption, | ||
SolutionType, | ||
SubControllerDefinitionWithoutProps, | ||
} from '../../../../app/commerce-ssr-engine/types/common'; | ||
import {buildProductListing} from '../../product-listing/headless-product-listing'; | ||
import {buildSearch} from '../../search/headless-search'; | ||
import {Sort, SortProps, SortState} from './headless-core-commerce-sort'; | ||
|
||
export type {Sort, SortProps, SortState}; | ||
|
||
/** | ||
* Defines a `Sort` controller instance. | ||
* | ||
* @param props - The configurable `Sort` properties. | ||
* @returns The `Sort` controller definition. | ||
* | ||
* @internal | ||
*/ | ||
export function defineSort< | ||
TOptions extends ControllerDefinitionOption | undefined, | ||
>(props?: SortProps, options?: TOptions) { | ||
ensureAtLeastOneSolutionType(options); | ||
return { | ||
...options, | ||
build: (engine, solutionType) => | ||
solutionType === SolutionType.listing | ||
? buildProductListing(engine).sort(props) | ||
: buildSearch(engine).sort(props), | ||
} as SubControllerDefinitionWithoutProps<Sort, TOptions>; | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/headless/src/controllers/commerce/core/summary/headless-core-summary.ssr.ts
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,40 @@ | ||
import {ensureAtLeastOneSolutionType} from '../../../../app/commerce-ssr-engine/common'; | ||
import { | ||
ControllerDefinitionOption, | ||
SolutionType, | ||
SubControllerDefinitionWithoutProps, | ||
} from '../../../../app/commerce-ssr-engine/types/common'; | ||
import {buildProductListing} from '../../product-listing/headless-product-listing'; | ||
import {ProductListingSummaryState} from '../../product-listing/summary/headless-product-listing-summary'; | ||
import {RecommendationsSummaryState} from '../../recommendations/summary/headless-recommendations-summary'; | ||
import {buildSearch} from '../../search/headless-search'; | ||
import {SearchSummaryState} from '../../search/summary/headless-search-summary'; | ||
import {Summary, SummaryState} from './headless-core-summary'; | ||
|
||
export type { | ||
Summary, | ||
ProductListingSummaryState, | ||
RecommendationsSummaryState, | ||
SearchSummaryState, | ||
SummaryState, | ||
}; | ||
|
||
/** | ||
* Defines a `Summary` controller instance. | ||
* | ||
* @returns The `Summary` controller definition. | ||
* | ||
* @internal | ||
*/ | ||
export function defineSummary< | ||
TOptions extends ControllerDefinitionOption | undefined, | ||
>(options?: TOptions) { | ||
ensureAtLeastOneSolutionType(options); | ||
return { | ||
...options, | ||
build: (engine, solutionType) => | ||
solutionType === SolutionType.listing | ||
? buildProductListing(engine).summary() | ||
: buildSearch(engine).summary(), | ||
} as SubControllerDefinitionWithoutProps<Summary, TOptions>; | ||
} |
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
43 changes: 43 additions & 0 deletions
43
packages/headless/src/controllers/commerce/product-view/headless-product-view.ssr.ts
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,43 @@ | ||
import {CommerceEngine} from '../../../app/commerce-engine/commerce-engine'; | ||
import {SharedControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common'; | ||
import { | ||
buildController, | ||
Controller, | ||
} from '../../controller/headless-controller'; | ||
import { | ||
buildProductView, | ||
ProductView as BaseProductView, | ||
} from './headless-product-view'; | ||
|
||
export interface ProductViewDefinition | ||
extends SharedControllerDefinitionWithoutProps<ProductView> {} | ||
|
||
/** | ||
* Defines a `ProductView` controller instance. | ||
* | ||
* This controller is stateless and does not implement a `subscribe` method, | ||
* making it simpler but different from other controllers in the system. | ||
* Its sole purpose is to log an `ec.productView` event. | ||
* | ||
* @returns The `ProductView` controller definition. | ||
* | ||
* @internal | ||
*/ | ||
export function defineProductView(): ProductViewDefinition { | ||
return { | ||
listing: true, | ||
search: true, | ||
build: (engine) => buildSSRProductView(engine), | ||
}; | ||
} | ||
|
||
export interface ProductView extends BaseProductView, Controller {} | ||
|
||
function buildSSRProductView(engine: CommerceEngine): ProductView { | ||
const controller = buildController(engine); | ||
const productView = buildProductView(engine); | ||
return { | ||
...controller, | ||
...productView, | ||
}; | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/headless/src/controllers/commerce/search/did-you-mean/headless-did-you-mean.ssr.ts
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,19 @@ | ||
import {SearchOnlyControllerDefinitionWithoutProps} from '../../../../app/commerce-ssr-engine/types/common'; | ||
import {buildSearch} from '../headless-search'; | ||
import {DidYouMean, DidYouMeanState} from './headless-did-you-mean'; | ||
|
||
export type {DidYouMean, DidYouMeanState}; | ||
|
||
/** | ||
* Defines a `DidYouMean` controller instance. | ||
* | ||
* @returns The `DidYouMean` controller definition. | ||
* | ||
* @internal | ||
* */ | ||
export function defineDidYouMean(): SearchOnlyControllerDefinitionWithoutProps<DidYouMean> { | ||
return { | ||
search: true, | ||
build: (engine) => buildSearch(engine).didYouMean(), | ||
}; | ||
} |
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
1 change: 1 addition & 0 deletions
1
packages/samples/headless-ssr-commerce/app/_components/did-you-mean.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 @@ | ||
// TODO // TODO KIT-3463: implement did you mean in sample |
Oops, something went wrong.