Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Add Product Query Support for Atomic Rating Block #7352

Merged
merged 14 commits into from
Oct 31, 2022

Conversation

danielwrobert
Copy link
Contributor

@danielwrobert danielwrobert commented Oct 10, 2022

Adds Product Query support for the atomic Rating block.

On the client side, when the Rating Block is used within the Product Query block, the markup will be rendered on the server side - no javascript related to the Rating block will be loaded.

Fixes #7330

Testing

  1. Checkout this branch and run npm start (need to run the start command to catch the experimental flags).
  2. Be sure that you have the latest version of the Gutenberg feature plugin installed and activated.
  3. Add the woocommerce/product-rating block to the Product Query INNER_BLOCKS_TEMPLATE in assets/js/blocks/product-query/constants.ts under line 54 (only for testing, remove afterwards).
  4. Choose at least one product and add a rating to it (this can be done on a single product view for any product).
  5. Create a post/page and add the Product Query block.
  6. Save it.
  7. Open the frontend side and confirm that:
    • No JS related to the Product Rating block is loaded (search in your network tab for product-rating-frontend.js).
    • The UI of the block is identical to on the editor side.
    • That the settings are reflected correctly (eg: custom style).
  8. Confirm that the rating stars for the product that you've added the rating to appear as expected in the UI.
  9. Back in the editor confirm that the Product Rating block can be added as a child of the Product query block via the inserter.
  10. For comparison, switch to trunk and add the All Products block to confirm that the aforementioned product-rating-frontend.js is loaded.
  • Do not include in the Testing Notes

WooCommerce Visibility

  • WooCommerce Core
  • Feature plugin
  • Experimental

@danielwrobert danielwrobert self-assigned this Oct 10, 2022
@danielwrobert danielwrobert added type: refactor The issue/PR is related to refactoring. skip-changelog PRs that you don't want to appear in the changelog. block-type: product elements Issues related to Product Element blocks. labels Oct 10, 2022
@github-actions
Copy link
Contributor

The release ZIP for this PR is accessible via:

https://wcblocks.wpcomstaging.com/wp-content/uploads/woocommerce-gutenberg-products-block-7352.zip

@github-actions
Copy link
Contributor

github-actions bot commented Oct 10, 2022

Size Change: +6.82 kB (+1%)

Total Size: 999 kB

