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

New contexts for StoreNoticesContainer and notice grouping #7711

Merged
merged 48 commits into from
Dec 19, 2022

Conversation

mikejolley
Copy link
Member

@mikejolley mikejolley commented Nov 18, 2022

This PR refactors StoreNoticeContainer so it combines snack bars and regular notices (instead of separate components) and introduces new noticeContexts for inner blocks.

New noticeContexts

Adds several notice contexts for the main inner blocks that can be used as context instead of strings. Each notice context has a corresponding StoreNoticesContainer component to render any notices added.

Rather than create a new interface, I extended the existing one which only contained payment contexts:

PAYMENTS = 'wc/checkout/payments',
EXPRESS_PAYMENTS = 'wc/checkout/express-payments',

The full list is now:

CART = 'wc/cart',
CHECKOUT = 'wc/checkout',
PAYMENTS = 'wc/checkout/payments',
EXPRESS_PAYMENTS = 'wc/checkout/express-payments',
CONTACT_INFORMATION = 'wc/checkout/contact-information',
SHIPPING_ADDRESS = 'wc/checkout/shipping-address',
BILLING_ADDRESS = 'wc/checkout/billing-address',
SHIPPING_METHODS = 'wc/checkout/shipping-methods',
CHECKOUT_ACTIONS = 'wc/checkout/checkout-actions',

Notice grouping

Groups notices of the same type into a single error notice. You can test this on the cart page by calling this a few times:

wp.data.dispatch( 'core/notices' ).createNotice( 'error', 'Test', { context: 'wc/cart' } );

Screenshot 2022-12-14 at 16 44 13

Notice creation helpers

Existing notices have been modified to use the new containers and createNotice functions.

Additionally, when a new error is displayed, it will scroll to the parent StoreNoticeContainer. The scrolling fixes #2559

This mechanism also ensures that if a StoreNoticesContainer does not exist, it uses a parent context.

Changes to hooks

New filters: showApplyCouponNotice and showRemoveCouponNotice

To simplify notice handling, I've moved the error suppression for coupons to showApplyCouponNotice to prevent the notice from being added in the first place. This will break Points and Rewards (in a non-destructive way), so this will need a patch to use the new filter (https://github.com/woocommerce/woocommerce-points-and-rewards/blob/5d3b6812188e969505f612868ce228a387f1ea12/assets/js/filters.js#L24).

The fix for points and rewards can be found here: https://github.com/woocommerce/woocommerce-points-and-rewards/pull/491

Other changes

  • Removed useCheckoutNotices. It was only used in one place and not shared, so I moved the logic inline.
  • removeAllNotices when processing checkout so new notices can surface.
  • Typed checkout events and push-changes code.
  • Helpers to processErrorResponse and processInvalidParamResponse from the API

Closes #2559

Testing

User-facing testing

Snack bars:

  1. Add a product to the cart.
  2. Go to cart page
  3. Apply a valid coupon code
  4. See snack bar notice on the page

Error notices from API:

  1. Add a product to the cart.
  2. Go to the cart page and set the quantity to 5 for the product.
  3. Checkout. Fill out the form but do not submit.
  4. In another tab, go into product admin and edit the product. Enable stock management, and set stock qty to 4.
  5. Go back to the checkout and place order.
  6. Checkout should scroll to the top and display an error:

Screenshot 2022-12-14 at 16 22 28

  • Do not include in the Testing Notes

cc @opr

@mikejolley mikejolley requested a review from opr November 18, 2022 11:47
@mikejolley mikejolley self-assigned this Nov 18, 2022
@rubikuserbot rubikuserbot requested a review from a team November 18, 2022 11:47
@mikejolley mikejolley added block: cart Issues related to the cart block. block: checkout Issues related to the checkout block. labels Nov 18, 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-7711.zip

@github-actions
Copy link
Contributor

github-actions bot commented Nov 18, 2022

TypeScript Errors Report

Files with errors: 439
Total errors: 2099

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

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

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

assets/js/data/cart/resolvers.ts

assets/js/data/checkout/types.ts

assets/js/data/checkout/utils.ts

assets/js/data/index.ts

assets/js/data/payment/types.ts

assets/js/data/shared-controls.ts

assets/js/data/validation/actions.ts

assets/js/data/validation/reducers.ts

assets/js/data/validation/test/reducers.ts

assets/js/data/validation/test/selectors.ts

