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

Fix React hook dependency warnings in Cart & Checkout blocks + withAttributes HOC #3314

Merged
merged 6 commits into from
Oct 27, 2020

Conversation

Aljullu
Copy link
Contributor

@Aljullu Aljullu commented Oct 22, 2020

Part of #3204.
Related to #3285.
Fixes #2813.

How to test the changes in this Pull Request:

Cart and Checkout blocks

Most of the changes are related to input field validation/displaying errors + the cart item quantity input, so these are the main flows that need to be tested.

  1. In the Cart block, try increasing and decreasing the quantity of a product. Verify that while calculating the new totals, the Proceed to Checkout button is disabled.
  2. Verify you can remove an item from the cart.
  3. Go to the Checkout block with a no-registered user and with the Stripe gateway set up.
  4. Try submitting the form before interacting with any input item.
  5. Verify validation errors appear because the input fields were empty.
  6. Interact with the input fields and selects and verify when they are invalid (required field is empty, email field is not a valid email, etc.) a validation error is shown below it. And that error disappears when the input becomes valid.
  7. Interact with Stripe input fields and verify validation errors are also displayed when they are invalid.
  8. Try submitting the order when inputs are invalid and verify the order is not processed.
  9. Fix the invalid inputs fields and submit the order. Verify the order is processed.

withAttributes HOC

  1. Create a page and add a Products by Attribute block.
  2. Verify attributes are listed.
  3. Verify you can select an attribute and clicking Done shows the products that match that attribute.
  4. Verify you can edit the block (pencil icon) and the previously selected attribute is selected by default.
  5. Verify you can change to another attribute.

@Aljullu Aljullu added status: needs review type: refactor The issue/PR is related to refactoring. skip-changelog PRs that you don't want to appear in the changelog. labels Oct 22, 2020
@Aljullu Aljullu added this to the 3.7.0 milestone Oct 22, 2020
@Aljullu Aljullu requested a review from a team as a code owner October 22, 2020 14:43
@Aljullu Aljullu self-assigned this Oct 22, 2020
@Aljullu Aljullu requested review from senadir and removed request for a team October 22, 2020 14:43
@github-actions
Copy link
Contributor

github-actions bot commented Oct 22, 2020

Size Change: +421 B (0%)

Total Size: 1.12 MB

Filename Size Change
build/cart-frontend.js 70.2 kB +108 B (0%)
build/cart.js 38.8 kB +129 B (0%)
build/checkout-frontend.js 86 kB +80 B (0%)
build/checkout.js 42.1 kB +94 B (0%)
build/products-by-attribute.js 8.35 kB +20 B (0%)
build/wc-payment-method-stripe.js 12 kB -10 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/active-filters-frontend.js 8.81 kB 0 B
build/active-filters.js 8.85 kB 0 B
build/all-products-frontend.js 31.2 kB 0 B
build/all-products.js 36.1 kB 0 B
build/all-reviews.js 9.78 kB 0 B
build/atomic-block-components/add-to-cart-frontend.js 8.95 kB 0 B
build/atomic-block-components/add-to-cart.js 7.52 kB 0 B
build/atomic-block-components/add-to-cart~atomic-block-components/button.js 3.18 kB 0 B
build/atomic-block-components/add-to-cart~atomic-block-components/image~atomic-block-components/title.js 335 B 0 B
build/atomic-block-components/button-frontend.js 2.02 kB 0 B
build/atomic-block-components/button.js 839 B 0 B
build/atomic-block-components/category-list-frontend.js 469 B 0 B
build/atomic-block-components/category-list.js 478 B 0 B
build/atomic-block-components/image-frontend.js 1.7 kB 0 B
build/atomic-block-components/image.js 1.15 kB 0 B
build/atomic-block-components/price-frontend.js 2.29 kB 0 B
build/atomic-block-components/price.js 2.32 kB 0 B
build/atomic-block-components/rating-frontend.js 524 B 0 B
build/atomic-block-components/rating.js 527 B 0 B
build/atomic-block-components/sale-badge-frontend.js 859 B 0 B
build/atomic-block-components/sale-badge.js 863 B 0 B
build/atomic-block-components/sku-frontend.js 386 B 0 B
build/atomic-block-components/sku.js 395 B 0 B
build/atomic-block-components/stock-indicator-frontend.js 568 B 0 B
build/atomic-block-components/stock-indicator.js 573 B 0 B
build/atomic-block-components/summary-frontend.js 917 B 0 B
build/atomic-block-components/summary.js 927 B 0 B
build/atomic-block-components/tag-list-frontend.js 467 B 0 B
build/atomic-block-components/tag-list.js 474 B 0 B
build/atomic-block-components/title-frontend.js 1.23 kB 0 B
build/atomic-block-components/title.js 1.06 kB 0 B
build/attribute-filter-frontend.js 18.2 kB 0 B
build/attribute-filter.js 12.4 kB 0 B
build/blocks.js 3.54 kB 0 B
build/editor-rtl.css 13.8 kB 0 B
build/editor.css 13.9 kB 0 B
build/featured-category.js 7.73 kB 0 B
build/featured-product.js 9.97 kB 0 B
build/handpicked-products.js 7.37 kB 0 B
build/price-filter-frontend.js 14.8 kB 0 B
build/price-filter.js 10.3 kB 0 B
build/product-best-sellers.js 7.45 kB 0 B
build/product-categories.js 3.23 kB 0 B
build/product-category.js 8.39 kB 0 B
build/product-new.js 7.61 kB 0 B
build/product-on-sale.js 8 kB 0 B
build/product-search.js 3.56 kB 0 B
build/product-tag.js 6.53 kB 0 B
build/product-top-rated.js 7.58 kB 0 B
build/reviews-by-category.js 11.8 kB 0 B
build/reviews-by-product.js 13.4 kB 0 B
build/reviews-frontend.js 9.4 kB 0 B
build/single-product-frontend.js 33.8 kB 0 B
build/single-product.js 10.1 kB 0 B
build/style-rtl.css 17.9 kB 0 B
build/style.css 17.9 kB 0 B
build/vendors-style-rtl.css 1.03 kB 0 B
build/vendors-style.css 1.03 kB 0 B
build/vendors.js 417 kB 0 B
build/vendors~atomic-block-components/price-frontend.js 5.65 kB 0 B
build/wc-blocks-data.js 7.1 kB 0 B
build/wc-blocks-middleware.js 931 B 0 B
build/wc-blocks-registry.js 2.32 kB 0 B
build/wc-payment-method-bacs.js 790 B 0 B
build/wc-payment-method-cheque.js 787 B 0 B
build/wc-payment-method-cod.js 879 B 0 B
build/wc-payment-method-paypal.js 831 B 0 B
build/wc-settings.js 2.35 kB 0 B
build/wc-shared-context.js 1.53 kB 0 B
build/wc-shared-hocs.js 1.66 kB 0 B