Filename Size Change
build/active-filters.js 7.3 kB -1 B (0%)
build/all-products.js 33.7 kB +70 B (0%)
build/all-reviews.js 7.79 kB -2 B (0%)
build/cart-blocks/cart-cross-sells-products-frontend.js 4.68 kB +17 B (0%)
build/cart-frontend.js 54.5 kB -3 B (0%)
build/cart.js 47.5 kB +27 B (0%)
build/checkout.js 41.2 kB +10 B (0%)
build/featured-category.js 13.2 kB -1 B (0%)
build/featured-product.js 13.5 kB -1 B (0%)
build/handpicked-products.js 7.33 kB +1 B (0%)
build/legacy-template.js 2.83 kB -1 B (0%)
build/mini-cart-contents.js 17.2 kB +4 B (0%)
build/product-add-to-cart--product-button--product-image--product-title.js 0 B -2.67 kB (removed) 🏆
build/product-add-to-cart.js 8.57 kB +2.1 kB (+32%) 🚨
build/product-button--product-image--product-rating--product-sale-badge--product-title.js 301 B +1 B (0%)
build/product-button.js 3.87 kB +2.29 kB (+145%) 🆘
build/product-image.js 3.99 kB +2.38 kB (+147%) 🆘
build/product-price.js 1.53 kB +1 B (0%)
build/product-query.js 2.89 kB -1 B (0%)
build/product-rating-frontend.js 1.2 kB +13 B (+1%)
build/product-rating.js 788 B +14 B (+2%)
build/product-sale-badge.js 817 B +2 B (0%)
build/product-tag.js 8.05 kB -1 B (0%)
build/product-title.js 3.36 kB +2.42 kB (+259%) 🆘
build/rating-filter.js 6.16 kB +2 B (0%)
build/reviews-by-category.js 11.3 kB +2 B (0%)
build/reviews-by-product.js 12.4 kB -2 B (0%)
build/single-product.js 10.1 kB -1 B (0%)
build/stock-filter.js 6.65 kB -2 B (0%)
build/wc-blocks-vendors.js 62.4 kB +1 B (0%)
build/product-add-to-cart--product-button--product-image--product-rating--product-title.js 151 B +151 B (new file) 🆕
ℹ️ View Unchanged
Filename Size
build/active-filters-frontend.js 7.72 kB
build/active-filters-wrapper-frontend.js 6 kB
build/all-products-frontend.js 26.5 kB
build/attribute-filter-frontend.js 22.5 kB
build/attribute-filter-wrapper-frontend.js 7.07 kB
build/attribute-filter.js 12.2 kB
build/blocks-checkout.js 17.5 kB
build/cart-blocks/cart-accepted-payment-methods-frontend.js 1.38 kB
build/cart-blocks/cart-cross-sells-frontend.js 253 B
build/cart-blocks/cart-cross-sells-products--product-add-to-cart-frontend.js 5.63 kB
build/cart-blocks/cart-express-payment--checkout-blocks/express-payment-frontend.js 5.1 kB
build/cart-blocks/cart-express-payment-frontend.js 784 B
build/cart-blocks/cart-items-frontend.js 299 B
build/cart-blocks/cart-line-items--mini-cart-contents-block/products-table-frontend.js 5.31 kB
build/cart-blocks/cart-line-items-frontend.js 1.07 kB
build/cart-blocks/cart-order-summary-frontend.js 1.11 kB
build/cart-blocks/cart-totals-frontend.js 320 B
build/cart-blocks/empty-cart-frontend.js 346 B
build/cart-blocks/filled-cart-frontend.js 782 B
build/cart-blocks/order-summary-coupon-form-frontend.js 2.73 kB
build/cart-blocks/order-summary-discount-frontend.js 2.16 kB
build/cart-blocks/order-summary-fee-frontend.js 272 B
build/cart-blocks/order-summary-heading-frontend.js 456 B
build/cart-blocks/order-summary-shipping--checkout-blocks/order-summary-shipping-frontend.js 6.61 kB
build/cart-blocks/order-summary-shipping-frontend.js 428 B
build/cart-blocks/order-summary-subtotal-frontend.js 273 B
build/cart-blocks/order-summary-taxes-frontend.js 433 B
build/cart-blocks/proceed-to-checkout-frontend.js 1.19 kB
build/checkout-blocks/actions-frontend.js 1.79 kB
build/checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 4.98 kB
build/checkout-blocks/billing-address-frontend.js 953 B
build/checkout-blocks/contact-information-frontend.js 3.02 kB
build/checkout-blocks/express-payment-frontend.js 1.13 kB
build/checkout-blocks/fields-frontend.js 343 B
build/checkout-blocks/order-note-frontend.js 1.14 kB
build/checkout-blocks/order-summary-cart-items-frontend.js 3.67 kB
build/checkout-blocks/order-summary-coupon-form-frontend.js 2.88 kB
build/checkout-blocks/order-summary-discount-frontend.js 2.28 kB
build/checkout-blocks/order-summary-fee-frontend.js 275 B
build/checkout-blocks/order-summary-frontend.js 1.11 kB
build/checkout-blocks/order-summary-shipping-frontend.js 603 B
build/checkout-blocks/order-summary-subtotal-frontend.js 273 B
build/checkout-blocks/order-summary-taxes-frontend.js 433 B
build/checkout-blocks/payment-frontend.js 7.86 kB
build/checkout-blocks/shipping-address-frontend.js 1.06 kB
build/checkout-blocks/shipping-methods-frontend.js 4.89 kB
build/checkout-blocks/terms-frontend.js 1.64 kB
build/checkout-blocks/totals-frontend.js 323 B
build/checkout-frontend.js 56.7 kB
build/filter-wrapper-frontend.js 13.8 kB
build/filter-wrapper.js 2.88 kB
build/general-style-rtl.css 1.29 kB
build/general-style.css 1.29 kB
build/mini-cart-component-frontend.js 20 kB
build/mini-cart-contents-block/empty-cart-frontend.js 367 B
build/mini-cart-contents-block/filled-cart-frontend.js 230 B
build/mini-cart-contents-block/footer-frontend.js 2.97 kB
build/mini-cart-contents-block/items-frontend.js 236 B
build/mini-cart-contents-block/products-table-frontend.js 590 B
build/mini-cart-contents-block/shopping-button-frontend.js 287 B
build/mini-cart-contents-block/title-frontend.js 367 B
build/mini-cart-frontend.js 1.82 kB
build/mini-cart.js 4.28 kB
build/price-filter-frontend.js 13.6 kB
build/price-filter-wrapper-frontend.js 6.95 kB
build/price-filter.js 8.31 kB
build/price-format.js 1.19 kB
build/product-add-to-cart--product-button--product-category-list--product-image--product-price--product-r--a0326d00.js 228 B
build/product-add-to-cart-frontend.js 1.25 kB
build/product-best-sellers.js 7.7 kB
build/product-button--product-category-list--product-image--product-price--product-rating--product-sale-b--e17c7c01.js 431 B
build/product-button-frontend.js 1.89 kB
build/product-categories.js 2.36 kB
build/product-category-list-frontend.js 886 B
build/product-category-list.js 502 B
build/product-category.js 8.69 kB
build/product-image-frontend.js 1.92 kB
build/product-new.js 7.69 kB
build/product-on-sale.js 8.02 kB
build/product-price-frontend.js 1.92 kB
build/product-sale-badge-frontend.js 1.15 kB
build/product-search.js 2.61 kB
build/product-sku-frontend.js 380 B
build/product-sku.js 380 B
build/product-stock-indicator-frontend.js 1 kB
build/product-stock-indicator.js 624 B
build/product-summary-frontend.js 1.29 kB
build/product-summary.js 920 B
build/product-tag-list-frontend.js 879 B
build/product-tag-list.js 497 B
build/product-title-frontend.js 1.34 kB
build/product-top-rated.js 7.93 kB
build/products-by-attribute.js 8.61 kB
build/rating-filter-frontend.js 7.09 kB
build/rating-filter-wrapper-frontend.js 5.35 kB
build/reviews-frontend.js 7.01 kB
build/single-product-frontend.js 32.3 kB
build/stock-filter-frontend.js 7.73 kB
build/stock-filter-wrapper-frontend.js 5.99 kB
build/vendors--attribute-filter-wrapper--mini-cart-contents-block/footer-frontend.js 6.86 kB
build/vendors--attribute-filter-wrapper-frontend.js 8.22 kB
build/vendors--cart-blocks/cart-cross-sells-products--cart-blocks/cart-line-items--cart-blocks/cart-order--671ca56f-frontend.js 5.26 kB
build/vendors--cart-blocks/cart-cross-sells-products--cart-blocks/order-summary-shipping--checkout-blocks--18f9376a-frontend.js 19.1 kB
build/vendors--cart-blocks/cart-cross-sells-products--product-add-to-cart-frontend.js 7.53 kB
build/vendors--cart-blocks/cart-line-items--checkout-blocks/order-summary-cart-items--mini-cart-contents---233ab542-frontend.js 3.14 kB
build/vendors--cart-blocks/order-summary-shipping--checkout-blocks/billing-address--checkout-blocks/order--5b8feb0b-frontend.js 4.85 kB
build/wc-blocks-data.js 18.3 kB
build/wc-blocks-editor-style-rtl.css 5.24 kB
build/wc-blocks-editor-style.css 5.24 kB
build/wc-blocks-google-analytics.js 1.56 kB
build/wc-blocks-middleware.js 932 B
build/wc-blocks-registry.js 2.93 kB
build/wc-blocks-shared-context.js 1.52 kB
build/wc-blocks-shared-hocs.js 1.72 kB
build/wc-blocks-style-rtl.css 24.2 kB
build/wc-blocks-style.css 24.2 kB
build/wc-blocks-vendors-style-rtl.css 1.95 kB
build/wc-blocks-vendors-style.css 1.95 kB
build/wc-blocks.js 2.62 kB
build/wc-payment-method-bacs.js 816 B
build/wc-payment-method-cheque.js 811 B
build/wc-payment-method-cod.js 909 B
build/wc-payment-method-paypal.js 837 B
build/wc-settings.js 2.6 kB

compressed-size-action

@danielwrobert danielwrobert force-pushed the add/atomic-rating-pq-support branch from 00e05be to 58b7473 Compare October 10, 2022 18:19
@danielwrobert danielwrobert marked this pull request as ready for review October 10, 2022 19:54
@rubikuserbot rubikuserbot requested review from a team and sunyatasattva and removed request for a team October 10, 2022 19:54
@danielwrobert danielwrobert requested review from gigitux and sunyatasattva and removed request for sunyatasattva and gigitux October 10, 2022 22:40
@danielwrobert danielwrobert marked this pull request as draft October 11, 2022 02:11
@danielwrobert danielwrobert force-pushed the add/atomic-rating-pq-support branch 3 times, most recently from 2de7f19 to 62841a3 Compare October 11, 2022 16:31
@danielwrobert danielwrobert marked this pull request as ready for review October 11, 2022 16:39
@rubikuserbot rubikuserbot requested a review from a team October 11, 2022 16:39
@danielwrobert danielwrobert requested a review from gigitux October 11, 2022 16:39
@rubikuserbot rubikuserbot requested review from Aljullu and removed request for a team October 11, 2022 16:39
@danielwrobert
Copy link
Contributor Author

danielwrobert commented Oct 11, 2022

I forgot about converting this to TS but I'll plan to also handle this in a follow-up along with the Price block if that's ok.