packages/checkout/components/store-notices-container/snackbar-notices.tsx

packages/checkout/components/store-notices-container/store-notices.tsx

packages/checkout/components/store-notices-container/test/index.tsx

@github-actions
Copy link
Contributor

github-actions bot commented Nov 18, 2022

Size Change: -3.61 kB (0%)

Total Size: 1 MB

Filename Size Change
build/active-filters-frontend.js 7.74 kB -10 B (0%)
build/active-filters-wrapper-frontend.js 6.01 kB +2 B (0%)
build/active-filters.js 7.32 kB -3 B (0%)
build/all-products-frontend.js 11.3 kB -55 B (0%)
build/all-products.js 33.3 kB +5 B (0%)
build/all-reviews.js 7.65 kB -132 B (-2%)
build/attribute-filter-frontend.js 22.7 kB +87 B (0%)
build/attribute-filter-wrapper-frontend.js 7.66 kB +8 B (0%)
build/attribute-filter.js 12.3 kB +5 B (0%)
build/blocks-checkout.js 39.5 kB +12.3 kB (+45%) 🚨
build/cart-blocks/cart-accepted-payment-methods-frontend.js 1.38 kB -8 B (-1%)
build/cart-blocks/cart-cross-sells-products--product-add-to-cart-frontend.js 0 B -5.48 kB (removed) 🏆
build/cart-blocks/cart-cross-sells-products-frontend.js 9.57 kB +4.8 kB (+101%) 🆘
build/cart-blocks/cart-express-payment--checkout-blocks/express-payment-frontend.js 5.08 kB +63 B (+1%)
build/cart-blocks/cart-express-payment-frontend.js 778 B -1 B (0%)
build/cart-blocks/cart-line-items-frontend.js 1.06 kB +2 B (0%)
build/cart-blocks/cart-order-summary-frontend.js 1.24 kB +1 B (0%)
build/cart-blocks/cart-totals-frontend.js 321 B +1 B (0%)
build/cart-blocks/filled-cart-frontend.js 780 B -1 B (0%)
build/cart-blocks/order-summary-coupon-form-frontend.js 1.77 kB +78 B (+5%) 🔍
build/cart-blocks/order-summary-discount-frontend.js 2.18 kB +43 B (+2%)
build/cart-blocks/order-summary-fee-frontend.js 273 B -1 B (0%)
build/cart-blocks/order-summary-heading-frontend.js 456 B +1 B (0%)
build/cart-blocks/order-summary-shipping-frontend.js 14.6 kB +8.71 kB (+149%) 🆘
build/cart-blocks/order-summary-subtotal-frontend.js 273 B -2 B (-1%)
build/cart-blocks/order-summary-taxes-frontend.js 436 B +5 B (+1%)
build/cart-frontend.js 27.3 kB -18.9 kB (-41%) 🎉
build/cart.js 45.8 kB -75 B (0%)
build/checkout-blocks/actions-frontend.js 1.85 kB +50 B (+3%)
build/checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 3.87 kB +120 B (+3%)
build/checkout-blocks/billing-address-frontend.js 1.12 kB +38 B (+3%)
build/checkout-blocks/contact-information-frontend.js 2 kB +171 B (+9%) 🔍
build/checkout-blocks/fields-frontend.js 342 B -2 B (-1%)
build/checkout-blocks/order-note-frontend.js 1.14 kB +3 B (0%)
build/checkout-blocks/order-summary-cart-items-frontend.js 3.67 kB -3 B (0%)
build/checkout-blocks/order-summary-coupon-form-frontend.js 1.93 kB +75 B (+4%)
build/checkout-blocks/order-summary-discount-frontend.js 2.29 kB +42 B (+2%)
build/checkout-blocks/order-summary-frontend.js 1.24 kB +3 B (0%)
build/checkout-blocks/order-summary-shipping-frontend.js 14.6 kB +8.73 kB (+148%) 🆘
build/checkout-blocks/order-summary-subtotal-frontend.js 273 B -1 B (0%)
build/checkout-blocks/order-summary-taxes-frontend.js 436 B +4 B (+1%)
build/checkout-blocks/payment-frontend.js 8.34 kB +63 B (+1%)
build/checkout-blocks/shipping-address-frontend.js 1.11 kB +47 B (+4%)
build/checkout-blocks/shipping-methods-frontend.js 4.57 kB -441 B (-9%)
build/checkout-blocks/terms-frontend.js 1.56 kB -5 B (0%)
build/checkout-blocks/totals-frontend.js 325 B +1 B (0%)
build/checkout-frontend.js 29.2 kB -19.1 kB (-40%) 🎉
build/checkout.js 40.1 kB -139 B (0%)
build/featured-category.js 13.1 kB -131 B (-1%)
build/featured-product.js 13.3 kB -138 B (-1%)
build/filter-wrapper-frontend.js 13.8 kB -5 B (0%)
build/filter-wrapper.js 2.4 kB -3 B (0%)
build/handpicked-products.js 7.21 kB -122 B (-2%)
build/legacy-template.js 2.85 kB -1 B (0%)
build/mini-cart-component-frontend.js 20 kB -22 B (0%)
build/mini-cart-contents-block/footer-frontend.js 2.81 kB -177 B (-6%)
build/mini-cart-contents-block/products-table-frontend.js 589 B -1 B (0%)
build/mini-cart-contents-block/title-frontend.js 368 B +2 B (+1%)
build/mini-cart-contents.js 17.1 kB -193 B (-1%)
build/mini-cart-frontend.js 1.88 kB -2 B (0%)
build/mini-cart.js 4.28 kB +2 B (0%)
build/price-filter-frontend.js 13.6 kB -24 B (0%)
build/price-filter-wrapper-frontend.js 7.01 kB +9 B (0%)
build/price-filter.js 8.37 kB -6 B (0%)
build/product-add-to-cart-frontend.js 6.71 kB +5.25 kB (+359%) 🆘
build/product-add-to-cart.js 8.46 kB +92 B (+1%)
build/product-best-sellers.js 7.58 kB -124 B (-2%)
build/product-button--product-category-list--product-image--product-price--product-rating--product-sale-b--e17c7c01.js 441 B +1 B (0%)
build/product-button--product-image--product-rating--product-sale-badge--product-title.js 302 B +2 B (+1%)
build/product-button-frontend.js 2.17 kB -6 B (0%)
build/product-button.js 3.84 kB -7 B (0%)
build/product-categories.js 2.36 kB +2 B (0%)
build/product-category-list-frontend.js 1.14 kB -3 B (0%)
build/product-category-list.js 502 B -1 B (0%)
build/product-category.js 8.57 kB -125 B (-1%)
build/product-image-frontend.js 2.16 kB -9 B (0%)
build/product-image.js 3.92 kB -5 B (0%)
build/product-new.js 7.57 kB -122 B (-2%)
build/product-on-sale.js 7.9 kB -118 B (-1%)
build/product-price-frontend.js 2.17 kB -1 B (0%)
build/product-price.js 1.54 kB -4 B (0%)
build/product-query.js 5.95 kB +2 B (0%)
build/product-rating-frontend.js 1.48 kB -2 B (0%)
build/product-sale-badge-frontend.js 1.39 kB -5 B (0%)
build/product-sale-badge.js 815 B +2 B (0%)
build/product-search.js 2.62 kB +2 B (0%)
build/product-sku-frontend.js 629 B +1 B (0%)
build/product-stock-indicator-frontend.js 1.27 kB -4 B (0%)
build/product-stock-indicator.js 645 B -1 B (0%)
build/product-summary.js 917 B -2 B (0%)
build/product-tag-list-frontend.js 1.14 kB -3 B (0%)
build/product-tag-list.js 498 B +1 B (0%)
build/product-tag.js 8.06 kB +3 B (0%)
build/product-title-frontend.js 1.58 kB -2 B (0%)
build/product-title.js 3.3 kB +1 B (0%)
build/product-top-rated.js 7.81 kB -119 B (-2%)
build/products-by-attribute.js 8.5 kB -118 B (-1%)
build/rating-filter-frontend.js 21.1 kB +76 B (0%)
build/rating-filter.js 7.39 kB -4 B (0%)
build/reviews-by-category.js 11.2 kB -122 B (-1%)
build/reviews-by-product.js 12.3 kB -129 B (-1%)
build/reviews-frontend.js 6.88 kB -133 B (-2%)
build/single-product-frontend.js 17.4 kB -153 B (-1%)
build/single-product.js 9.97 kB -132 B (-1%)
build/stock-filter-frontend.js 20.8 kB +45 B (0%)
build/stock-filter-wrapper-frontend.js 5.86 kB +9 B (0%)
build/stock-filter.js 8.13 kB -3 B (0%)
build/vendors--attribute-filter-wrapper--mini-cart-contents-block/footer--product-add-to-cart--rating-fil--b8470171-frontend.js 0 B -6.85 kB (removed) 🏆
build/vendors--attribute-filter-wrapper--rating-filter-wrapper--stock-filter-wrapper-frontend.js 7.7 kB +8 B (0%)
build/vendors--cart-blocks/cart-cross-sells-products--cart-blocks/cart-line-items--cart-blocks/cart-order--671ca56f-frontend.js 5.26 kB +2 B (0%)
build/vendors--cart-blocks/cart-cross-sells-products--product-add-to-cart-frontend.js 7.53 kB -6 B (0%)
build/vendors--cart-blocks/order-summary-shipping--checkout-blocks/billing-address--checkout-blocks/order--5b8feb0b-frontend.js 4.82 kB -1 B (0%)
build/vendors--cart-blocks/order-summary-shipping--checkout-blocks/order-summary-shipping--checkout-block--dda5866c-frontend.js 0 B -8.85 kB (removed) 🏆
build/wc-blocks-data.js 20.9 kB +1.46 kB (+8%) 🔍
build/wc-blocks-middleware.js 931 B -3 B (0%)
build/wc-blocks-registry.js 2.92 kB +4 B (0%)
build/wc-blocks-shared-context.js 1.52 kB +2 B (0%)
build/wc-blocks-shared-hocs.js 1.72 kB -1 B (0%)
build/wc-blocks-style-rtl.css 24.5 kB +51 B (0%)
build/wc-blocks-style.css 24.5 kB +53 B (0%)
build/wc-blocks-vendors.js 62.7 kB -18 B (0%)
build/wc-blocks.js 2.63 kB -1 B (0%)
build/wc-settings.js 2.6 kB -2 B (0%)
build/vendors--attribute-filter-wrapper--cart-blocks/cart-cross-sells-products--cart-blocks/order-summary--cde4eab5-frontend.js 6.86 kB +6.86 kB (new file) 🆕
build/vendors--checkout-blocks/shipping-methods-frontend.js 9.48 kB +9.48 kB (new file) 🆕
ℹ️ View Unchanged
Filename Size
build/cart-blocks/cart-cross-sells-frontend.js 253 B
build/cart-blocks/cart-items-frontend.js 298 B
build/cart-blocks/cart-line-items--mini-cart-contents-block/products-table-frontend.js 5.29 kB
build/cart-blocks/empty-cart-frontend.js 346 B
build/cart-blocks/proceed-to-checkout-frontend.js 1.23 kB
build/checkout-blocks/express-payment-frontend.js 1.13 kB
build/checkout-blocks/order-summary-fee-frontend.js 276 B
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/items-frontend.js 237 B
build/mini-cart-contents-block/shopping-button-frontend.js 312 B
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 225 B
build/product-add-to-cart--product-button--product-image--product-rating--product-title.js 151 B
build/product-rating.js 814 B
build/product-sku.js 376 B
build/product-summary-frontend.js 1.53 kB
build/rating-filter-wrapper-frontend.js 6.19 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-line-items--checkout-blocks/order-summary-cart-items--mini-cart-contents---233ab542-frontend.js 3.14 kB
build/wc-blocks-editor-style-rtl.css 5.22 kB
build/wc-blocks-editor-style.css 5.23 kB
build/wc-blocks-google-analytics.js 1.56 kB
build/wc-blocks-vendors-style-rtl.css 1.95 kB
build/wc-blocks-vendors-style.css 1.95 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

