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: update result information in the QPs #1591

Merged

Conversation

lauramargar
Copy link
Contributor

@lauramargar lauramargar commented Aug 13, 2024

Pull request template

Currently, if we are working with results with variants, we cannot update the result information when switching variants in the preview queries.

That's why it was necessary to carry out this task, which involves modifying the payload of the UserSelectedAResultVariant event to send the hash of the preview query of the result to be modified, and creating a mutation in the queriesPreview module to update the result with the new information.

Motivation and context

  • Dependencies. If any, specify:
  • Open issue. If applicable, link: EMP-4683

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that causes existing functionality to not work as expected)
  • Change requires a documentation update

What is the destination branch of this PR?

  • Main
  • Other. Specify:

How has this been tested?

Try in primor setup to install this x version locally and change variants in a query preview result.

And add this code in the plugin.options.ts:

queriesPreview: {
      wiring: {
        UserSelectedAResultVariant: {
          updateResultVariantWire
        }
      }
    },
const updateResultVariantWire = createWireFromFunction<XEventPayload<'UserSelectedAResultVariant'>>(
  ({ store, eventPayload }) => {
    const resultFromEvent = { ...eventPayload.result };
    const variantSelected = eventPayload.variant;
    const queryPreviewHash = eventPayload.queryPreviewHash;

    if (resultFromEvent) {
      const updateResult = {
        ...resultFromEvent,
        price: {
          value: variantSelected.salePrice,
          originalValue: variantSelected.originalPrice,
          hasDiscount:
            variantSelected.salePrice < (variantSelected.originalPrice ?? variantSelected.salePrice)
        },
        discountPct: variantSelected.discountPct,
        variantValue: variantSelected.variantValue,
        images: [variantSelected.imageLink],
        url: variantSelected.link ?? resultFromEvent.url,
        hexadecimalCode: variantSelected.hexadecimalCode,
        variantAttributeId: variantSelected.variantAttributeId,
        variantOptionId: variantSelected.variantOptionId
      };
      if (queryPreviewHash) {
        store.commit('x/queriesPreview/updateAQueryPreviewResult', {
          result: updateResult,
          queryPreviewHash
        });
      } else {
        store.commit('x/search/updateResult', updateResult);
      }
    }
  }
);

Checklist:

  • My code follows the style guidelines of this project.
  • I have performed a self-review on my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

@lauramargar lauramargar requested a review from a team as a code owner August 13, 2024 07:53
Copy link
Contributor

@diegopf diegopf left a comment

Choose a reason for hiding this comment

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

Great job! I like the new approach

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd enhance the render function to allow configure the provide values and add a new test case where check that the provided value is sent as payload:

const render = ({
  template = '<ResultVariantSelector/>',
  result = {},
  autoSelectDepth = Number.POSITIVE_INFINITY,
+  provide = {}
  it('emits UserSelectedAResultVariant event when a variant from a query preview is selected', async () => {
    const { wrapper, emitSpy } = render({
      result,
      autoSelectDepth: 0,
      provide: {
        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()
    );
  });

@@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const queryPreviewResult = state.queriesPreview[queryPreviewHash.value].results.find(
const queryPreviewResult = state.queriesPreview[queryPreviewHash.value]?.results.find(

@diegopf diegopf merged commit 3e99d0c into main Aug 14, 2024
4 checks passed
@diegopf diegopf deleted the feature/EMP-4683-update-result-information-in-the-queries-preview branch August 14, 2024 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants