Skip to content

Commit

Permalink
add alphanumericNatural sort criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
fpbrault committed Oct 28, 2024
1 parent 1143cae commit 284d673
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/atomic/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand Down Expand Up @@ -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;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
/**
Expand Down Expand Up @@ -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 (
<FacetGuard
enabled={this.facetState.enabled}
Expand Down
2 changes: 2 additions & 0 deletions packages/headless/src/features/facets/facet-api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export interface SortCriteria<
| 'score'
| 'alphanumeric'
| 'alphanumericDescending'
| 'alphanumericNatural'
| 'alphanumericNaturalDescending'
| 'ascending'
| 'descending'
| 'occurrences'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ export const facetSortCriteria: FacetSortCriterion[] = [
'alphanumericDescending',
'occurrences',
'automatic',
'alphanumericNatural',
'alphanumericNaturalDescending',
];

export type FacetSortCriterion =
| 'score'
| 'alphanumeric'
| 'alphanumericDescending'
| 'occurrences'
| 'automatic';
| 'automatic'
| 'alphanumericNatural'
| 'alphanumericNaturalDescending';

export type FacetSortOrder = 'ascending' | 'descending';

Expand Down
15 changes: 15 additions & 0 deletions packages/headless/src/features/search/search-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ describe('search request', () => {
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({
Expand Down
23 changes: 17 additions & 6 deletions packages/headless/src/features/search/search-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import {mapSearchRequest} from './search-mappings.js';
type StateNeededBySearchRequest = ConfigurationSection &
Partial<SearchAppState>;

type SortCriteria = {
type: string;
order: string;
};

export const buildSearchRequest = async (
state: StateNeededBySearchRequest,
navigatorContext: NavigatorContext,
Expand Down Expand Up @@ -147,20 +152,26 @@ function getAllFacets(state: StateNeededBySearchRequest) {
];
}

const sortCriteriaMap: Record<string, SortCriteria> = {
alphanumericDescending: {type: 'alphanumeric', order: 'descending'},
alphanumericNaturalDescending: {
type: 'alphanumericNatural',
order: 'descending',
},
};

function getSpecificFacetRequests<T extends FacetSetState>(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;
});
}
Expand Down
23 changes: 17 additions & 6 deletions packages/headless/src/utils/facet-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {ConfigurationSection} from '../state/state-sections.js';
type StateNeededBySearchRequest = ConfigurationSection &
Partial<SearchAppState>;

type SortCriteria = {
type: string;
order: string;
};

export function sortFacets<T extends {facetId: string}>(
facets: T[],
sortOrder: string[]
Expand Down Expand Up @@ -40,20 +45,26 @@ function getRangeFacetRequests<T extends RangeFacetSetState>(state: T) {
});
}

const sortCriteriaMap: Record<string, SortCriteria> = {
alphanumericDescending: {type: 'alphanumeric', order: 'descending'},
alphanumericNaturalDescending: {
type: 'alphanumericNatural',
order: 'descending',
},
};

function getSpecificFacetRequests<T extends FacetSetState>(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;
});
}
Expand Down

0 comments on commit 284d673

Please sign in to comment.