Skip to content

Commit

Permalink
CDP #23 - Updating FacetStateContextProvider to allow include/exclude…
Browse files Browse the repository at this point in the history
… props
  • Loading branch information
dleadbetter committed Jan 8, 2025
1 parent 409a179 commit ef4d8a7
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions packages/core-data/src/components/FacetStateContextProvider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import React, {
useCallback,
useEffect,
useMemo,
useState,
Expand Down Expand Up @@ -46,11 +47,21 @@ type Props = {
*/
children: Node,

/**
* A list of facets to exclude.
*/
exclude?: Array<string>,

/**
* Search host URI.
*/
host: string,

/**
* A list of facets to include.
*/
include?: Array<string>,

/**
* Search index name.
*/
Expand Down Expand Up @@ -86,19 +97,38 @@ const TYPE_INT_ARRAY = 'int64[]';
const FacetStateContextProvider = (props: Props) => {
const [facets, setFacets] = useState<Array<string>>([]);

/**
* Returns true if the passed field should be included as a facet.
*
* @type {(function(*): (*|boolean))|*}
*/
const isValid = useCallback((field) => {
if (!_.isEmpty(props.include)) {
return _.contains(props.include, field.name);
}

if (!_.isEmpty(props.exclude)) {
return _.contains(props.exclude, field.name);
}

return true;
}, [props.exclude, props.include]);

/**
* Memo-ize the refinement list facets.
*/
const listFacets = useMemo(() => (
_.filter(facets, (field: any) => field.facet && field.type !== TYPE_AUTO && field.type !== TYPE_INT_ARRAY)
), [facets]);
_.filter(facets, (field: any) => isValid(field)
&& field.facet && field.type !== TYPE_AUTO
&& field.type !== TYPE_INT_ARRAY)
), [facets, isValid]);

/**
* Memo-ize the range facets.
*/
const rangeFacets = useMemo(() => (
_.filter(facets, (field: any) => field.facet && field.type === TYPE_INT_ARRAY)
), [facets]);
_.filter(facets, (field: any) => isValid(field) && field.facet && field.type === TYPE_INT_ARRAY)
), [facets, isValid]);

/**
* Backwards compatability for consumers using the "attributes" return value.
Expand Down

0 comments on commit ef4d8a7

Please sign in to comment.