Actually, I've changed my mind. I'm going to convert now because I will add a types interface for the context that we'll be able to use on the rest of the blocks.

Going forward, however, I think it will be best to approach the TS conversion in separate PRs due to the potential "rabbit-hole" of necessary adjustments to other parts of the code base.

@danielwrobert danielwrobert force-pushed the add/atomic-rating-pq-support branch from 62841a3 to d782eca Compare October 11, 2022 17:43
@sunyatasattva
Copy link
Contributor

Hello @danielwrobert! Thanks for the work you're making here.

I'm failing to properly test this PR, I'm not sure what I'm doing wrong.

  1. I can't see the Product Rating in the inserter when I try to add it manually to my Product Query block.
  2. If I follow your test steps, it appears on the editor but it asks me to choose a product. Even when I do that, it doesn't show up anymore.

Screenshot 2022-10-12 at 06 26 04

Is there something obvious that I might be missing?

FWIW I feel the test instructions should also include adding the block via the inserter. Also:

Note that this block has a different behavior when it is used in the Product Query block as respect when it is used in All Products. We will address this issue in the next iteration #6949.

Seem to refer to the Add to Cart block. Is there a different behavior for this block as well?

@github-actions
Copy link
Contributor

This is a workaround until we can confirm the correct typ...

This is a workaround until we can confirm the correct type from ProductResponseItem.


// TODO: This is a workaround until we can confirm the correct type from ProductResponseItem.
product: Record< string, unknown > & {
review_count: number | string;
}
) => {
const count = isNumber( product.review_count )
? product.review_count
: parseInt( product.review_count, 10 );
return Number.isFinite( count ) && count > 0 ? count : 0;
};
/**

🚀 This comment was generated by the automations bot based on a todo comment in 1f583eb in #7352. cc @danielwrobert

@github-actions
Copy link
Contributor

github-actions bot commented Oct 14, 2022

Script Dependencies Report

There is no changed script dependency between this branch and trunk.

This comment was automatically generated by the ./github/compare-assets action.

@sunyatasattva sunyatasattva added the block-type: product-query Issues related to/affecting all product-query variations. label Oct 14, 2022

/**
* Internal dependencies
*/
import Block from './block';
import withProductSelector from '../shared/with-product-selector';
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
import { Attributes } from './types';
import { ProductQueryContext as Context } from '../../../../blocks/product-query/types';
Copy link
Contributor Author

@danielwrobert danielwrobert Oct 14, 2022

Choose a reason for hiding this comment

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

Just noting that I realize this is not an ideal import approach by using the relative path. However, moving this to an alias includes updating/cleaning up the tsconfig(s) and webpack config - it would also be good to test any potential impact that might raise.

In the interest of keeping this PR scoped to supporting the PQ block, I propose addressing this in a follow-up item.

Copy link
Contributor

Choose a reason for hiding this comment

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

What potential impacts do you see here? I agree with you on keeping the PR scoped, but also to not unnecessarily add any other small tasks which could be solved with little effort.

