empathyco / x

Commerce Search & Discovery frontend web components
Apache License 2.0
80 stars 19 forks source link

feat: update result information in the QPs #1591

Closed lauramargar closed 1 month ago

lauramargar commented 1 month ago

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

Type of change

What is the destination branch of this PR?

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: