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

Conversation

gigitux
Copy link
Contributor

@gigitux gigitux commented Jul 26, 2023

This PR implements the animation discussed into https://github.com/woocommerce/woocommerce-blocks/discussions/10153#discussioncomment-6475505.

Cached Page Not Cached Page
Screen.Capture.on.2023-07-26.at.16-22-00.mp4
Screen.Capture.on.2023-07-28.at.12-06-26.mp4

Testing

User Facing Testing

  1. Install the plugin WP Optimize.
  2. Ensure that your cart is empty.
  3. Ensure that you have the Mini Cart block in the header.
  4. Enable the cache.
  5. Visit the page with the Products block.
  6. Refresh the page.
  7. Add a product.
  8. Refresh the page.
  9. Ensure that the counter is updated with a smoother animation.
  10. Open the Mini Cart block and change the quantity of the product in the cart.
  11. Ensure that the counter inside the button is updated with a smoother animation.
  12. Disable the plugin.
  13. Visit the page with the Products block.
  14. Open the Mini Cart block and change the quantity of the product in the cart.
  15. Ensure that the counter inside the button is updated with a smoother animation.
  16. Ensure that there isn't any regression.

@gigitux gigitux added type: enhancement The issue is a request for an enhancement. skip-changelog PRs that you don't want to appear in the changelog. labels Jul 26, 2023
@gigitux gigitux requested review from luisherranz and Aljullu July 26, 2023 08:29
@gigitux gigitux self-assigned this Jul 26, 2023
@woocommercebot woocommercebot requested a review from a team July 26, 2023 08:30
return ref.textContent;
}

ref.textContent = '';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is necessary otherwise, the returned string (line 97) is appended.

@gigitux gigitux changed the title ProductButton: Implement animation ProductButton: Add animation Jul 26, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jul 26, 2023

The release ZIP for this PR is accessible via:

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

Script Dependencies Report

The compare-assets action has detected some changed script dependencies between this branch and trunk. Please review and confirm the following are correct before merging.

Script Handle Added Removed
wc-blocks.js wp-block-editor, wp-components, wp-core-data, wp-data, wp-edit-site, wp-plugins ⚠️
product-gallery.js wp-components, wp-i18n, wp-primitives 🎉
product-collection.js wp-hooks 🎉

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

TypeScript Errors Report

  • Files with errors: 465
  • Total errors: 2241

⚠️ ⚠️ This PR introduces new TS errors on 11 files:

assets/js/atomic/blocks/product-elements/button/frontend.tsx

assets/js/atomic/blocks/product-elements/rating-stars/index.tsx

assets/js/atomic/blocks/product-elements/rating-stars/support.ts

assets/js/blocks/classic-template/test/utils.ts

assets/js/blocks/mini-cart/edit.tsx

assets/js/blocks/mini-cart/index.tsx

assets/js/blocks/mini-cart/mini-cart-contents/edit.tsx

assets/js/blocks/product-gallery/edit.tsx

assets/js/hocs/test/with-searched-products.js

assets/js/interactivity/store.js

assets/js/interactivity/utils.js

comments-aggregator

@gigitux gigitux force-pushed the after-load-callback-animation branch from e5ff92b to 1ec122b Compare July 26, 2023 08:36
@github-actions
Copy link
Contributor

github-actions bot commented Jul 26, 2023

Size Change: +878 B (0%)

Total Size: 1.37 MB

