Skip to content

Commit

Permalink
feat: filter query preview (#1296)
Browse files Browse the repository at this point in the history
EMP-1825

Co-authored-by: Gerardo Vázquez <[email protected]>
Co-authored-by: Guillermo <[email protected]>
  • Loading branch information
3 people authored Sep 20, 2023
1 parent d9a16ab commit 9543c47
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
44 changes: 41 additions & 3 deletions packages/x-components/src/x-modules/facets/wiring.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
import { Facet } from '@empathyco/x-types';
import { UrlParams } from '../../types/url-params';
import { createRawFilters } from '../../utils/filters';
import { wireCommit, wireService, wireServiceWithoutPayload } from '../../wiring/wires.factory';
import { wireService, wireServiceWithoutPayload } from '../../wiring/wires.factory';
import { filter, mapWire } from '../../wiring/wires.operators';
import { createWiring } from '../../wiring/wiring.utils';
import { XEventPayload } from '../../wiring/index';
import {
namespacedWireCommit,
namespacedWireCommitWithoutPayload,
XEventPayload
} from '../../wiring/index';
import { DefaultFacetsService } from './service/facets.service';

/**
* `facets` {@link XModuleName | XModule name}.
*
* @internal
*/
const moduleName = 'facets';

/**
* WireCommit for {@link FacetsXModule}.
*
* @internal
*/
const wireCommit = namespacedWireCommit(moduleName);

/**
* WireCommitWithoutPayload for {@link FacetsXModule}.
*
* @internal
*/
const wireCommitWithoutPayload = namespacedWireCommitWithoutPayload(moduleName);

/**
* Wires factory for {@link DefaultFacetsService}.
*/
Expand Down Expand Up @@ -125,12 +150,22 @@ const setQuery = wireFacetsService('setQuery');
* @internal
*/
const clearStickyFilters = filter<XEventPayload<'SearchResponseChanged'>>(
wireCommit('x/facets/clearStickyFilters'),
wireCommitWithoutPayload('clearStickyFilters'),
({ eventPayload }) => {
return eventPayload.totalResults === 0;
}
);

/**
* Sets the filters of the facets module from a selectedQueryPreview's filters.
*
* @public
*/
export const setSelectedFiltersFromPreview = wireCommit(
'setFilters',
({ eventPayload: { filters } }) => (filters ? createRawFilters(filters) : [])
);

/**
* Wiring configuration for the {@link FacetsXModule | facets module}.
*
Expand Down Expand Up @@ -181,5 +216,8 @@ export const facetsWiring = createWiring({
},
SearchResponseChanged: {
clearStickyFilters
},
UserAcceptedAQueryPreview: {
setSelectedFiltersFromPreview
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { QueryPreviewInfo } from '../../store/index';
import { getXComponentXModuleName, isXComponent } from '../../../../components/index';

function renderQueryPreviewButton({
queryPreviewInfo = { query: 'milk', extraParams: { store: 'Magrathea' } },
queryPreviewInfo = {
query: 'milk',
extraParams: { store: 'Magrathea' },
filters: ['fit:regular']
},
template = `<QueryPreviewButton v-bind="$attrs" />`
}: RenderQueryPreviewButtonOptions = {}): RenderQueryPreviewButtonAPI {
const localVue = createLocalVue();
Expand Down Expand Up @@ -98,7 +102,8 @@ describe('query preview button', () => {
query: 'milk',
extraParams: {
store: 'Magrathea'
}
},
filters: ['fit:regular']
});

await updateExtraParams({ warehouse: 42 });
Expand All @@ -110,7 +115,8 @@ describe('query preview button', () => {
extraParams: {
store: 'Magrathea',
warehouse: 42
}
},
filters: ['fit:regular']
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@
* @internal
*/
protected get events(): Partial<XEventsTypes> {
return { UserAcceptedAQueryPreview: this.fullQueryPreviewInfo };
return {
UserAcceptedAQueryPreview: this.fullQueryPreviewInfo
};
}
}
</script>
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 @@ -6,6 +6,7 @@ import {
} from '../../wiring/namespaced-wires.factory';
import { WirePayload } from '../../wiring/wiring.types';
import { createWiring } from '../../wiring/wiring.utils';
import { createRawFilters } from '../../utils/filters';
import { InternalSearchRequest } from './types';

/**
Expand All @@ -14,6 +15,7 @@ import { InternalSearchRequest } from './types';
* @internal
*/
const moduleName = 'search';

/**
* WireCommit for {@link SearchXModule}.
*
Expand Down Expand Up @@ -202,6 +204,16 @@ export const setSearchExtraParamsFromPreview = wireCommit(
({ eventPayload: { extraParams } }) => extraParams
);

/**
* Sets the search state `selectedFilters` with the selectedQueryPreview's filters.
*
* @public
*/
export const setSearchSelectedFiltersFromPreview = wireCommit(
'setSelectedFilters',
({ eventPayload: { filters } }) => (filters ? createRawFilters(filters) : [])
);

/**
* Search wiring.
*
Expand Down Expand Up @@ -265,6 +277,7 @@ export const searchWiring = createWiring({
UserAcceptedAQueryPreview: {
setSearchQueryFromPreview,
setSearchExtraParamsFromPreview,
setSearchSelectedFiltersFromPreview,
saveOriginWire
},
QueryPreviewUnselected: {
Expand Down
14 changes: 13 additions & 1 deletion packages/x-components/src/x-modules/url/wiring.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { namespacedWireCommit } from '../../wiring/namespaced-wires.factory';
import { createWiring } from '../../wiring/wiring.utils';
import { createRawFilters } from '../../utils/filters';

/**
* WireCommit for {@link UrlXModule}.
Expand Down Expand Up @@ -63,6 +64,16 @@ export const setUrlParamsFromPreview = wireCommit(
({ eventPayload: { extraParams } }) => extraParams
);

/**
* Sets the filters of the url module from a selectedQueryPreview's filters.
*
* @public
*/
export const setUrlSelectedFiltersFromPreview = wireCommit(
'setFilters',
({ eventPayload: { filters } }) => (filters ? createRawFilters(filters) : [])
);

/**
* Sets the scroll of the url module.
*
Expand Down Expand Up @@ -98,7 +109,8 @@ export const urlWiring = createWiring({
},
UserAcceptedAQueryPreview: {
setUrlQueryFromPreview,
setUrlParamsFromPreview
setUrlParamsFromPreview,
setUrlSelectedFiltersFromPreview
},
UserClearedQuery: {
setUrlQuery
Expand Down

0 comments on commit 9543c47

Please sign in to comment.