Skip to content

Commit

Permalink
feat(facets): Unify __unknown__ and __unknown-facet__ in a constant (#…
Browse files Browse the repository at this point in the history
…1351)

BREAKING CHANGE: The '__unknown-facet__' value for a facetId is deprecated: it has been replaced by the UNKNOWN_FACET_KEY constant, whose value is '__unknown__'.

Co-authored-by: Guillermo Cacheda <[email protected]>
  • Loading branch information
annacv and CachedaCodes authored Nov 16, 2023
1 parent d40ae0a commit 30585f2
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Facet, FilterModelName, Filter, isFacetFilter } from '@empathyco/x-types';
import { Store } from 'vuex';
import { RootXStoreState } from '../../../store/store.types';
import { UNKNOWN_FACET_KEY } from '../store/constants';
import { EditableNumberRangeFilterEntity } from './editable-number-range-filter.entity';
import { HierarchicalFilterEntity } from './hierarchical-filter.entity';
import { NumberRangeFilterEntity } from './number-range-filter.entity';
Expand Down Expand Up @@ -49,7 +50,7 @@ export class FilterEntityFactory {
*
* @internal
*/
protected cache: Record<Facet['id'] | '__unknown-facet__', FilterEntity> = {};
protected cache: Record<Facet['id'] | typeof UNKNOWN_FACET_KEY, FilterEntity> = {};

/**
* Creates a new FilterEntity from a filter.
Expand All @@ -61,7 +62,7 @@ export class FilterEntityFactory {
* @returns The {@link FilterEntity} created by the factory.
*/
getFilterEntity(store: Store<RootXStoreState>, filter: Filter): FilterEntity {
const cacheKey = isFacetFilter(filter) ? filter.facetId : '__unknown-facet__';
const cacheKey = isFacetFilter(filter) ? filter.facetId : UNKNOWN_FACET_KEY;
return this.cache[cacheKey] ?? (this.cache[cacheKey] = this.createFilterEntity(store, filter));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { SafeStore } from '../../../../store/__tests__/utils';
import { arrayToObject } from '../../../../utils/array';
import { facetsXStoreModule } from '../module';
import { FacetsActions, FacetsGetters, FacetsMutations, FacetsState } from '../types';
import { UNKNOWN_FACET_KEY } from '../constants';
import { resetFacetsStateWith } from './utils';

describe('testing facets module getters', () => {
Expand Down Expand Up @@ -266,7 +267,7 @@ describe('testing facets module getters', () => {
createNumberRangeFilter('price', { min: 25, max: 50 }, true),
createEditableNumberRangeFilter('age', { min: null, max: 5 }),
createEditableNumberRangeFilter('size', { min: null, max: null }),
// Raw filters don't belong to a facet so they will be under `__unknown-facet__` key
// Raw filters don't belong to a facet, so they will be under UNKNOWN_FACET_KEY key
createRawFilter('size:xl')
]);

Expand All @@ -275,7 +276,7 @@ describe('testing facets module getters', () => {
category: [store.state.filters['category:Shorts']],
price: [store.state.filters['price:25-50']],
age: [store.state.filters['age:*-5']],
['__unknown-facet__']: [store.state.filters['size:xl']]
[UNKNOWN_FACET_KEY]: [store.state.filters['size:xl']]
});
});

Expand Down Expand Up @@ -326,7 +327,7 @@ describe('testing facets module getters', () => {
const store = createFacetsStore([createRawFilter('size:xl')]);

expect(store.getters.selectedFiltersByFacet).toEqual({
['__unknown-facet__']: [store.state.filters['size:xl']]
[UNKNOWN_FACET_KEY]: [store.state.filters['size:xl']]
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const UNKNOWN_FACET_KEY = '__unknown__';
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isFacetFilter } from '@empathyco/x-types';
import { map } from '@empathyco/x-utils';
import { groupItemsBy } from '../../../../utils/array';
import { FacetsXStoreModule } from '../types';
import { UNKNOWN_FACET_KEY } from '../constants';

/**
* Default implementation for the {@link FacetsGetters.facets} getter.
Expand All @@ -14,7 +15,7 @@ import { FacetsXStoreModule } from '../types';
*/
export const facets: FacetsXStoreModule['getters']['facets'] = state => {
const filtersByFacet = groupItemsBy(Object.values(state.filters), filter =>
isFacetFilter(filter) ? filter.facetId : '__unknown-facet__'
isFacetFilter(filter) ? filter.facetId : UNKNOWN_FACET_KEY
);
return map(state.facets, (_id, facet) => ({
...facet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isFacetFilter } from '@empathyco/x-types';
import { map } from '@empathyco/x-utils';
import { groupItemsBy } from '../../../../utils/array';
import { FacetsXStoreModule, FiltersByFacet } from '../types';
import { UNKNOWN_FACET_KEY } from '../constants';

/**
* Default implementation for the {@link FacetsGetters.selectedFiltersByFacet} getter.
Expand All @@ -12,8 +13,8 @@ import { FacetsXStoreModule, FiltersByFacet } from '../types';
* facets' module.
*
* @returns A record containing the selected filters indexed by its facet id.
* @remarks If there are filters without facet Id (RawFilter), they will be grouped under
* `__unknown-facet__` key.
* @remarks If there are filters without facetId (RawFilter), they will be grouped under
* the UNKNOWN_FACET_KEY constant.
*
* @public
*/
Expand All @@ -24,7 +25,7 @@ export const selectedFiltersByFacet: FacetsXStoreModule['getters']['selectedFilt
// The `emptyRecord` is to return an empty array for those facets that haven't selected filters.
const emptyRecord: FiltersByFacet = map(state.facets, () => []);
const filtersByFacet = groupItemsBy(getters.selectedFilters, filter =>
isFacetFilter(filter) ? filter.facetId : '__unknown-facet__'
isFacetFilter(filter) ? filter.facetId : UNKNOWN_FACET_KEY
);
return Object.assign(emptyRecord, filtersByFacet);
};
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ describe('testing history queries module actions', () => {
query: 'gato',
page: 1,
filters: {
__unknown__: [
UNKNOWN_FACET_KEY: [
{
id: 'categoryIds:66dd06d9f',
selected: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Filter } from '@empathyco/x-types';
import { HistoryQueriesXStoreModule } from '../types';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { HistoryQueriesActions, HistoryQueriesXStoreModule } from '../types';
import { InternalSearchResponse } from '../../../search/index';
import { UNKNOWN_FACET_KEY } from '../../../facets/store/constants';

/**
* Default implementation for the
Expand Down Expand Up @@ -67,7 +69,7 @@ function getHistoryQueriesFiltersList(
} else {
return Object.entries(requestFilters).flatMap(([facetId, facetFilters]) => {
const matchingFacet =
facetId !== '__unknown__' ? responseFacets.find(facet => facet.id === facetId) : null;
facetId !== UNKNOWN_FACET_KEY ? responseFacets.find(facet => facet.id === facetId) : null;

return facetFilters.reduce<Filter[]>((accFilters, requestFilter) => {
const matchingFilter = matchingFacet
Expand Down
4 changes: 2 additions & 2 deletions packages/x-components/src/x-modules/search/store/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { setQuery } from '../../../store/utils/query.utils';
import { setStatus } from '../../../store/utils/status-store.utils';
import { groupItemsBy } from '../../../utils/array';
import { mergeConfig, setConfig } from '../../../store/utils/config-store.utils';
// eslint-disable-next-line max-len
import { UNKNOWN_FACET_KEY } from '../../facets/store/constants';
import {
cancelFetchAndSaveSearchResponse,
fetchAndSaveSearchResponse
Expand Down Expand Up @@ -61,7 +61,7 @@ export const searchXStoreModule: SearchXStoreModule = {
},
setSelectedFilters(state, selectedFilters) {
state.selectedFilters = groupItemsBy(selectedFilters, filter =>
isFacetFilter(filter) ? filter.facetId : '__unknown__'
isFacetFilter(filter) ? filter.facetId : UNKNOWN_FACET_KEY
);
},
setBanners(state, banners) {
Expand Down

0 comments on commit 30585f2

Please sign in to comment.