diff --git a/packages/atomic/src/components.d.ts b/packages/atomic/src/components.d.ts index a2ac7066608..eb339734195 100644 --- a/packages/atomic/src/components.d.ts +++ b/packages/atomic/src/components.d.ts @@ -884,7 +884,7 @@ export namespace Components { */ "resultsMustMatch": FacetResultsMustMatch; /** - * The sort criterion to apply to the returned facet values. Possible values are 'score', 'alphanumeric', 'alphanumericDescending', 'occurrences', and 'automatic'. + * The sort criterion to apply to the returned facet values. Possible values are 'score', 'alphanumeric', 'alphanumericDescending', 'occurrences', alphanumericNatural', 'alphanumericNaturalDescending' and 'automatic'. */ "sortCriteria": FacetSortCriterion; /** @@ -6934,7 +6934,7 @@ declare namespace LocalJSX { */ "resultsMustMatch"?: FacetResultsMustMatch; /** - * The sort criterion to apply to the returned facet values. Possible values are 'score', 'alphanumeric', 'alphanumericDescending', 'occurrences', and 'automatic'. + * The sort criterion to apply to the returned facet values. Possible values are 'score', 'alphanumeric', 'alphanumericDescending', 'occurrences', alphanumericNatural', 'alphanumericNaturalDescending' and 'automatic'. */ "sortCriteria"?: FacetSortCriterion; /** diff --git a/packages/atomic/src/components/search/facets/atomic-facet/atomic-facet.tsx b/packages/atomic/src/components/search/facets/atomic-facet/atomic-facet.tsx index eea2caa2292..2df9415ae7b 100644 --- a/packages/atomic/src/components/search/facets/atomic-facet/atomic-facet.tsx +++ b/packages/atomic/src/components/search/facets/atomic-facet/atomic-facet.tsx @@ -174,7 +174,7 @@ export class AtomicFacet implements InitializableComponent { @Prop({reflect: true}) public withSearch = true; /** * The sort criterion to apply to the returned facet values. - * Possible values are 'score', 'alphanumeric', 'alphanumericDescending', 'occurrences', and 'automatic'. + * Possible values are 'score', 'alphanumeric', 'alphanumericDescending', 'occurrences', alphanumericNatural', 'alphanumericNaturalDescending' and 'automatic'. */ @Prop({reflect: true}) public sortCriteria: FacetSortCriterion = 'automatic'; /** @@ -334,6 +334,13 @@ export class AtomicFacet implements InitializableComponent { } public render() { + console.log( + this.facetId, + this.facetState.enabled, + this.searchStatusState.hasError, + this.searchStatusState.firstSearchExecuted, + this.facetState.values.length + ); return ( { type: 'alphanumeric', }); }); + it('#searchRequest returns the facet state alphanumericNaturalDescending #sortCriteria', async () => { + const request = buildMockFacetRequest({ + field: 'objecttype', + sortCriteria: 'alphanumericNaturalDescending', + }); + state.facetSet[1] = buildMockFacetSlice({request}); + const {facets} = ( + await buildSearchRequest(state, buildMockNavigatorContextProvider()()) + ).request; + + expect(facets?.map((f) => f.sortCriteria)).toContainEqual({ + order: 'descending', + type: 'alphanumericNatural', + }); + }); it('#searchRequest returns the facets in the state #numericFacetSet', async () => { const request = buildMockNumericFacetRequest({ diff --git a/packages/headless/src/features/search/search-request.ts b/packages/headless/src/features/search/search-request.ts index 5aab6993014..afffd1a35e2 100644 --- a/packages/headless/src/features/search/search-request.ts +++ b/packages/headless/src/features/search/search-request.ts @@ -17,6 +17,11 @@ import {mapSearchRequest} from './search-mappings.js'; type StateNeededBySearchRequest = ConfigurationSection & Partial; +type SortCriteria = { + type: string; + order: string; +}; + export const buildSearchRequest = async ( state: StateNeededBySearchRequest, navigatorContext: NavigatorContext, @@ -147,20 +152,26 @@ function getAllFacets(state: StateNeededBySearchRequest) { ]; } +const sortCriteriaMap: Record = { + alphanumericDescending: {type: 'alphanumeric', order: 'descending'}, + alphanumericNaturalDescending: { + type: 'alphanumericNatural', + order: 'descending', + }, +}; + function getSpecificFacetRequests(state: T) { return getFacetRequests(state).map((request) => { /* The Search API does not support 'alphanumericDescending' as a string value and instead relies on a new sort mechanism to specify sort order. At the moment, this is only supported for alphanumeric sorting, but will likely transition to this pattern for other types in the future. */ - if (request.sortCriteria === 'alphanumericDescending') { + const sortCriteria = + sortCriteriaMap[request.sortCriteria as keyof typeof sortCriteriaMap]; + if (sortCriteria) { return { ...request, - sortCriteria: { - type: 'alphanumeric', - order: 'descending', - }, + sortCriteria, }; } - return request; }); } diff --git a/packages/headless/src/utils/facet-utils.ts b/packages/headless/src/utils/facet-utils.ts index 4cd38279441..0194c8a821d 100644 --- a/packages/headless/src/utils/facet-utils.ts +++ b/packages/headless/src/utils/facet-utils.ts @@ -8,6 +8,11 @@ import {ConfigurationSection} from '../state/state-sections.js'; type StateNeededBySearchRequest = ConfigurationSection & Partial; +type SortCriteria = { + type: string; + order: string; +}; + export function sortFacets( facets: T[], sortOrder: string[] @@ -40,20 +45,26 @@ function getRangeFacetRequests(state: T) { }); } +const sortCriteriaMap: Record = { + alphanumericDescending: {type: 'alphanumeric', order: 'descending'}, + alphanumericNaturalDescending: { + type: 'alphanumericNatural', + order: 'descending', + }, +}; + function getSpecificFacetRequests(state: T) { return getFacetRequests(state).map((request) => { /* The Search API does not support 'alphanumericDescending' as a string value and instead relies on a new sort mechanism to specify sort order. At the moment, this is only supported for alphanumeric sorting, but will likely transition to this pattern for other types in the future. */ - if (request.sortCriteria === 'alphanumericDescending') { + const sortCriteria = + sortCriteriaMap[request.sortCriteria as keyof typeof sortCriteriaMap]; + if (sortCriteria) { return { ...request, - sortCriteria: { - type: 'alphanumeric', - order: 'descending', - }, + sortCriteria, }; } - return request; }); }