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: filter query preview #1296

Merged
merged 13 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
39 changes: 36 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,32 @@
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';

const wireCommitWithoutPayload = namespacedWireCommitWithoutPayload(moduleName);
Copy link
Contributor

Choose a reason for hiding this comment

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

Document this and put it below the wireCommit.


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

/**
* Wires factory for {@link DefaultFacetsService}.
*/
Expand Down Expand Up @@ -125,12 +145,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 +211,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
CachedaCodes marked this conversation as resolved.
Show resolved Hide resolved
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
12 changes: 12 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 Down Expand Up @@ -202,6 +203,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 +276,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/index';
CachedaCodes marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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