-
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.
- Loading branch information
Yen Truong
committed
Oct 22, 2021
1 parent
9aa53ed
commit 5a3cbf6
Showing
4 changed files
with
106 additions
and
103 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
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); | ||
} |
25 changes: 2 additions & 23 deletions
25
...p/src/utils/getdisplayablefilterutils.tsx → ...-app/src/utils/displayablefilterutils.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
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