compressed-size-action

Copy link
Member

@senadir senadir 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 a great PR, unlike my comment here #3285 (review) I feel more confident merging this PR, it makes sense and no change require documentation because the diff is self-documenting

Comment on lines 64 to 80
useEffect( () => {
if ( validateOnMount ) {
validateInput();
if ( isPristine ) {
if ( focusOnMount ) {
inputRef.current.focus();
}
if ( validateOnMount ) {
validateInput();
}
setIsPristine( false );
}
}, [ validateOnMount ] );
}, [
focusOnMount,
isPristine,
setIsPristine,
validateOnMount,
validateInput,
] );
Copy link
Member

Choose a reason for hiding this comment

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

Why did you merge two useEffects? the general conscious is to separate useEffects unless you have a reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I undid the merge in 14c4ae8.

Comment on lines -43 to -49
/**
* @todo Investigate extra POST request on initial cart render.
*
* We have an unfixed bug in our test in which the full cart triggers a POST
* request to `wc/store/cart/update-item` causing the fetch to be called twice.
*/
expect( fetchMock ).toHaveBeenCalledTimes( 2 );
Copy link
Member

Choose a reason for hiding this comment

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

💃 Finally fixed!

Copy link
Member

Choose a reason for hiding this comment

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

Should probably also close this
#2813

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, yes. Thanks for pointing to the issue. I assigned it to me and linked it with this PR.

@@ -3,7 +3,6 @@
*/
import { useState, useEffect } from '@wordpress/element';
import { getAttributes, getTerms } from '@woocommerce/editor-components/utils';
import { find } from 'lodash';
Copy link
Member

Choose a reason for hiding this comment

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

💯

const { selected } = props;
const [ attributes, setAttributes ] = useState( [] );
const { selected = [] } = props;
const selectedSlug = selected.length ? selected[ 0 ]?.attr_slug : null;
Copy link
Member

Choose a reason for hiding this comment

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

In my experience, when working with an expected data endpoint (like our own REST API), using optional chaining signals a code smell.
Disregarding line 32, do we expect selected to be an empty array?
Also if you defaulting to an empty array in line 32, length would evaluate to 0, which would skip directly to null, so no optional chaining required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah... good point. I don't think the optional chaining is necessary at all, probably it's a left-over from an in-progress refactor I was doing. Fixed in bca57a5.

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: refactor The issue/PR is related to refactoring.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate extra POST request on initial cart render.
3 participants