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

Add CartEventsContext to dispatch events in the Cart block #7694

Merged
merged 13 commits into from
Nov 18, 2022

Conversation

opr
Copy link
Contributor

@opr opr commented Nov 16, 2022

This PR adds a new context called CartEventsContext it currently has two functions as its value: onProceedToCheckout and dispatchOnProceedToCheckout

It uses the same pattern as the CheckoutEventsContext however I opted to create this new context to keep the concerns separated.

In this PR, we also call dispatchOnProceedToCheckout when the proceed to checkout button is clicked, and if any observers throw an error, or return an object with a type of error causing the process to stop and the user will not proceed to checkout.

The QuantitySelector component subscribes to this event, and if the quantity does not satisfy the validation criteria then it focuses the input, returns the error object and stops the cart -> checkout flow.

Two tests are also included in this PR, one to check the event is dispatched and correctly handled by observers when pressing the button, and one to check unsubscribing from observers works correctly.

This PR is based on remove/auto-quantity-update (#7606).

Accessibility

Other Checks

  • This PR adds/removes a feature flag & I've updated this doc.
  • This PR adds/removes an experimental interfaces and I've updated this doc.
  • I tagged two reviewers because this PR makes queries to the database or I think it might have some security impact.

Screenshots

https://www.loom.com/share/5445f3a871204aa7a6b89fbed6800889

Testing

Automated Tests

  • Changes in this PR are covered by Automated Tests.
    • Unit tests
    • E2E tests

User Facing Testing

  1. Install WooCommerce Min/Max Quantities
  2. Set up some items (please test with more than one) with minimum and maximum quantities. Add these items to your cart.
  3. Go to the Cart block, edit the quantities so they violate the min/max rules you set. You should see an error.
  4. Try to proceed to checkout.
  5. Ensure the first invalid input is focused.
  6. Fix the first error and try again.
  7. Ensure the next invalid input is focused.
  8. Fix all errors and ensure you can proceed to checkout.
  • Do not include in the Testing Notes

WooCommerce Visibility

  • WooCommerce Core
  • Feature plugin
  • Experimental

Performance Impact

Changelog

Prevent shoppers from proceeding to checkout if the cart has invalid items.

@opr opr added status: needs review type: enhancement The issue is a request for an enhancement. focus: blocks Specific work involving or impacting how blocks behave. block: cart Issues related to the cart block. labels Nov 16, 2022
@opr opr self-assigned this Nov 16, 2022
@opr opr added this to the Behind Feature Flag milestone Nov 16, 2022
@rubikuserbot rubikuserbot requested review from a team and senadir and removed request for a team November 16, 2022 14:54
@github-actions
Copy link
Contributor

github-actions bot commented Nov 16, 2022

TypeScript Errors Report

Files with errors: 434
Total errors: 2072

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

assets/js/atomic/blocks/product-elements/image/block.js

assets/js/atomic/blocks/product-elements/image/edit.js

assets/js/atomic/blocks/product-elements/image/index.js

assets/js/base/context/providers/cart-checkout/cart-events/test/index.tsx

assets/js/base/context/providers/store-notices/components/store-notices-container.js

assets/js/base/context/providers/store-snackbar-notices/components/snackbar-notices-container.js

assets/js/blocks/cart/cart-cross-sells-product-list/cart-cross-sells-product.tsx

assets/js/blocks/cart/inner-blocks/filled-cart-block/frontend.tsx

assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/test/block.tsx

assets/js/utils/notices.ts

@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-7694.zip

@github-actions
Copy link
Contributor

github-actions bot commented Nov 16, 2022

Size Change: +2.18 kB (0%)

Total Size: 994 kB

Filename Size Change
build/active-filters-frontend.js 7.74 kB -4 B (0%)
build/active-filters-wrapper-frontend.js 6.02 kB -1 B (0%)
build/active-filters.js 7.33 kB +1 B (0%)
build/all-products.js 33.5 kB -9 B (0%)
build/all-reviews.js 7.78 kB -6 B (0%)
build/attribute-filter-wrapper-frontend.js 7.12 kB -2 B (0%)
build/attribute-filter.js 12.3 kB +3 B (0%)
build/cart-blocks/cart-accepted-payment-methods-frontend.js 1.38 kB +1 B (0%)
build/cart-blocks/cart-cross-sells-products--product-add-to-cart-frontend.js 5.48 kB +6 B (0%)
build/cart-blocks/cart-cross-sells-products-frontend.js 4.67 kB +1 B (0%)
build/cart-blocks/cart-express-payment--checkout-blocks/express-payment-frontend.js 5.01 kB +2 B (0%)
build/cart-blocks/cart-express-payment-frontend.js 765 B +1 B (0%)
build/cart-blocks/cart-line-items--mini-cart-contents-block/products-table-frontend.js 5.78 kB +145 B (+3%)
build/cart-blocks/cart-line-items-frontend.js 1.07 kB +1 B (0%)
build/cart-blocks/cart-totals-frontend.js 319 B -1 B (0%)
build/cart-blocks/filled-cart-frontend.js 858 B -1 B (0%)
build/cart-blocks/order-summary-coupon-form-frontend.js 1.72 kB -1 B (0%)
build/cart-blocks/order-summary-discount-frontend.js 2.16 kB +3 B (0%)
build/cart-blocks/order-summary-heading-frontend.js 456 B +1 B (0%)
build/cart-blocks/order-summary-shipping--checkout-blocks/order-summary-shipping-frontend.js 5.66 kB -4 B (0%)
build/cart-blocks/proceed-to-checkout-frontend.js 1.26 kB +60 B (+5%) 🔍
build/cart-frontend.js 54.6 kB +168 B (0%)
build/cart.js 46.8 kB +215 B (0%)
build/checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 3.9 kB +1 B (0%)
build/checkout-blocks/billing-address-frontend.js 953 B +1 B (0%)
build/checkout-blocks/contact-information-frontend.js 1.77 kB +1 B (0%)
build/checkout-blocks/express-payment-frontend.js 1.12 kB +2 B (0%)
build/checkout-blocks/fields-frontend.js 344 B +1 B (0%)
build/checkout-blocks/order-summary-cart-items-frontend.js 3.68 kB +3 B (0%)
build/checkout-blocks/order-summary-coupon-form-frontend.js 1.89 kB -4 B (0%)
build/checkout-blocks/order-summary-discount-frontend.js 2.28 kB +1 B (0%)
build/checkout-blocks/order-summary-fee-frontend.js 274 B -1 B (0%)
build/checkout-blocks/order-summary-shipping-frontend.js 602 B -1 B (0%)
build/checkout-blocks/payment-frontend.js 7.77 kB +2 B (0%)
build/checkout-blocks/shipping-methods-frontend.js 4.89 kB +3 B (0%)
build/checkout-blocks/terms-frontend.js 1.63 kB -1 B (0%)
build/checkout-frontend.js 56.6 kB +5 B (0%)
build/checkout.js 40.3 kB -1 B (0%)
build/featured-category.js 13.2 kB -4 B (0%)
build/filter-wrapper.js 2.4 kB +1 B (0%)
build/handpicked-products.js 7.29 kB +3 B (0%)
build/legacy-template.js 2.85 kB +1 B (0%)
build/mini-cart-component-frontend.js 20 kB -1 B (0%)
build/mini-cart-contents-block/footer-frontend.js 2.97 kB -2 B (0%)
build/mini-cart-contents-block/items-frontend.js 236 B -1 B (0%)
build/mini-cart-contents-block/products-table-frontend.js 1.61 kB +1.02 kB (+172%) 🆘
build/mini-cart-contents-block/shopping-button-frontend.js 287 B -1 B (0%)
build/mini-cart-contents.js 18 kB +590 B (+3%)
build/mini-cart.js 4.28 kB -2 B (0%)
build/price-filter-frontend.js 13.6 kB -2 B (0%)
build/price-filter-wrapper-frontend.js 7.01 kB -1 B (0%)
build/price-filter.js 8.38 kB +2 B (0%)
build/product-add-to-cart.js 8.37 kB +5 B (0%)
build/product-best-sellers.js 7.62 kB -2 B (0%)
build/product-button--product-category-list--product-image--product-price--product-rating--product-sale-b--e17c7c01.js 432 B +1 B (0%)
build/product-button--product-image--product-rating--product-sale-badge--product-title.js 303 B +1 B (0%)
build/product-button-frontend.js 1.9 kB +5 B (0%)
build/product-button.js 3.82 kB -1 B (0%)
build/product-category-list-frontend.js 883 B -1 B (0%)
build/product-category.js 8.61 kB -2 B (0%)
build/product-image.js 3.92 kB -3 B (0%)
build/product-new.js 7.62 kB -6 B (0%)
build/product-on-sale.js 7.95 kB -3 B (0%)
build/product-price-frontend.js 1.92 kB +2 B (0%)
build/product-price.js 1.53 kB -1 B (0%)
build/product-rating-frontend.js 1.2 kB +2 B (0%)
build/product-rating.js 786 B -1 B (0%)
build/product-sale-badge-frontend.js 1.14 kB -4 B (0%)
build/product-sale-badge.js 815 B +3 B (0%)
build/product-search.js 2.62 kB -1 B (0%)
build/product-sku-frontend.js 378 B +1 B (0%)
build/product-stock-indicator-frontend.js 1.01 kB +2 B (0%)
build/product-stock-indicator.js 645 B -1 B (0%)
build/product-summary-frontend.js 1.29 kB +1 B (0%)
build/product-summary.js 919 B -2 B (0%)
build/product-tag-list-frontend.js 877 B -1 B (0%)
build/product-tag-list.js 496 B -2 B (0%)
build/product-tag.js 8 kB +5 B (0%)
build/product-title-frontend.js 1.33 kB -4 B (0%)
build/product-top-rated.js 7.86 kB -2 B (0%)
build/products-by-attribute.js 8.54 kB +1 B (0%)
build/rating-filter-frontend.js 7.16 kB +1 B (0%)
build/reviews-by-category.js 11.2 kB -3 B (0%)
build/reviews-by-product.js 12.3 kB +4 B (0%)
build/single-product-frontend.js 32.2 kB -3 B (0%)
build/single-product.js 10 kB -1 B (0%)
build/stock-filter-wrapper-frontend.js 6.03 kB -2 B (0%)
build/stock-filter.js 6.71 kB +1 B (0%)
build/vendors--cart-blocks/cart-cross-sells-products--cart-blocks/order-summary-shipping--checkout-blocks--18f9376a-frontend.js 19.1 kB -2 B (0%)
build/vendors--cart-blocks/cart-cross-sells-products--product-add-to-cart-frontend.js 7.53 kB -3 B (0%)
build/wc-blocks-vendors.js 62.4 kB +6 B (0%)
ℹ️ View Unchanged
Filename Size
build/all-products-frontend.js 26.4 kB
build/attribute-filter-frontend.js 22.6 kB
build/blocks-checkout.js 18.5 kB
build/cart-blocks/cart-cross-sells-frontend.js 253 B
build/cart-blocks/cart-items-frontend.js 298 B
build/cart-blocks/cart-order-summary-frontend.js 1.11 kB
build/cart-blocks/empty-cart-frontend.js 346 B
build/cart-blocks/order-summary-fee-frontend.js 273 B
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/checkout-blocks/actions-frontend.js 1.77 kB
build/checkout-blocks/order-note-frontend.js 1.14 kB
build/checkout-blocks/order-summary-frontend.js 1.11 kB
build/checkout-blocks/order-summary-subtotal-frontend.js 273 B
build/checkout-blocks/order-summary-taxes-frontend.js 433 B
build/checkout-blocks/shipping-address-frontend.js 1.06 kB
build/checkout-blocks/totals-frontend.js 323 B
build/featured-product.js 13.4 kB
build/filter-wrapper-frontend.js 13.8 kB
build/general-style-rtl.css 1.29 kB
build/general-style.css 1.29 kB
build/mini-cart-contents-block/empty-cart-frontend.js 366 B
build/mini-cart-contents-block/filled-cart-frontend.js 230 B
build/mini-cart-contents-block/title-frontend.js 368 B
build/mini-cart-frontend.js 1.77 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 226 B
build/product-add-to-cart--product-button--product-image--product-rating--product-title.js 151 B
build/product-add-to-cart-frontend.js 1.25 kB
build/product-categories.js 2.36 kB
build/product-category-list.js 502 B
build/product-image-frontend.js 1.9 kB
build/product-query.js 2.89 kB
build/product-sku.js 376 B
build/product-title.js 3.3 kB
build/rating-filter-wrapper-frontend.js 5.41 kB
build/rating-filter.js 5.79 kB
build/reviews-frontend.js 7.01 kB
build/stock-filter-frontend.js 7.78 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-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.5 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 931 B
build/wc-blocks-registry.js 2.92 kB
build/wc-blocks-shared-context.js 1.51 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

@opr opr force-pushed the add/cart-events-context branch from 67be8e3 to 3d80d8c Compare November 16, 2022 15:36
@github-actions
Copy link
Contributor

TypeScript Errors Report

Files with errors: 448
Total errors: 2122

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

assets/js/atomic/blocks/product-elements/image/test/block.test.js

assets/js/atomic/blocks/product-elements/price/index.js

assets/js/atomic/blocks/product-elements/stock-indicator/block.js

assets/js/atomic/blocks/product-elements/stock-indicator/edit.js

assets/js/atomic/blocks/product-elements/tag-list/index.ts

assets/js/base/components/cart-checkout/address-form/address-form.tsx

assets/js/base/components/cart-checkout/totals/coupon/index.tsx

assets/js/base/components/product-price/index.tsx

assets/js/base/components/state-input/state-input.tsx

assets/js/base/components/text-input/text-input.tsx

assets/js/base/components/text-input/validated-text-input.tsx

assets/js/base/context/hooks/use-checkout-submit.js

assets/js/base/context/providers/cart-checkout/cart-events/test/index.tsx

assets/js/blocks/cart/block.js

assets/js/blocks/cart/inner-blocks/filled-cart-block/frontend.tsx

assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/block.tsx

assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/test/block.tsx

assets/js/blocks/checkout/block.tsx

assets/js/blocks/checkout/inner-blocks/checkout-contact-information-block/block.tsx

assets/js/blocks/checkout/phone-number/index.tsx

assets/js/blocks/mini-cart/frontend.ts

assets/js/blocks/product-query/constants.ts

assets/js/data/payment/check-payment-methods.ts

assets/js/types/type-defs/payment-method-interface.ts

packages/checkout/blocks-registry/get-registered-blocks.ts

packages/checkout/blocks-registry/index.ts

packages/checkout/blocks-registry/register-checkout-block.ts

packages/checkout/components/index.js

packages/checkout/components/order-meta/index.js

packages/checkout/components/totals/index.js

packages/checkout/index.js

packages/checkout/slot/index.js

packages/checkout/utils/index.js

@github-actions
Copy link
Contributor

github-actions bot commented Nov 16, 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.

Copy link
Member

@alexflorisca alexflorisca left a comment

Choose a reason for hiding this comment

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

Nice work, I've tested and it works. Code looks good too, just one suggestion. I think you have quite a few TS errors though so worth fixing those before you merge. Not really sure if the bot displays the results after every push though...

@@ -87,8 +90,10 @@ const Block = ( { attributes, children, scrollToTop } ) => (
<StoreNoticesContainer context="wc/cart" />
<SlotFillProvider>
<CartProvider>
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if we should combine Cart and CartEvents providers? At the moment, the Cart provider just passed the children down and has a redirectUrl

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please see Mike's comment below

@opr opr force-pushed the remove/auto-quantity-update branch from f7e86c8 to 7a554d8 Compare November 17, 2022 12:03
@opr opr force-pushed the add/cart-events-context branch from 93781db to ab88dfe Compare November 17, 2022 12:36
@opr opr force-pushed the add/cart-events-context branch from 4c30af1 to bddf8f6 Compare November 17, 2022 12:53
Copy link
Member

@mikejolley mikejolley left a comment

Choose a reason for hiding this comment

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

👍🏻 I think it's ok to keep the cart provider separate to the cart events context provider. Maybe they will grow in scope over time.

@opr opr merged commit 1d30534 into remove/auto-quantity-update Nov 18, 2022
@opr opr deleted the add/cart-events-context branch November 18, 2022 15:42
@opr opr restored the add/cart-events-context branch December 1, 2022 15:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: cart Issues related to the cart block. focus: blocks Specific work involving or impacting how blocks behave. 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