Skip to content

Commit

Permalink
fix: PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
annacv committed Oct 18, 2023
1 parent 8e8f8e7 commit 3d03a19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ describe('testing history queries module actions', () => {
// eslint-disable-next-line max-len
it('updates a history query when the search response changes because a filter is selected', async () => {
gato.totalResults = 50;
const selectedFilters = createHistoryQueriesFiltersList(responseFacets);
const selectedFilters = createHistoryQueriesFiltersList(responseFacets, requestFilters);
resetStateWith({ historyQueries: [gato, perro] });
await store.dispatch('updateHistoryQueriesWithSearchResponse', {
request: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export const updateHistoryQueriesWithSearchResponse: HistoryQueriesXStoreModule[
const historyQuery = state.historyQueries[indexOfHistoryQuery];
const isCurrentSessionHistoryQuery = historyQuery.timestamp > state.sessionTimeStampInMs;
if (!isCurrentSessionHistoryQuery || historyQuery.totalResults == null || searchResponse) {
const filters = createHistoryQueriesFiltersList(searchResponse.facets);
const filters = createHistoryQueriesFiltersList(
searchResponse.facets,
searchResponse.request.filters
);

const newHistoryQueries = state.historyQueries.slice();
newHistoryQueries[indexOfHistoryQuery] = {
Expand All @@ -46,21 +49,30 @@ export const updateHistoryQueriesWithSearchResponse: HistoryQueriesXStoreModule[
};

/**
* Take facets from the response and push the selected filters into a list.
* Creates a selected filters list by comparing request filters and response facets.
* Uses the 'filter.id' to match and merge the objects in a single one with all the keys.
*
* @param responseFacets - Facets from the response.
* @param requestFilters - Filters from the request.
*
* @returns A list of selected filters in the history query.
*
*/
export function createHistoryQueriesFiltersList(
responseFacets: InternalSearchResponse['facets']
responseFacets: InternalSearchResponse['facets'],
requestFilters: InternalSearchResponse['request']['filters']
): Filter[] {
return responseFacets
? Object.values(responseFacets).reduce((accFilters: Filter[], facet) => {
facet.filters.forEach(filter => {
return filter.selected ? accFilters.push(filter) : [];
});
return requestFilters && responseFacets
? Object.values(requestFilters).reduce((accFilters, filters) => {
responseFacets.map(facet =>
facet.filters.forEach(filter => {
filters.forEach(requestFilter => {
if (requestFilter.id === filter.id) {
accFilters.push(Object.assign(requestFilter, filter));
}
});
})
);
return accFilters;
}, [])
: [];
Expand Down

0 comments on commit 3d03a19

Please sign in to comment.