I see that perhaps you mean that if we introduce a @woocommerce/blocks alias path and migrate all our paths it could cause some unintended consequences, is that correct? While I'm not seeing too many unintended consequences for that either, we can add the alias and use it only here (and from now on) to keep this PR correctly scoped.

It's, in a sense, the same principle as not necessarily removing all our TS errors, but not add any further TS error. So here, we don't have to fix all relative paths, just not add wood to the fire, so to speak. And then, like you say, make another refactoring issue and PR to fix the older relative paths (or fix them as they come into play).

So, these are my 2c. But I'm approving the PR because it's such a small thing and if you disagree with it, I'm also ok with that.

Copy link
Contributor

@sunyatasattva sunyatasattva left a comment

Choose a reason for hiding this comment

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

Still have a small comment about the relative path, but it's not mandatory if you don't want to fix it in this PR, so I am approving.

@github-actions github-actions bot added this to the 8.9.0 milestone Oct 27, 2022
@danielwrobert danielwrobert force-pushed the add/atomic-rating-pq-support branch from 9eab7cb to b734e8b Compare October 29, 2022 02:07
Set up the block for PQ support and add necessary adjustments for the
editor. Will address dynamic save functionality in a following commit.
As a workaround, added a general Record type but left a TODO to revisit
the proper object, as there is a mismatch in the shape of the default
object property types and the actual types.
Allows for the ability to add the rating block from in the inserter
(as long as it's an inner block of the listed parents in the config).
Also disables the placeholder product selector from being rendered
unnecessarily (i.e., when the context ID is present).
Reassign parent array to ancestor array which allows for blocks to be
included with more flexibility - i.e., added within groups that are
children of the ancestor block.
Some adjustments to utilize types that we already have available, along
with some syntax adjustments and more sensible import tweaks.
Instead of using the generic Record, we can utilize the
ProductResponseItem interface and set an omission for the average_rating
property until that is corrected to properly reflect the API response.
Allows us to use exports from the blocks dir as "external" imports. This
way we do not need to write long, relative import paths (which can be
fragile in the long run).
@danielwrobert danielwrobert force-pushed the add/atomic-rating-pq-support branch from a6fb26c to 72e87b3 Compare October 31, 2022 18:01
@danielwrobert danielwrobert merged commit 59d7f24 into trunk Oct 31, 2022
@danielwrobert danielwrobert deleted the add/atomic-rating-pq-support branch October 31, 2022 18:56
senadir pushed a commit to senadir/woocommerce-blocks that referenced this pull request Nov 12, 2022
* Add PQ support for client-side.

Set up the block for PQ support and add necessary adjustments for the
editor. Will address dynamic save functionality in a following commit.

* Add dynamic render function for PQ support.

* Add dynamic render callback for SSR.

* Remove client-side Save function.

* Add PQ Context interface to shared type defs.

* Convert all block JS files to TS.

* Remove commented import from block file.

* Add typecasting to block function params.

As a workaround, added a general Record type but left a TODO to revisit
the proper object, as there is a mismatch in the shape of the default
object property types and the actual types.

* Update inserter behavior.

Allows for the ability to add the rating block from in the inserter
(as long as it's an inner block of the listed parents in the config).
Also disables the placeholder product selector from being rendered
unnecessarily (i.e., when the context ID is present).

* Update parent inner blocks config.

Reassign parent array to ancestor array which allows for blocks to be
included with more flexibility - i.e., added within groups that are
children of the ancestor block.

* Add productID to rating Attributes interface.

* TS type casting and import adustments.

Some adjustments to utilize types that we already have available, along
with some syntax adjustments and more sensible import tweaks.

* Update type-casting to use ProductResponseItem

Instead of using the generic Record, we can utilize the
ProductResponseItem interface and set an omission for the average_rating
property until that is corrected to properly reflect the API response.

* Add alias to blocks dir for imports.

Allows us to use exports from the blocks dir as "external" imports. This
way we do not need to write long, relative import paths (which can be
fragile in the long run).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block-type: product elements Issues related to Product Element blocks. block-type: product-query Issues related to/affecting all product-query variations. skip-changelog PRs that you don't want to appear in the changelog. type: refactor The issue/PR is related to refactoring.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Product Query: [Atomic Block] Convert Rating Block
4 participants