Filename Size Change
build/all-products-rtl.css 4.41 kB +84 B (+2%)
build/all-products.css 4.41 kB +84 B (+2%)
build/cart-rtl.css 9.81 kB +117 B (+1%)
build/cart.css 9.8 kB +117 B (+1%)
build/product-button-interactivity-frontend.js 9.48 kB +247 B (+3%)
build/product-button-rtl.css 1.15 kB +115 B (+11%) ⚠️
build/product-button.css 1.15 kB +114 B (+11%) ⚠️
ℹ️ View Unchanged
Filename Size
build/active-filters-frontend.js 8.71 kB
build/active-filters-rtl.css 2.02 kB
build/active-filters-wrapper--mini-cart-contents-block/cart-button--mini-cart-contents-block/checkout-but--3d5b804b-style.js 958 B
build/active-filters-wrapper-frontend.js 7.64 kB
build/active-filters-wrapper-rtl.css 1.88 kB
build/active-filters-wrapper.css 1.88 kB
build/active-filters.css 2.02 kB
build/active-filters.js 7.59 kB
build/all-products-frontend.js 12.2 kB
build/all-products.js 41.2 kB
build/all-reviews-rtl.css 1.84 kB
build/all-reviews.css 1.84 kB
build/all-reviews.js 7.86 kB
build/attribute-filter-frontend.js 23.1 kB
build/attribute-filter-rtl.css 4.19 kB
build/attribute-filter-wrapper-frontend.js 7.79 kB
build/attribute-filter-wrapper-rtl.css 4.06 kB
build/attribute-filter-wrapper.css 4.06 kB
build/attribute-filter.css 4.19 kB
build/attribute-filter.js 13.3 kB
build/blocks-checkout.js 35.1 kB
build/breadcrumbs-rtl.css 253 B
build/breadcrumbs.css 253 B
build/breadcrumbs.js 2.14 kB
build/cart-blocks/cart-accepted-payment-methods-frontend.js 1.38 kB
build/cart-blocks/cart-accepted-payment-methods-style.js 137 B
build/cart-blocks/cart-cross-sells-frontend.js 253 B
build/cart-blocks/cart-cross-sells-products--product-price-frontend.js 2.91 kB
build/cart-blocks/cart-cross-sells-products-frontend.js 3.84 kB
build/cart-blocks/cart-cross-sells-products-style.js 137 B
build/cart-blocks/cart-cross-sells-style.js 253 B
build/cart-blocks/cart-express-payment--checkout-blocks/express-payment-frontend.js 5.16 kB
build/cart-blocks/cart-express-payment-frontend.js 717 B
build/cart-blocks/cart-express-payment-style.js 137 B
build/cart-blocks/cart-items-frontend.js 300 B
build/cart-blocks/cart-items-style.js 229 B
build/cart-blocks/cart-line-items--mini-cart-contents-block/products-table-frontend.js 5.47 kB
build/cart-blocks/cart-line-items-frontend.js 1.07 kB
build/cart-blocks/cart-line-items-style.js 137 B
build/cart-blocks/cart-order-summary-frontend.js 1.27 kB
build/cart-blocks/cart-order-summary-style.js 318 B
build/cart-blocks/cart-totals-frontend.js 308 B
build/cart-blocks/cart-totals-style.js 239 B
build/cart-blocks/empty-cart-frontend.js 346 B
build/cart-blocks/empty-cart-style.js 340 B
build/cart-blocks/filled-cart-frontend.js 655 B
build/cart-blocks/filled-cart-style.js 312 B
build/cart-blocks/order-summary-coupon-form-frontend.js 1.63 kB
build/cart-blocks/order-summary-coupon-form-style.js 136 B
build/cart-blocks/order-summary-discount-frontend.js 2.12 kB
build/cart-blocks/order-summary-discount-style.js 137 B
build/cart-blocks/order-summary-fee-frontend.js 273 B
build/cart-blocks/order-summary-fee-style.js 138 B
build/cart-blocks/order-summary-heading-frontend.js 332 B
build/cart-blocks/order-summary-heading-style.js 335 B
build/cart-blocks/order-summary-shipping-frontend.js 17 kB
build/cart-blocks/order-summary-shipping-style.js 178 B
build/cart-blocks/order-summary-subtotal-frontend.js 274 B
build/cart-blocks/order-summary-subtotal-style.js 138 B
build/cart-blocks/order-summary-taxes-frontend.js 432 B
build/cart-blocks/order-summary-taxes-style.js 177 B
build/cart-blocks/proceed-to-checkout-frontend.js 1.43 kB
build/cart-blocks/proceed-to-checkout-style.js 1.09 kB
build/cart-frontend.js 29.9 kB
build/cart.js 45.4 kB
build/catalog-sorting-rtl.css 277 B
build/catalog-sorting.css 276 B
build/catalog-sorting.js 1.71 kB
build/checkout-blocks/actions--checkout-blocks/terms-style.js 487 B
build/checkout-blocks/actions-frontend.js 1.88 kB
build/checkout-blocks/actions-style.js 685 B
build/checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 4.7 kB
build/checkout-blocks/billing-address-frontend.js 1.18 kB
build/checkout-blocks/billing-address-style.js 530 B
build/checkout-blocks/contact-information-frontend.js 2.05 kB
build/checkout-blocks/contact-information-style.js 609 B
build/checkout-blocks/express-payment-frontend.js 1.13 kB
build/checkout-blocks/fields-frontend.js 318 B
build/checkout-blocks/fields-style.js 261 B
build/checkout-blocks/order-note-frontend.js 1.13 kB
build/checkout-blocks/order-summary-cart-items-frontend.js 3.76 kB
build/checkout-blocks/order-summary-cart-items-style.js 138 B
build/checkout-blocks/order-summary-coupon-form-frontend.js 1.79 kB
build/checkout-blocks/order-summary-coupon-form-style.js 137 B
build/checkout-blocks/order-summary-discount-frontend.js 2.29 kB
build/checkout-blocks/order-summary-discount-style.js 137 B
build/checkout-blocks/order-summary-fee-frontend.js 276 B
build/checkout-blocks/order-summary-fee-style.js 137 B
build/checkout-blocks/order-summary-frontend.js 1.28 kB
build/checkout-blocks/order-summary-shipping-frontend.js 17 kB
build/checkout-blocks/order-summary-shipping-style.js 137 B
build/checkout-blocks/order-summary-style.js 319 B
build/checkout-blocks/order-summary-subtotal-frontend.js 274 B
build/checkout-blocks/order-summary-subtotal-style.js 136 B
build/checkout-blocks/order-summary-taxes-frontend.js 432 B
build/checkout-blocks/order-summary-taxes-style.js 177 B
build/checkout-blocks/payment-frontend.js 9.29 kB
build/checkout-blocks/payment-style.js 461 B
build/checkout-blocks/pickup-options-frontend.js 4.84 kB
build/checkout-blocks/pickup-options-style.js 443 B
build/checkout-blocks/shipping-address-frontend.js 1.17 kB
build/checkout-blocks/shipping-address-style.js 476 B
build/checkout-blocks/shipping-method-frontend.js 2.63 kB
build/checkout-blocks/shipping-method-style.js 1.37 kB
build/checkout-blocks/shipping-methods-frontend.js 6.4 kB
build/checkout-blocks/shipping-methods-style.js 418 B
build/checkout-blocks/terms-frontend.js 1.55 kB
build/checkout-blocks/terms-style.js 675 B
build/checkout-blocks/totals-frontend.js 349 B
build/checkout-blocks/totals-style.js 284 B
build/checkout-frontend.js 31.8 kB
build/checkout-rtl.css 9.23 kB
build/checkout.css 9.22 kB
build/checkout.js 48 kB
build/customer-account-rtl.css 406 B
build/customer-account.css 406 B
build/customer-account.js 3.19 kB
build/featured-category-rtl.css 986 B
build/featured-category.css 987 B
build/featured-category.js 15.1 kB
build/featured-product-rtl.css 1.03 kB
build/featured-product.css 1.03 kB
build/featured-product.js 15.3 kB
build/filter-wrapper-frontend.js 14.5 kB
build/filter-wrapper-rtl.css 399 B
build/filter-wrapper.css 397 B
build/filter-wrapper.js 2.4 kB
build/handpicked-products.js 8.08 kB
build/legacy-template-rtl.css 258 B
build/legacy-template.css 257 B
build/legacy-template.js 8.92 kB
build/mini-cart-component-frontend.js 30.7 kB
build/mini-cart-contents-block/cart-button--mini-cart-contents-block/checkout-button--mini-cart-contents---358acf4e-style.js 293 B
build/mini-cart-contents-block/cart-button-frontend.js 1.73 kB
build/mini-cart-contents-block/cart-button-style.js 386 B
build/mini-cart-contents-block/checkout-button-frontend.js 1.81 kB
build/mini-cart-contents-block/checkout-button-style.js 468 B
build/mini-cart-contents-block/empty-cart-frontend.js 360 B
build/mini-cart-contents-block/empty-cart-style.js 362 B
build/mini-cart-contents-block/filled-cart-frontend.js 268 B
build/mini-cart-contents-block/filled-cart-style.js 268 B
build/mini-cart-contents-block/footer-frontend.js 3.82 kB
build/mini-cart-contents-block/footer-rtl.css 419 B
build/mini-cart-contents-block/footer-style.js 2.41 kB
build/mini-cart-contents-block/footer.css 418 B
build/mini-cart-contents-block/items-frontend.js 237 B
build/mini-cart-contents-block/items-style.js 237 B
build/mini-cart-contents-block/products-table--product-summary-style.js 737 B
build/mini-cart-contents-block/products-table-frontend.js 589 B
build/mini-cart-contents-block/products-table-rtl.css 2.19 kB
build/mini-cart-contents-block/products-table-style.js 5.1 kB
build/mini-cart-contents-block/products-table.css 2.18 kB
build/mini-cart-contents-block/shopping-button-frontend.js 529 B
build/mini-cart-contents-block/shopping-button-style.js 392 B
build/mini-cart-contents-block/title-frontend.js 1.89 kB
build/mini-cart-contents-block/title-items-counter-frontend.js 1.59 kB
build/mini-cart-contents-block/title-items-counter-style.js 300 B
build/mini-cart-contents-block/title-label-frontend.js 1.53 kB
build/mini-cart-contents-block/title-label-style.js 301 B
build/mini-cart-contents-block/title-style.js 443 B
build/mini-cart-contents-rtl.css 2.73 kB
build/mini-cart-contents.css 2.73 kB
build/mini-cart-contents.js 18.1 kB
build/mini-cart-frontend.js 2.84 kB
build/mini-cart-rtl.css 2.61 kB
build/mini-cart.css 2.61 kB
build/mini-cart.js 5.93 kB
build/packages-style-rtl.css 3.59 kB
build/packages-style.css 3.59 kB
build/price-filter-frontend.js 14.7 kB
build/price-filter-rtl.css 2.71 kB
build/price-filter-wrapper-frontend.js 6.86 kB
build/price-filter-wrapper-rtl.css 2.56 kB
build/price-filter-wrapper.css 2.56 kB
build/price-filter.css 2.7 kB
build/price-filter.js 8.66 kB
build/price-format.js 1.19 kB
build/product-add-to-cart--product-button--product-image--product-price--product-rating--product-rating-s--92e2d51d.js 272 B
build/product-add-to-cart--product-button--product-image--product-rating--product-rating-stars--product-title.js 151 B
build/product-add-to-cart-frontend.js 6.52 kB
build/product-add-to-cart-rtl.css 1.39 kB
build/product-add-to-cart.css 1.4 kB
build/product-add-to-cart.js 8.84 kB
build/product-best-sellers.js 8.43 kB
build/product-button--product-image--product-price--product-rating--product-rating-stars--product-sale-ba--4e6a8b3c.js 954 B
build/product-button-frontend.js 2.66 kB
build/product-button.js 3.97 kB
build/product-categories-rtl.css 671 B
build/product-categories.css 670 B
build/product-categories.js 2.71 kB
build/product-category.js 9.43 kB
build/product-collection.js 12.9 kB
build/product-details-rtl.css 413 B
build/product-details.css 410 B
build/product-gallery-large-image-rtl.css 314 B
build/product-gallery-large-image.css 313 B
build/product-gallery-large-image.js 2.01 kB
build/product-gallery.js 2.3 kB
build/product-image-frontend.js 2.71 kB
build/product-image-gallery-rtl.css 322 B
build/product-image-gallery.css 322 B
build/product-image-rtl.css 951 B
build/product-image.css 949 B
build/product-image.js 4.21 kB
build/product-new.js 8.72 kB
build/product-on-sale.js 8.73 kB
build/product-price-frontend.js 231 B
build/product-price-rtl.css 696 B
build/product-price.css 695 B
build/product-price.js 1.67 kB
build/product-query-rtl.css 367 B
build/product-query.css 365 B
build/product-query.js 12 kB
build/product-rating-frontend.js 2.35 kB
build/product-rating-rtl.css 262 B
build/product-rating-stars-frontend.js 2.24 kB
build/product-rating-stars-rtl.css 904 B
build/product-rating-stars.css 906 B
build/product-rating-stars.js 939 B
build/product-rating.css 262 B
build/product-rating.js 1.04 kB
build/product-results-count-rtl.css 248 B
build/product-results-count.css 247 B
build/product-results-count.js 1.66 kB
build/product-reviews-rtl.css 474 B
build/product-reviews.css 473 B
build/product-sale-badge-frontend.js 1.8 kB
build/product-sale-badge-rtl.css 392 B
build/product-sale-badge.css 389 B
build/product-sale-badge.js 667 B
build/product-search-rtl.css 435 B
build/product-search.css 434 B
build/product-search.js 2.63 kB
build/product-sku-frontend.js 1.85 kB
build/product-sku-rtl.css 258 B
build/product-sku.css 258 B
build/product-sku.js 535 B
build/product-stock-indicator-frontend.js 2.04 kB
build/product-stock-indicator-rtl.css 250 B
build/product-stock-indicator.css 250 B
build/product-stock-indicator.js 731 B
build/product-summary-frontend.js 2.26 kB
build/product-summary-rtl.css 571 B
build/product-summary.css 572 B
build/product-summary.js 1 kB
build/product-tag.js 8.92 kB
build/product-template-rtl.css 439 B
build/product-template.css 437 B
build/product-template.js 3.36 kB
build/product-title-frontend.js 2.22 kB
build/product-title-rtl.css 718 B
build/product-title.css 719 B
build/product-title.js 3.66 kB
build/product-top-rated.js 8.98 kB
build/products-by-attribute.js 9.77 kB
build/rating-filter-frontend.js 21.4 kB
build/rating-filter-rtl.css 4.24 kB
build/rating-filter-wrapper-frontend.js 6.29 kB
build/rating-filter-wrapper-rtl.css 4.11 kB
build/rating-filter-wrapper.css 4.11 kB
build/rating-filter.css 4.24 kB
build/rating-filter.js 6.99 kB
build/reviews-by-category-rtl.css 1.84 kB
build/reviews-by-category.css 1.84 kB
build/reviews-by-category.js 12.2 kB
build/reviews-by-product-rtl.css 1.84 kB
build/reviews-by-product.css 1.84 kB
build/reviews-by-product.js 13.3 kB
build/reviews-frontend.js 7.17 kB
build/single-product-rtl.css 399 B
build/single-product.css 397 B
build/single-product.js 11.2 kB
build/stock-filter-frontend.js 21.7 kB
build/stock-filter-rtl.css 4.06 kB
build/stock-filter-wrapper-frontend.js 6.55 kB
build/stock-filter-wrapper-rtl.css 3.93 kB
build/stock-filter-wrapper.css 3.93 kB
build/stock-filter.css 4.06 kB
build/stock-filter.js 7.73 kB
build/store-notices.js 1.69 kB
build/vendors--active-filters-wrapper--attribute-filter-wrapper--mini-cart-contents-block/cart-button--mi--6436fd83-style.js 605 B
build/vendors--attribute-filter-wrapper--cart-blocks/order-summary-coupon-form--cart-blocks/order-summary--48e1e4bb-frontend.js 6.82 kB
build/vendors--attribute-filter-wrapper--cart-blocks/order-summary-shipping--checkout-blocks/billing-addr--d9f38f9d-frontend.js 4.2 kB
build/vendors--attribute-filter-wrapper-frontend.js 5.11 kB
build/vendors--cart-blocks/cart-cross-sells-products--cart-blocks/cart-line-items--cart-blocks/cart-order--3c5fe802-frontend.js 5.26 kB
build/vendors--cart-blocks/cart-line-items--checkout-blocks/order-summary-cart-items--mini-cart-contents---233ab542-frontend.js 3.57 kB
build/vendors--cart-blocks/order-summary-shipping--checkout-blocks/billing-address--checkout-blocks/order--decc3dc6-frontend.js 19.4 kB
build/vendors--cart-blocks/proceed-to-checkout-style.js 179 B
build/vendors--checkout-blocks/pickup-options--checkout-blocks/shipping-methods-frontend.js 8.25 kB
build/vendors--checkout-blocks/shipping-method-frontend.js 12.4 kB
build/vendors--checkout-blocks/shipping-method-style.js 11.7 kB
build/vendors--mini-cart-contents-block/products-table--price-filter-wrapper--product-price-style.js 5.27 kB
build/vendors--mini-cart-contents-block/products-table-style.js 3.16 kB
build/vendors--price-filter-wrapper-frontend.js 2.2 kB
build/vendors--product-add-to-cart-frontend.js 7.25 kB
build/vendors--rating-filter-wrapper-frontend.js 5.11 kB
build/vendors--stock-filter-wrapper-frontend.js 5.11 kB
build/wc-blocks-data.js 22.4 kB
build/wc-blocks-editor-style-rtl.css 6.17 kB
build/wc-blocks-editor-style.css 6.17 kB
build/wc-blocks-google-analytics.js 1.56 kB
build/wc-blocks-middleware.js 934 B
build/wc-blocks-registry.js 3.15 kB
build/wc-blocks-rtl.css 2.54 kB
build/wc-blocks-shared-context.js 1.1 kB
build/wc-blocks-shared-hocs.js 1.75 kB
build/wc-blocks-vendors.js 65.3 kB
build/wc-blocks.css 2.54 kB
build/wc-blocks.js 3.75 kB
build/wc-interactivity.js 10.7 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
build/wc-shipping-method-pickup-location.js 30.4 kB

