Skip to content

Commit

Permalink
feat: update result information in the QPs (#1591)
Browse files Browse the repository at this point in the history
  • Loading branch information
lauramargar authored Aug 14, 2024
1 parent 00c4f52 commit 3e99d0c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const result = createResultStub('jacket', { variants });
const render = ({
template = '<ResultVariantSelector/>',
result = {},
autoSelectDepth = Number.POSITIVE_INFINITY
autoSelectDepth = Number.POSITIVE_INFINITY,
queryPreviewHash = null as string | null
} = {}) => {
installNewXPlugin();
const emitSpy = jest.spyOn(XPlugin.bus, 'emit');
Expand All @@ -50,6 +51,9 @@ const render = ({
ResultVariantsProvider,
ResultVariantSelector
},
provide: {
queryPreviewHash
},
data: () => ({
result,
autoSelectDepth
Expand Down Expand Up @@ -165,7 +169,26 @@ describe('results with variants', () => {
expect(emitSpy).toHaveBeenCalledTimes(1);
expect(emitSpy).toHaveBeenCalledWith(
'UserSelectedAResultVariant',
{ result, variant: variants[0], level: 0 },
{ result, variant: variants[0], level: 0, queryPreviewHash: null },
expect.anything()
);
});

it('emits UserSelectedAResultVariant event when a variant from a query preview is selected', async () => {
const { wrapper, emitSpy } = render({
result,
autoSelectDepth: 0,
queryPreviewHash: 'abcd'
});

const button = wrapper.find(getDataTestSelector('variant-button'));

await button.trigger('click');

expect(emitSpy).toHaveBeenCalledTimes(1);
expect(emitSpy).toHaveBeenCalledWith(
'UserSelectedAResultVariant',
{ result, variant: variants[0], level: 0, queryPreviewHash: 'abcd' },
expect.anything()
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<script lang="ts">
import { defineComponent, ref, computed, watch, provide, Ref, PropType, h } from 'vue';
import {
defineComponent,
ref,
computed,
watch,
provide,
Ref,
PropType,
h,
inject,
ComputedRef
} from 'vue';
import { Result, ResultVariant } from '@empathyco/x-types';
import {
RESULT_WITH_VARIANTS_KEY,
Expand Down Expand Up @@ -55,6 +66,13 @@
*/
const selectedVariants = ref<ResultVariant[]>([]);
/**
* It injects the queryPreviewHash provided by a query-preview.
*
* @internal
*/
const queryPreviewHash = inject<ComputedRef<string> | null>('queryPreviewHash', null);
/**
* Selects a variant of the result.
* When called, it slices the array of selected variants to remove the selected child variants.
Expand All @@ -68,7 +86,12 @@
return;
}
selectedVariants.value.splice(level, Number.POSITIVE_INFINITY, variant);
xBus.emit('UserSelectedAResultVariant', { variant, level, result: result.value });
xBus.emit('UserSelectedAResultVariant', {
variant,
level,
result: result.value,
queryPreviewHash
});
}
/**
Expand Down
12 changes: 9 additions & 3 deletions packages/x-components/src/wiring/events.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Result, ResultVariant, Suggestion } from '@empathyco/x-types';
import { ComputedRef } from 'vue';
import { ExtractPayload } from '../store/store.types';
import { ArrowKey, PropsWithType } from '../utils';
import { DeviceXEvents } from '../x-modules/device';
Expand Down Expand Up @@ -200,10 +201,15 @@ export interface XEventsTypes
UserReachedEmpathizeTop: void;
/**
* The user selected a result variant.
* Payload: And object containing the result, the selected variant and the level of the selected
* variant.
* Payload: And object containing the result, the selected variant, the level of the selected
* variant and the query preview hash.
*/
UserSelectedAResultVariant: { result: Result; variant: ResultVariant; level: number };
UserSelectedAResultVariant: {
result: Result;
variant: ResultVariant;
level: number;
queryPreviewHash: ComputedRef<string> | null;
};
/**
* User selected any kind of suggestion (query-suggestion, popular-search...)
* Payload: The {@link @empathyco/x-types#Suggestion | suggestion} that the user selected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@
return getHashFromQueryPreviewInfo(props.queryPreviewInfo);
});
provide('queryPreviewHash', queryPreviewHash);
/**
* Gets from the state the results preview of the query preview.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = {
Vue.delete(state.queriesPreview, queryPreviewHash);
}
}
},
updateAQueryPreviewResult(state, { result, queryPreviewHash }) {
const queryPreviewResult = state.queriesPreview[queryPreviewHash.value]?.results.find(
resultPreview => resultPreview.id === result.id
);
if (queryPreviewResult) {
Object.assign(queryPreviewResult, result);
}
}
},
actions: {
Expand Down
15 changes: 15 additions & 0 deletions packages/x-components/src/x-modules/queries-preview/store/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Result, SearchRequest, SearchResponse, TaggingRequest } from '@empathyco/x-types';
import { Dictionary } from '@empathyco/x-utils';
import { ComputedRef } from 'vue';
import { XActionContext } from '../../../store/actions.types';
import { XStoreModule } from '../../../store/store.types';
import { RequestStatus, StatusState } from '../../../store/utils/status-store.utils';
Expand Down Expand Up @@ -134,6 +135,20 @@ export interface QueriesPreviewMutations extends ConfigMutations<QueriesPreviewS
queryPreviewHash: string;
cache: boolean;
}): void;
/**
* Updates a result with new fields.
*
* @param QueryPreviewResultPayload - Information needed to update a query preview result.
* result is an object that contains at least an id, and the properties to modify.
* queryPreviewHash is the query preview key to find the QueryPreview saved in the state.
*/
updateAQueryPreviewResult({
result,
queryPreviewHash
}: {
result: Result;
queryPreviewHash: ComputedRef<string>;
}): void;
}

/**
Expand Down

0 comments on commit 3e99d0c

Please sign in to comment.