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 setSelectedFiltersFromHitoryQueries = wireCommit(
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
'setFilters',
({ eventPayload: { filters } }) => filters
);

/**
* Wiring configuration for the {@link FacetsXModule | facets module}.
*
Expand Down Expand Up @@ -219,5 +229,8 @@ export const facetsWiring = createWiring({
},
UserAcceptedAQueryPreview: {
setSelectedFiltersFromPreview
},
UserSelectedAHistoryQuery: {
setSelectedFiltersFromHitoryQueries
}
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Filter } from '@empathyco/x-types';
import { HistoryQueriesXStoreModule } from '../types';

/**
Expand Down Expand Up @@ -32,12 +33,28 @@ export const updateHistoryQueriesWithSearchResponse: HistoryQueriesXStoreModule[
if (
!isCurrentSessionHistoryQuery ||
historyQuery.totalResults == null ||
historyQuery.totalResults < searchResponse.totalResults
historyQuery.totalResults !== searchResponse.totalResults
) {
const filtersApplied: Filter[] = [];
if (searchResponse.request.filters) {
Object.keys(searchResponse.request.filters).forEach(key => {
const facet = searchResponse.request.filters![key];
facet.forEach(filter => {
if (filtersApplied.includes(filter)) {
const filterIndex = filtersApplied.findIndex(value => value.id === filter.id);
filtersApplied.splice(filterIndex, 1);
} else {
filtersApplied.push(filter);
}
});
});
}

const newHistoryQueries = state.historyQueries.slice();
newHistoryQueries[indexOfHistoryQuery] = {
...historyQuery,
totalResults: searchResponse.totalResults
totalResults: searchResponse.totalResults,
filters: filtersApplied
};
return dispatch('setHistoryQueries', newHistoryQueries);
}
Expand Down
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].filters = 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: { filters } }) => filters
);

/**
* 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. */
filters?: Filter[];
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
}
Loading