compressed-size-action

Copy link
Contributor

@Aljullu Aljullu left a comment

Choose a reason for hiding this comment

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

This is working great, really exciting to see how this is shaping up! I left some comments related to the code below, but overall it's looking good.

Comment on lines 66 to 74
span {

&.wc-block-scrollToUp {
animation: scrollToUp 0.1s linear 1 normal;
}
&.wc-block-scrollFromDown {
animation: scrollFromDown 0.1s linear 1 normal;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be wrapped with prefers-reduced-motion.

),
$span_button_directives = 'data-wc-text="selectors.woocommerce.addToCartText"';

if ( WC()->cart->is_empty() ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is causing me an error when saving posts. It looks like WC()->cart is null, so the is_empty() method doesn't exist. Can you reproduce as well?

imatge

@gigitux
Copy link
Contributor Author

gigitux commented Jul 28, 2023

Thanks, @Aljullu, I had a pair session with @luisherranz that explained my wrong approach. Thanks to his help, I refactored this code and implemented the animation when the quantity of the product is updated from the Mini Cart as well.

@gigitux gigitux requested a review from Aljullu July 28, 2023 10:40
Copy link
Contributor

@Aljullu Aljullu left a comment

Choose a reason for hiding this comment

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

As mentioned in the previous review, this is looking awesome, I'm very excited for this to be merged. 👏

I left some comments inline, but they are mostly about coding style, so feel free to disregard them if you don't agree.

Also, I noticed that using the woocommerce_add_to_cart_quantity filter doesn't work anymore. Steps to reproduce:

  1. In any PHP file, add this code:
// Function to set the quantity of items added to cart
function custom_add_to_cart_quantity( $quantity, $product_id ) {
	return 10;
}
add_filter( 'woocommerce_add_to_cart_quantity', 'custom_add_to_cart_quantity', 10, 2 );
  1. In a page with the Products block, add a product to the cart.
  2. Notice only one product is added, instead of 10 as specified in the filter from step 1.

Can you reproduce as well?

state,
context,
}: {
selecotrs: any;
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a typo here.

hasCartLoaded: ( { state }: { state: State } ) => {
return state.woocommerce.cart !== undefined;
},
numberOfItems: ( {
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that this is a function, I think it would be great to start its name with a verb, something like getNumberofItems(). What do you think?

Comment on lines 178 to 191
const isFreshLoad =
selectors.woocommerce.hasCartLoaded( store ) &&
context.woocommerce.initialNumberOfItems !==
selectors.woocommerce.numberOfItems( store ) &&
context.woocommerce.numberOfItems !==
selectors.woocommerce.numberOfItems( store );

const isFromCart =
selectors.woocommerce.numberOfItemsPrev( store ) !==
undefined &&
selectors.woocommerce.numberOfItemsPrev( store ) !==
selectors.woocommerce.numberOfItems( store ) &&
selectors.woocommerce.numberOfItems( store ) !==
context.woocommerce.numberOfItems;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: given that we are calling numberOfItems() several times, could we call it at the beginning of the function and save its value into a variable? Same with numberOfItemsPrev(). I feel it would make the code easier to read.


return product?.quantity || 0;
},
numberOfItemsPrev: ( {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would change this one to start with get too, so it's clear it's a function.

@gigitux gigitux force-pushed the after-load-callback-animation branch from e294c2e to c86c4e8 Compare July 28, 2023 14:27
@gigitux gigitux force-pushed the after-load-callback-animation branch from 28d935f to c6acd18 Compare July 28, 2023 14:40
@gigitux
Copy link
Contributor Author

gigitux commented Jul 28, 2023

As mentioned in the previous review, this is looking awesome, I'm very excited for this to be merged. 👏

I left some comments inline, but they are mostly about coding style, so feel free to disregard them if you don't agree.

Also, I noticed that using the woocommerce_add_to_cart_quantity filter doesn't work anymore. Steps to reproduce:

  1. In any PHP file, add this code:
// Function to set the quantity of items added to cart
function custom_add_to_cart_quantity( $quantity, $product_id ) {
	return 10;
}
add_filter( 'woocommerce_add_to_cart_quantity', 'custom_add_to_cart_quantity', 10, 2 );
  1. In a page with the Products block, add a product to the cart.
  2. Notice only one product is added, instead of 10 as specified in the filter from step 1.

Can you reproduce as well?

Nice catch! I added the support to the filter with c6acd18

@luisherranz
Copy link
Collaborator

Hey Luigi, I think the number of variables storing numberOfItems was getting out of hand so I tried a new approach based on this logic. It took me a while to figure out, but I think it's working great. I also introduced a wp-effect to simplify the logic that starts the animation. I forgot about wp-effect the other day when we looked at it.

I made a video to explain everything:

https://www.loom.com/share/19478a6e951442f09f5902d3fb20c68e

I still want to take a look at how to await the population of the store in the AddToCart action, but let me know what you think.

@gigitux
Copy link
Contributor Author

gigitux commented Jul 31, 2023

Hey Luigi, I think the number of variables storing numberOfItems was getting out of hand so I tried a new approach based on this logic. It took me a while to figure out, but I think it's working great. I also introduced a wp-effect to simplify the logic that starts the animation. I forgot about wp-effect the other day when we looked at it.

I made a video to explain everything:


https://www.loom.com/share/19478a6e951442f09f5902d3fb20c68e

I still want to take a look at how to await the population of the store in the AddToCart action, but let me know what you think.

Thanks for your work and your great explanation! I forget entirely about the wp-effect 😅

With 1333cc7 I re-added the added class support. Currently, the class added is only added if the user adds the product by clicking the button. I think that it makes sense replicate the same behavior, but feel free to give your feedback.

@gigitux
Copy link
Contributor Author

gigitux commented Jul 31, 2023

Regarding the other improvements that you mentioned:

  • using the scope.
  • having one source of truth, so waiting for the store to be updated in action.

Should we continue to work on it, or can we start to merge this and work on them with follow-up issues/PRs?

@luisherranz
Copy link
Collaborator

having one source of truth, so waiting for the store to be updated in action.

It works fine 🙂

https://www.loom.com/share/3dde7d89d1c8496fa8c6d1b4a2652f57

can we start to merge this and work on them with follow-up issues/PRs?

Yeah, let's merge this PR now. I'd like to take a look at the need for React and NoticeBanner, but we can do it in another PR.

@gigitux
Copy link
Contributor Author

gigitux commented Aug 2, 2023

Regarding the .added class I searched across the extensions and it seems that any extension uses the .added class in their logic. I think that for now, we can remove it, and if it is necessary, we can easily re-add it.

I removed the logic for the .added class with 49651c2.

@gigitux
Copy link
Contributor Author

gigitux commented Aug 2, 2023

If you think that everything is ready, could you approve the PR? @luisherranz 🙇

Copy link
Collaborator

@luisherranz luisherranz left a comment

Choose a reason for hiding this comment

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

Yes, let's merge this 🙂

@gigitux gigitux merged commit 01875cc into interactivity-api-after-load-callback Aug 2, 2023
@gigitux gigitux deleted the after-load-callback-animation branch August 2, 2023 16:26
gigitux added a commit that referenced this pull request Aug 3, 2023
…10338)

* show notice when there is an error

* Add initial implementation for store callbacks

* Run `afterLoad` callbacks after `init`

* Move cart state subscription to Product button

* Remove cart-store from Interactivity API internals

* Change callbacks with options and save only afterLoad callbacks

* ProductButton: Add animation (#10351)

* implement animation

* improve logic

* refactor logic

* refactor code

* address feedback about code style

* add support for woocommerce_add_to_cart_quantity

* Fix animation flickering

* Introduce wp-effect, reduce the amount of numberOfItem variables to 2 and consolidate animation status

* add support for added class

* Remove unnecessary selector

* Don't fetch cart if it was already fetched

* remove added class

---------

Co-authored-by: Luis Herranz <luisherranz@gmail.com>

---------

Co-authored-by: Luigi <gigitux@gmail.com>
Co-authored-by: Luis Herranz <luisherranz@gmail.com>
gigitux added a commit that referenced this pull request Aug 10, 2023
* Update Interactivity API JS files

* Disable TS checks in the Interactivity API for now

* Add new SSR files

* Replace wp_ prefixes with wc_ ones

* Replace wp- prefix with wc-

* Replace guternberg_ prefix with woocommerce_

* Remove file comments from Gutenberg

* Rename files with `wp` prefix

* Fix code to load Interactivity API php files

* Remove TODO comments

* Replace @WordPress with @woocommerce

* Update Webpack configuration

* Fix directive prefix

* Remove interactivity folder from tsconfig exclude

* Add client-side navigation meta tag code

* Remove unneeded blocks.php file

* Fix store tag id

* Register Interactivity API runtime script

* Fix Interactivity API runtime registering

* Remove all files related to directive processing in PHP

* Move json_encode to Store's render method

* WIP

* WIP

* WIP

* WIP

* Preserve previous context

* Ignore Minicart block on client-side navigation

* Refresh page on store updatRefresh page on store updatee

* Refactor logic

* Add console error when a path is missing

* fix PHP lint error

* WIP store

* use store approach

* update jest configuration

* restore Mini Cart changes

* move cart store subscription to interactivity package

* move interactivity flag

* format HTML

* move addToCartText to the context

* Load product-query stylesheet when rendering the Products block

* update sideEffects array

* fix catch

* rename moreThanOneItem to isThereMoreThanOneItem

* improve how scripts are enqueued

* update default value for the filter woocommerce_blocks_enable_interactivity_api

* Update assets/js/atomic/blocks/product-elements/button/block.json

Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com>

* Update assets/js/interactivity/cart/cart-store.ts

Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com>

* fix block.json

* remove updateStore function

* restore interactivity api changes

* import cart store

* show notice when there is an error

* add logic to dequeue script on classic themes and block themes

* imrpove logic about notice

* Interactivity API: add `afterLoad` callbacks to `store()` function (#10338)

* show notice when there is an error

* Add initial implementation for store callbacks

* Run `afterLoad` callbacks after `init`

* Move cart state subscription to Product button

* Remove cart-store from Interactivity API internals

* Change callbacks with options and save only afterLoad callbacks

* ProductButton: Add animation (#10351)

* implement animation

* improve logic

* refactor logic

* refactor code

* address feedback about code style

* add support for woocommerce_add_to_cart_quantity

* Fix animation flickering

* Introduce wp-effect, reduce the amount of numberOfItem variables to 2 and consolidate animation status

* add support for added class

* Remove unnecessary selector

* Don't fetch cart if it was already fetched

* remove added class

---------

Co-authored-by: Luis Herranz <luisherranz@gmail.com>

---------

Co-authored-by: Luigi <gigitux@gmail.com>
Co-authored-by: Luis Herranz <luisherranz@gmail.com>

* update deepsignal

* remove added class

* update deepsignal

* Interactivity API and Product Button: Add E2E tests (#10036)

* Add FrontendUtils class

* fix conflicts

* use locator

* restore click usage

* Product Button: Add E2E test

* fix util

* fix E2E tests

* remove comment

* Add E2E test to ensure that woocommerce_product_add_to_cart_text works

* update sideEffects array

* add zip and unzip as package

* fix wp-env configuration

* fix E2E test

* add report

* try now

* try now

* try now

* fix E2E test

* E2E: Add documentation for testing actions and filters. Fixes #10135 (#10206)

* update description

* fix label

* rename files

* make requestUtils private

* remove page.goto

* use toHaveCount

* use productsToDisplay variable

* fix E2E tests

* rename class utils

---------

Co-authored-by: Daniel Dudzic <daniel.dudzic@automattic.com>

---------

Co-authored-by: David Arenas <david.arenas@automattic.com>
Co-authored-by: Luis Herranz <luisherranz@gmail.com>
Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com>
Co-authored-by: Daniel Dudzic <daniel.dudzic@automattic.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
skip-changelog PRs that you don't want to appear in the changelog. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants