Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(facets): Unify __unknown__ and __unknown-facet__ in a constant #1351

Merged
merged 5 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the file for the constants that we're injecting in several places of the app, we're not injecting the new constant anywhere so it shouldn't be here.
A better place would be in the facets module. See the history queries module for an example.

Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ export const SELECTED_VARIANTS_KEY: XInjectKey<ResultVariant[]> = 'selectedVaria
export const SELECT_RESULT_VARIANT_KEY: XInjectKey<
(variant: ResultVariant, level?: number) => void
> = 'selectResultVariant';

/**
* It's used to identify filters without facetId.
*
* @internal
*/
export const FACET_KEY: string | number = '__unknown__';
annacv marked this conversation as resolved.
Show resolved Hide resolved
annacv marked this conversation as resolved.
Show resolved Hide resolved
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 { FACET_KEY } from '../../../components/index';
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 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 : 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 { FACET_KEY } from '../../../../components/index';
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 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']]
[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']]
[FACET_KEY]: [store.state.filters['size:xl']]
});
});
});
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 } from '../types';
import { FACET_KEY } from '../../../../components/index';

/**
* 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 : 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 { FACET_KEY } from '../../../../components/index';

/**
* 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
* {@link FACET_KEY | '__unknown__'} key.
*
* @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 : 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__: [
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 { FACET_KEY } from '../../../../components/index';

/**
* 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 !== FACET_KEY ? responseFacets.find(facet => facet.id === facetId) : null;

return facetFilters.reduce<Filter[]>((accFilters, requestFilter) => {
const matchingFilter = matchingFacet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 { FACET_KEY } from '../../../components/index';
import {
cancelFetchAndSaveSearchResponse,
fetchAndSaveSearchResponse
Expand Down Expand Up @@ -61,7 +62,7 @@ export const searchXStoreModule: SearchXStoreModule = {
},
setSelectedFilters(state, selectedFilters) {
state.selectedFilters = groupItemsBy(selectedFilters, filter =>
isFacetFilter(filter) ? filter.facetId : '__unknown__'
isFacetFilter(filter) ? filter.facetId : FACET_KEY
);
},
setBanners(state, banners) {
Expand Down