2019-08-31 20:51:14 -07:00
|
|
|
import { typeOf } from '@ember/utils';
|
|
|
|
import { IFacetsSelectionsMap, IFacetSelections } from '@datahub/data-models/types/entity/facets';
|
2020-08-26 15:44:50 -07:00
|
|
|
import { ISearchEntityRenderProps } from '@datahub/data-models/types/search/search-entity-render-prop';
|
|
|
|
import { KeyNamesWithValueType } from '@datahub/utils/types/base';
|
2019-08-31 20:51:14 -07:00
|
|
|
|
|
|
|
/**
|
2020-08-26 15:44:50 -07:00
|
|
|
* Filtering field names that returns array
|
|
|
|
*/
|
|
|
|
type FieldNames = KeyNamesWithValueType<ISearchEntityRenderProps, Array<string> | undefined>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns key/value pairs depending on key of field
|
|
|
|
* @param fields
|
|
|
|
* @param filterField
|
2019-08-31 20:51:14 -07:00
|
|
|
*/
|
2020-08-26 15:44:50 -07:00
|
|
|
const facetFilter = (
|
|
|
|
fields: Array<ISearchEntityRenderProps>,
|
|
|
|
filterField: FieldNames
|
2019-08-31 20:51:14 -07:00
|
|
|
): Record<string, Array<string>> => {
|
|
|
|
return fields.reduce((facetsApiParams: Record<string, Array<string>>, field): Record<string, Array<string>> => {
|
2020-08-26 15:44:50 -07:00
|
|
|
if (filterField && typeOf(field[filterField]) !== 'undefined') {
|
2019-08-31 20:51:14 -07:00
|
|
|
return {
|
|
|
|
...facetsApiParams,
|
2020-08-26 15:44:50 -07:00
|
|
|
[field.fieldName]: field[filterField] || []
|
2019-08-31 20:51:14 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return facetsApiParams;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
/**
|
|
|
|
* Will return an key value object with forced facets given a list of fields
|
|
|
|
*/
|
|
|
|
export const getFacetForcedValueForEntity = (fields: Array<ISearchEntityRenderProps>): Record<string, Array<string>> =>
|
|
|
|
facetFilter(fields, 'forcedFacetValue');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will return an key value object with deafult facets given a list of fields
|
|
|
|
*/
|
|
|
|
export const getFacetDefaultValueForEntity = (fields: Array<ISearchEntityRenderProps>): Record<string, Array<string>> =>
|
|
|
|
facetFilter(fields, 'facetDefaultValue');
|
|
|
|
|
2019-08-31 20:51:14 -07:00
|
|
|
/**
|
|
|
|
* Transforms an input like this:
|
|
|
|
* {
|
|
|
|
* status: ['PUBLISHED']
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* into this:
|
|
|
|
* {
|
|
|
|
* status: {
|
|
|
|
* PUBLISHED: true
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* This is useful to transform default facets into a string
|
|
|
|
* @param defaults
|
|
|
|
*/
|
|
|
|
export const transformDefaultsIntoSelections = (defaults: Record<string, Array<string>>): IFacetsSelectionsMap =>
|
|
|
|
Object.keys(defaults).reduce((selections: IFacetsSelectionsMap, facetKey: string): IFacetsSelectionsMap => {
|
|
|
|
const values = defaults[facetKey];
|
|
|
|
return {
|
|
|
|
...selections,
|
|
|
|
[facetKey]: values.reduce(
|
|
|
|
(selection: IFacetSelections, value: string): IFacetSelections => ({ ...selection, [value]: true }),
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}, {});
|