-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- update DisplayableFilter interface to include additional fields to help identify and unify the different applied filter types (static, nlp, facet) - update functions in filterUtils to support applied facets J=1656 TEST=manual - in vertical page, input query 'virginia', and select some static filters and facet options, see that all 3 label types are displayed
- Loading branch information
Showing
6 changed files
with
168 additions
and
83 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { Filter } from '@yext/answers-core'; | ||
|
||
export interface DisplayableFilter { | ||
filterType: 'NLP_FILTER' | 'STATIC_FILTER' | 'FACET', | ||
filter: Filter, | ||
filterGroupLabel: string, | ||
filterLabel: string | ||
groupLabel: string, | ||
label: string | ||
} |
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,77 @@ | ||
import { AppliedQueryFilter } from "@yext/answers-core"; | ||
import { FiltersState } from "@yext/answers-headless/lib/esm/models/slices/filters"; | ||
import { DisplayableFilter } from "../models/displayableFilter"; | ||
import { GroupedFilters } from "../models/groupedFilters"; | ||
import { mapArrayToObject } from "./arrayutils"; | ||
import { isDuplicateFilter, flattenFilters } from './filterutils'; | ||
import { | ||
getDisplayableStaticFilters, | ||
getDisplayableAppliedFacets, | ||
getDisplayableNlpFilters | ||
} from "./displayablefilterutils"; | ||
|
||
/** | ||
* Returns a new list of nlp filters with duplicates of other filters and | ||
* filter listed in hiddenFields removed from the given nlp filter list. | ||
*/ | ||
function pruneNlpFilters ( | ||
nlpFilters: DisplayableFilter[], | ||
appliedFilters: DisplayableFilter[], | ||
hiddenFields: string[] | ||
): DisplayableFilter[] { | ||
const duplicatesRemoved = nlpFilters.filter(nlpFilter => { | ||
const isDuplicate = appliedFilters.find(appliedFilter => | ||
isDuplicateFilter(nlpFilter.filter, appliedFilter.filter) | ||
); | ||
return !isDuplicate; | ||
}); | ||
return pruneAppliedFilters(duplicatesRemoved, hiddenFields); | ||
} | ||
|
||
/** | ||
* Returns a new list of applied filters with filter on hiddenFields removed | ||
* from the given applied filter list. | ||
*/ | ||
function pruneAppliedFilters( | ||
appliedFilters: DisplayableFilter[], hiddenFields: string[]): DisplayableFilter[] { | ||
return appliedFilters.filter(appliedFilter => { | ||
return !hiddenFields.includes(appliedFilter.filter.fieldId); | ||
}); | ||
} | ||
|
||
/** | ||
* Combine all of the applied filters into a list of GroupedFilters where each contains a label and | ||
* list of filters under that same label or category. | ||
*/ | ||
function createGroupedFilters( | ||
appliedFilters: DisplayableFilter[], | ||
nlpFilters: DisplayableFilter[] | ||
): Array<GroupedFilters> { | ||
const getGroupLabel = (filter: DisplayableFilter) => filter.groupLabel; | ||
const allFilters = [...appliedFilters, ...nlpFilters]; | ||
const groupedFilters: Record<string, DisplayableFilter[]> = mapArrayToObject(allFilters, getGroupLabel); | ||
return Object.keys(groupedFilters).map(label => ({ | ||
label: label, | ||
filters: groupedFilters[label] | ||
})); | ||
} | ||
|
||
/** | ||
* Process all applied filter types (facets, static filters, and nlp filters) by removing | ||
* duplicates and specified hidden fields, and grouped the applied filters into categories. | ||
*/ | ||
export function getGroupedAppliedFilters( | ||
appliedFiltersState: FiltersState, | ||
nlpFilters: AppliedQueryFilter[], | ||
hiddenFields: string[] | ||
): Array<GroupedFilters> { | ||
const displayableStaticFilters = getDisplayableStaticFilters(flattenFilters(appliedFiltersState?.static)); | ||
const displayableFacets = getDisplayableAppliedFacets(appliedFiltersState?.facets); | ||
const displayableNlpFilters = getDisplayableNlpFilters(nlpFilters); | ||
|
||
const appliedFilters = [...displayableStaticFilters, ...displayableFacets]; | ||
const prunedAppliedFilters = pruneAppliedFilters(appliedFilters, hiddenFields); | ||
const prunedNlpFilters = pruneNlpFilters (displayableNlpFilters, prunedAppliedFilters, hiddenFields); | ||
|
||
return createGroupedFilters(appliedFilters, prunedNlpFilters); | ||
} |
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,59 @@ | ||
import { AppliedQueryFilter, Filter, DisplayableFacet } from '@yext/answers-core'; | ||
import { DisplayableFilter } from '../models/displayableFilter'; | ||
import { getFilterDisplayValue } from './filterutils'; | ||
|
||
/** | ||
* Convert a list of facets to DisplayableFilter format with only selected facets returned. | ||
*/ | ||
export function getDisplayableAppliedFacets(facets: DisplayableFacet[] | undefined): DisplayableFilter[] { | ||
let appliedFacets: DisplayableFilter[] = []; | ||
facets?.forEach(facet => { | ||
facet.options.forEach(option => { | ||
if(option.selected) { | ||
appliedFacets.push({ | ||
filterType: 'FACET', | ||
filter: { | ||
fieldId: facet.fieldId, | ||
matcher: option.matcher, | ||
value: option.value | ||
}, | ||
groupLabel: facet.displayName, | ||
label: option.displayName | ||
}); | ||
} | ||
}); | ||
}); | ||
return appliedFacets; | ||
} | ||
|
||
/** | ||
* Convert a list of static filters to DisplayableFilter format. | ||
*/ | ||
export function getDisplayableStaticFilters(filters: Filter[]): DisplayableFilter[] { | ||
let appliedStaticFilters: DisplayableFilter[] = []; | ||
filters?.forEach(filter => { | ||
appliedStaticFilters.push({ | ||
filterType: 'STATIC_FILTER', | ||
filter: filter, | ||
groupLabel: filter.fieldId, | ||
label: getFilterDisplayValue(filter), | ||
}); | ||
}); | ||
return appliedStaticFilters; | ||
} | ||
|
||
/** | ||
* Convert a list of nlp filters to DisplayableFilter format. | ||
*/ | ||
export function getDisplayableNlpFilters(filters: AppliedQueryFilter[]): DisplayableFilter[] { | ||
let appliedNlpFilters: DisplayableFilter[] = []; | ||
filters?.forEach(filter => { | ||
appliedNlpFilters.push({ | ||
filterType: 'NLP_FILTER', | ||
filter: filter.filter, | ||
groupLabel: filter.displayKey, | ||
label: filter.displayValue, | ||
}); | ||
}); | ||
return appliedNlpFilters; | ||
} |
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