Skip to content

Commit

Permalink
refactor: move logic to wiring
Browse files Browse the repository at this point in the history
  • Loading branch information
diegopf committed Mar 11, 2024
1 parent 898dc7c commit 3ece027
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
<script lang="ts">
import Vue from 'vue';
import { Component } from 'vue-property-decorator';
import { SemanticQuery as SemanticQueryModel, TaggingRequest } from '@empathyco/x-types';
import { SemanticQuery as SemanticQueryModel } from '@empathyco/x-types';
import { xComponentMixin } from '../../../components/x-component.mixin';
import { semanticQueriesXModule } from '../x-module';
import { NoElement } from '../../../components/no-element';
import { State, XOn } from '../../../components';
import { State } from '../../../components';
import BaseSuggestions from '../../../components/suggestions/base-suggestions.vue';
import SemanticQuery from './semantic-query.vue';
Expand Down Expand Up @@ -88,25 +88,6 @@
findSemanticQuery(query: string): SemanticQueryModel | undefined {
return this.suggestions.find(suggestion => suggestion.query === query);
}
/**
* The queryTagging from the search module state.
*/
@State('search', 'queryTagging')
public queryTagging!: TaggingRequest;
/**
* Emits `QueryTaggingWithNoResults` event when the semantic queries response changed.
*
* @param semanticQueries - The new semantic queries list.
*/
@XOn('SemanticQueriesResponseChanged')
emitQueryTaggingFromSemantics(semanticQueries: SemanticQueryModel[]): void {
if (this.queryTagging.params.totalHits === '0') {
const totalHits = semanticQueries.length > 0 ? '-1' : '0';
this.$x.emit('QueryTaggingWithNoResults', { queryTagging: this.queryTagging, totalHits });
}
}
}
</script>

Expand Down
35 changes: 32 additions & 3 deletions packages/x-components/src/x-modules/tagging/wiring.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Taggable, Tagging, TaggingRequest } from '@empathyco/x-types';
import { SemanticQuery, Taggable, Tagging, TaggingRequest } from '@empathyco/x-types';
import { DefaultSessionService } from '@empathyco/x-utils';
import {
namespacedWireCommit,
Expand Down Expand Up @@ -206,6 +206,35 @@ export function createTrackWire(property: keyof Tagging): Wire<Taggable> {
);
}

/**
* Performs a track of a query with no results that used semantics as fallback.
* The totalHits will be changed to -1 if semantic queries are found in order to differentiate
* it from scenarios where the user encounters a no-results page without any semantic queries.
*
* @public
*/
export const trackNoResultsQueryWithSemanticsWire = filter(
wireDispatch('track', ({ eventPayload, state }) => {
const { queryTaggingInfo } = state;
const totalHits = (eventPayload as SemanticQuery[]).length > 0 ? -1 : 0;
return {
params: { ...queryTaggingInfo?.params, totalHits },
url: queryTaggingInfo?.url ?? ''
};
}),
({ store }) => Number(store.state.x.tagging.queryTaggingInfo?.params.totalHits)! === 0
);

/**.
* Debounced version of {@link trackNoResultsQueryWithSemanticsWire}
*
* @public
*/
export const trackNoResultsQueryWithSemanticsWireDebounced = moduleDebounce(
trackNoResultsQueryWithSemanticsWire,
({ state }) => state.config.queryTaggingDebounceMs
);

/**
* Factory helper to create a wire for the track of the display click.
*
Expand Down Expand Up @@ -298,8 +327,8 @@ export const taggingWiring = createWiring({
trackDisplayClickedWire,
setQueryTaggingFromQueryPreview
},
QueryTaggingWithNoResults: {
setQueryTaggingWithNoResults
SemanticQueriesResponseChanged: {
trackNoResultsQueryWithSemanticsWireDebounced
},
ModuleRegistered: {
setHasSemantics
Expand Down

0 comments on commit 3ece027

Please sign in to comment.