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: filters in history queries #1315

Merged
merged 13 commits into from
Oct 11, 2023
13 changes: 13 additions & 0 deletions packages/x-components/src/x-modules/facets/wiring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ export const setSelectedFiltersFromPreview = wireCommit(
({ eventPayload: { filters } }) => (filters ? createRawFilters(filters) : [])
);

/**
* Sets the filters of the facets module from a selectedHistoryQuery's filters.
*
* @public
*/
export const setFiltersFromHistoryQueries = wireCommit(
'setFilters',
({ eventPayload: { selectedFilters } }) => selectedFilters
);

/**
* Sets the query of the facets module from a queryPreview.
*
Expand Down Expand Up @@ -227,5 +237,8 @@ export const facetsWiring = createWiring({
UserAcceptedAQueryPreview: {
setQueryFromPreview,
setSelectedFiltersFromPreview
},
UserSelectedAHistoryQuery: {
setFiltersFromHistoryQueries
}
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HistoryQuery, Result } from '@empathyco/x-types';
import { Filter, HistoryQuery, Result } from '@empathyco/x-types';
import { DeepPartial } from '@empathyco/x-utils';
import Vue from 'vue';
import Vuex, { Store } from 'vuex';
Expand Down Expand Up @@ -276,11 +276,24 @@ describe('testing history queries module actions', () => {
{ modelName: 'Result', id: '2' }
];
const totalResults = results.length;
const requestFilters: Record<string, Filter[]> = {
categoryPaths: [
{
id: 'categoryIds:c018019b6',
selected: true,
modelName: 'HierarchicalFilter'
}
]
};
let gato: HistoryQuery, perro: HistoryQuery;

beforeEach(() => {
[gato, perro] = ['gato', 'perro'].map(query =>
createHistoryQuery({ query, timestamp: store.state.sessionTimeStampInMs + 1 })
createHistoryQuery({
query,
timestamp: store.state.sessionTimeStampInMs + 1,
selectedFilters: []
})
);
resetStateWith({ historyQueries: [gato, perro] });
});
Expand Down Expand Up @@ -340,8 +353,8 @@ describe('testing history queries module actions', () => {
expectHistoryQueriesToEqual([{ ...gato, totalResults }, perro]);
});

it('updates a history query if the new totalResults is higher', async () => {
gato.totalResults = 1;
it('updates a history query if search response change', async () => {
gato.totalResults = 10;
resetStateWith({ historyQueries: [gato, perro] });
await store.dispatch('updateHistoryQueriesWithSearchResponse', {
request: {
Expand All @@ -355,19 +368,22 @@ describe('testing history queries module actions', () => {
expectHistoryQueriesToEqual([{ ...gato, totalResults }, perro]);
});

it('does not update a history query if the new totalResults is lower', async () => {
// eslint-disable-next-line max-len
it('updates a history query if search response change because a filter is selected', async () => {
gato.totalResults = 50;
const selectedFilters = Object.values(requestFilters)[0];
resetStateWith({ historyQueries: [gato, perro] });
await store.dispatch('updateHistoryQueriesWithSearchResponse', {
request: {
query: 'gato',
page: 1
page: 1,
filters: requestFilters
},
status: 'success',
results,
totalResults
});
expectHistoryQueriesToEqual([gato, perro]);
expectHistoryQueriesToEqual([{ ...gato, totalResults, selectedFilters }, perro]);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Filter } from '@empathyco/x-types';
import { HistoryQueriesXStoreModule } from '../types';
import { InternalSearchResponse } from '../../../search/index';

/**
* Default implementation for the
Expand All @@ -7,8 +9,7 @@ import { HistoryQueriesXStoreModule } from '../types';
* The matching history query will only be updated on the following scenarios:
* 1. If it is part of a previous session, not the current one.
* 2. If its total results count has not been registered yet.
* 3. If its total results count registered is less than the one specified on the search response,
* meaning that the previous update was part of a filtered request.
* 3. If there is a new search response.
*
* @param context - The {@link https://vuex.vuejs.org/guide/actions.html | context} of the actions,
* provided by Vuex.
Expand All @@ -29,18 +30,36 @@ export const updateHistoryQueriesWithSearchResponse: HistoryQueriesXStoreModule[
if (indexOfHistoryQuery >= 0) {
const historyQuery = state.historyQueries[indexOfHistoryQuery];
const isCurrentSessionHistoryQuery = historyQuery.timestamp > state.sessionTimeStampInMs;
if (
!isCurrentSessionHistoryQuery ||
historyQuery.totalResults == null ||
historyQuery.totalResults < searchResponse.totalResults
) {
if (!isCurrentSessionHistoryQuery || historyQuery.totalResults == null || searchResponse) {
const filters = createHistoryQueriesFiltersList(searchResponse.request.filters);

const newHistoryQueries = state.historyQueries.slice();
newHistoryQueries[indexOfHistoryQuery] = {
...historyQuery,
totalResults: searchResponse.totalResults
totalResults: searchResponse.totalResults,
selectedFilters: filters
};
return dispatch('setHistoryQueries', newHistoryQueries);
}
}
}
};

/**
* Take filters from the request and push them into a list.
*
* @param requestFilters - Filters from the request.
*
* @returns A list of selected filters in the history query.
*
*/
function createHistoryQueriesFiltersList(
requestFilters: InternalSearchResponse['request']['filters']
): Filter[] {
return requestFilters
? Object.values(requestFilters).reduce((accFilters, filters) => {
accFilters.push(...filters);
return accFilters;
}, [])
: [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export const historyQueriesXStoreModule: HistoryQueriesXStoreModule = {
setQuery,
setIsEnabled(state, isEnabled) {
state.isEnabled = isEnabled;
},
setSearchSelectedFilters(state, filters) {
state.historyQueries[0].selectedFilters = filters;
}
},
actions: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HistoryQuery } from '@empathyco/x-types';
import { Filter, HistoryQuery } from '@empathyco/x-types';
import { XActionContext, XStoreModule } from '../../../store';
import { QueryMutations, QueryState } from '../../../store/utils/query.utils';
import { UrlParams } from '../../../types/url-params';
Expand Down Expand Up @@ -93,6 +93,12 @@ export interface HistoryQueriesMutations extends QueryMutations {
* @param isEnabled - The new {@link HistoryQueriesState.isEnabled }.
*/
setIsEnabled(isEnabled: boolean): void;
/**
* Sets the {@link HistoryQueriesState.historyQueries } filters property.
*
* @param filters - The new {@link HistoryQueriesState.historyQueries } filters.
*/
setSearchSelectedFilters(filters: Filter[]): void;
}
/**
* HistoryQueries store actions.
Expand Down
12 changes: 12 additions & 0 deletions packages/x-components/src/x-modules/history-queries/wiring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ export const updateHistoryQueriesWithSearchResponse = wireDispatch(
'updateHistoryQueriesWithSearchResponse'
);

/**
* Sets the history queries state `filters` with a selectedHistoryQuery's filters.
*
* @public
*/
export const setSearchSelectedFilters = wireCommit(
'setSearchSelectedFilters',
({ eventPayload: { filters } }) => filters
);
/**
* Debounce function for the module.
*/
Expand Down Expand Up @@ -163,6 +172,9 @@ export const historyQueriesWiring = createWiring({
setHistoryQueriesQuery,
addQueryToHistoryQueries
},
UserSelectedAHistoryQuery: {
setSearchSelectedFilters
},
UserIsTypingAQuery: {
setHistoryQueriesQueryDebounce: moduleDebounce(
setHistoryQueriesQuery,
Expand Down
13 changes: 13 additions & 0 deletions packages/x-components/src/x-modules/search/wiring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ export const setSearchSelectedFiltersFromPreview = wireCommit(
({ eventPayload: { filters } }) => (filters ? createRawFilters(filters) : [])
);

/**
* Sets the search state `selectedFilters` with a selectedHistoryQuery's filters.
*
* @public
*/
export const setSearchSelectedFiltersFromPreviewable = wireCommit(
'setSelectedFilters',
({ eventPayload: { selectedFilters } }) => selectedFilters
);

/**
* Search wiring.
*
Expand Down Expand Up @@ -282,5 +292,8 @@ export const searchWiring = createWiring({
},
QueryPreviewUnselected: {
setSearchExtraParams
},
UserSelectedAHistoryQuery: {
setSearchSelectedFiltersFromPreviewable
}
});
3 changes: 3 additions & 0 deletions packages/x-types/src/history-query.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NamedModel } from './named-model.model';
import { Previewable } from './previewable.model';
import { Filter } from './facet';

/**
* Represents a query that has been made by the user.
Expand All @@ -9,4 +10,6 @@ import { Previewable } from './previewable.model';
export interface HistoryQuery extends Previewable, NamedModel<'HistoryQuery'> {
/** Timestamp when the history query was created. */
timestamp: number;
/** Filters selected for the query to search for. */
selectedFilters?: Filter[];
}