compressed-size-action

@github-actions
Copy link
Contributor

github-actions bot commented Nov 18, 2022

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
all-reviews.js wp-html-entities 🎉

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

Copy link
Contributor

@opr opr left a comment

Choose a reason for hiding this comment

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

Nice Mike, this tested well, but I think we do need to address the refs being in the data store.

We should also deprecate the snackbarNoticeVisibility filter properly imo.

Please see my comments below and let me know if anything is unclear or needs further testing.

packages/checkout/utils/create-notice.ts Outdated Show resolved Hide resolved
assets/js/data/store-notices/default-state.ts Outdated Show resolved Hide resolved
assets/js/data/store-notices/actions.ts Outdated Show resolved Hide resolved
assets/js/base/context/event-emit/utils.ts Outdated Show resolved Hide resolved
@mikejolley
Copy link
Member Author

@opr I've worked on everything you mentioned. I'll remove the test buttons before merge, and log/patch points and rewards for the new action name.

Copy link
Contributor

@opr opr 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, thanks Mike.

I tested it using MMQ and modifying the stock status of various items in my cart.

I am wondering if we should add some testing instructions for Globalstep/release lead for when this gets merged?

@mikejolley
Copy link
Member Author

@opr Compatibility patch: https://github.com/woocommerce/woocommerce-points-and-rewards/pull/491

