Skip to content

Commit

Permalink
Add special getActiveFilters method
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Jan 16, 2023
1 parent e6495bd commit 155488b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
20 changes: 3 additions & 17 deletions searchlib/components/FilterList/ActiveFilterList.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { Segment, Accordion, Button, Icon } from 'semantic-ui-react';
import { useAppConfig, useSearchContext } from '@eeacms/search/lib/hocs';
import { isFilterValueDefaultValue } from '@eeacms/search/lib/search/helpers';
import { isLandingPageAtom } from '@eeacms/search/state';
import { useAtom } from 'jotai';

Expand All @@ -11,23 +10,10 @@ const ActiveFilterList = (props) => {
const { filters, clearFilters, setFilter, removeFilter } = useSearchContext();
const [isOpened, setIsOpened] = React.useState(true);
const [isLandingPage] = useAtom(isLandingPageAtom);
const { appConfig } = useAppConfig();
const { facets = [] } = appConfig;
const { appConfig, registry } = useAppConfig();

const filterableFacets = facets.filter(
(f) =>
f.isFilter ||
(typeof f.showInFacetsList !== 'undefined' ? f.showInFacetsList : true),
);
const facetNames = filterableFacets.map((f) => f.field);

const filterNames = filters
.filter((f) => facetNames.includes(f.field))
.map((f) => f.field);

const activeFilters = filters
.filter((f) => filterNames.includes(f.field))
.filter((f) => !isFilterValueDefaultValue(f, appConfig));
const factoryName = appConfig.getActiveFilters;
const activeFilters = registry.resolve[factoryName](filters, appConfig);

return !isLandingPage && activeFilters.length ? (
<Segment className="active-filter-list">
Expand Down
1 change: 1 addition & 0 deletions searchlib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './lib/facets';
export * from './lib/utils';
export * from './lib/hocs';
export * from './lib/search';
export * from './lib/search/helpers';
export * from './lib/models';
export * from './lib/serialize';
export * from './state';
Expand Down
19 changes: 19 additions & 0 deletions searchlib/lib/search/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,22 @@ export const checkInteracted = ({ searchContext, appConfig }) => {

return wasInteracted;
};

export function getActiveFilters(filters, appConfig) {
const { facets = [] } = appConfig;
const filterableFacets = facets.filter(
(f) =>
f.isFilter ||
(typeof f.showInFacetsList !== 'undefined' ? f.showInFacetsList : true),
);
const facetNames = filterableFacets.map((f) => f.field);
const filterNames = filters
.filter((f) => facetNames.includes(f.field))
.map((f) => f.field);

const activeFilters = filters
.filter((f) => filterNames.includes(f.field))
.filter((f) => !isFilterValueDefaultValue(f, appConfig));

return activeFilters;
}
20 changes: 10 additions & 10 deletions searchlib/lib/search/query/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ export const buildHighlight = (searchTerm, config) => {

return searchTerm && config.highlight?.fields
? {
highlight: {
...config.highlight.queryParams,
fields: Object.assign(
{},
...config.highlight.fields.map((name) => ({
[name]: _highlight(searchTerm, name),
})),
),
},
}
highlight: {
...config.highlight.queryParams,
fields: Object.assign(
{},
...config.highlight.fields.map((name) => ({
[name]: _highlight(searchTerm, name),
})),
),
},
}
: {};
};
7 changes: 3 additions & 4 deletions searchlib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import {
getValueFacet,
getRangeFacet,
getDateRangeFilter,
// getDateRangeFacet,
getHistogramFilter,
getBooleanFilter,
getBooleanFacet,
Expand All @@ -58,8 +57,6 @@ import {
buildHistogramFacetAggregationRequest,
buildRangeFacetAggregationRequest,
buildMLTFilter,
// getHistogramFacet,
// getMLTValue,
highlightQueryBuilder,
buildBooleanFacetRequest,
} from '@eeacms/search/lib/search';
Expand All @@ -68,7 +65,7 @@ import {
addQAParams,
extractAnswers,
} from '@eeacms/search/components/AnswerBox';
// import { valueToString } from './lib/utils';
import { getActiveFilters } from '@eeacms/search/lib/search/helpers';

const config = {
resolve: {
Expand Down Expand Up @@ -236,6 +233,7 @@ const config = {

ResultModel,
highlightQueryBuilder,
defaultGetActiveFilters: getActiveFilters,
},

searchui: {
Expand Down Expand Up @@ -278,6 +276,7 @@ const config = {
promptQueryInterval: 20000,
alwaysSearchOnInitialLoad: false, // used in elastic search driver
showFilters: true, // enables the filters interface, to allow falling back to just a simple results list
getActiveFilters: 'defaultGetActiveFilters',

// highlight: {
// queryParams: {
Expand Down

0 comments on commit 155488b

Please sign in to comment.