There was a useMemo in our filter function which did not work when using filters inline (invalid hook usage). I've removed it — it looks like there is a cached value already so I'm not sure why useMemo was there anyway. Can you check?

@mikejolley
Copy link
Member Author

I tested it using MMQ and modifying the stock status of various items in my cart.

@opr By modifying cart items quantities? We can also trigger a snackbar with an invalid postcode, but we should be fixing that soon I think.

@opr
Copy link
Contributor

opr commented Nov 21, 2022

@opr By modifying cart items quantities? We can also trigger a snackbar with an invalid postcode, but we should be fixing that soon I think.

Yep, while on the Checkout page, I set the min quantity of an item then tried checking out. It shows the snackbar notice.
image

Setting an incorrect postcode also shows a snackbar.

@github-actions
Copy link
Contributor

Remove this when supported in Gutenberg.

Remove this when supported in Gutenberg. github.com/WordPress/gutenberg/pull/44059


* @todo Remove this when supported in Gutenberg.
* @see https://github.com/WordPress/gutenberg/pull/44059
*/
export const removeAllNotices = () => {
const containers = select( 'wc/store/notices' ).getContainers();
const { removeNotice } = dispatch( 'core/notices' );
const { getNotices } = select( 'core/notices' );
containers.forEach( ( container ) => {
getNotices( container ).forEach( ( notice ) => {
removeNotice( notice.id, container );
} );
} );

🚀 This comment was generated by the automations bot based on a todo comment in 3530786 in #7711. cc @mikejolley

@github-actions
Copy link
Contributor

Cart errors need to be watched for and created as notices...

Cart errors need to be watched for and created as notices elsewhere.


// @todo Cart errors need to be watched for and created as notices elsewhere.
useEffect( () => {
cartItemErrors.forEach( ( error ) => {
createErrorNotice( decodeEntities( error.message ), {

🚀 This comment was generated by the automations bot based on a todo comment in 3530786 in #7711. cc @mikejolley

@mikejolley
Copy link
Member Author

@opr I've merged the other notice PR here now - there are 2 tasks blocking this from merging because I don't want to surface too many inline notices as you populate the checkout. The worse ones are email and postcode.

Copy link
Contributor

@opr opr left a comment

Choose a reason for hiding this comment

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

@mikejolley sorry to be pedantic but could we rename the directory or store key for the new notices store? Currently the directory names map to the store name, prefixed with wc/store. Based on this folder name I, the data store should be wc/store/store-notices. So let's either update the key or dir name.

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

@mikejolley
Copy link
Member Author

@opr This is rebased now, and I've added a description of all the changes I've introduced. There are quite a few files touched, but the changes are mostly minimal. Mostly typescript.

Main changes are new contexts, the merging of notice containers, and the grouping of notices by type.

mikejolley and others added 23 commits December 19, 2022 12:40
* Update API type defs

* Move create notice utils

* Replace useCheckoutNotices with new contexts

* processCheckoutResponseHeaders should check headers are defined

* Scroll to error notices only if we're not editing a field

* Error handling utils

* processErrorResponse when pushing changes

* processErrorResponse when processing checkout

* remove formatStoreApiErrorMessage

* Add todo for cart errors

* Remove unused deps

* unused imports

* Fix linting warnings

* Unused dep

* Update assets/js/types/type-defs/api-response.ts

Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com>

* Add todo

* Use generic

* remove const

* Update array types

* Phone should be in address blocks

Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com>
@mikejolley mikejolley merged commit 4ead2e9 into trunk Dec 19, 2022
@mikejolley mikejolley deleted the refactor/store-notices branch December 19, 2022 15:30
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. block: checkout Issues related to the checkout block.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

All Products: scroll to top when a notice appears
2 participants