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

Filter Products By Price block: don't allow to insert negative values on inputs #5123

Merged

Conversation

gigitux
Copy link
Contributor

@gigitux gigitux commented Nov 10, 2021

This PR fixes some not unexpected behavior for the Filter Products By Price block:

The user can insert a negative value in the inputs

Now, with this PR, the user can't insert a negative value. This is possible thanks to the prop allowNegativesetted to true.

The user can "break" the slider when inserting in the right input a value that is lower than the value on the left input

Now, with this PR, when the user inserts in the right input a value that is lower than the value on the left input, the latter is reset to 0.
This check is done thanks to isValidMinValueand isValidMaxValue and this condition.

Fixes #2695

Testing

Manual Testing

How to test the changes in this Pull Request:

Check out this branch

  1. Make sure you have Woo Blocks plugins installed.
  2. Add "All Products" block.
  3. Add "Filter Products by Price" block
  4. Add "Active Product filters" block
  5. Click on Preview or Publish the page.
  6. Go to the page having all the above block added.

Check that:

  • the user can't insert in both inputs a negative number
  • the user can't insert on input left a number that is greater than input on the right
  • if the user inserts on the input on the right a number that is lower than input on the left, the component sets to 0 the minimum price

Changelog

Fixed Filter Products By Price Block: the user can't insert a negative value.

… Price block #2695

Don't allow to insert negative values on input for Filter Products By Price block
@gigitux gigitux added type: bug The issue/PR concerns a confirmed bug. block: filter by price Issues related to the Filter by Price block. labels Nov 10, 2021
@gigitux gigitux self-assigned this Nov 10, 2021
@rubikuserbot rubikuserbot requested review from a team and frontdevde and removed request for a team November 10, 2021 14:02
@github-actions
Copy link
Contributor

github-actions bot commented Nov 10, 2021

Size Change: +290 B (0%)

Total Size: 819 kB

Filename Size Change
build/checkout.js 47.1 kB +1 B (0%)
build/featured-category.js 8.55 kB +1 B (0%)
build/price-filter-frontend.js 12.6 kB +139 B (+1%)
build/price-filter.js 8.75 kB +149 B (+2%)
ℹ️ View Unchanged
Filename Size
build/active-filters-frontend.js 6.22 kB
build/active-filters.js 7.05 kB
build/all-products-frontend.js 18.6 kB
build/all-products.js 34.4 kB
build/all-reviews.js 8.35 kB
build/atomic-block-components/add-to-cart--atomic-block-components/button--atomic-block-components/image---a7e2bb9b.js 2.76 kB
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 1.48 kB
build/atomic-block-components/add-to-cart-frontend.js 6.87 kB
build/atomic-block-components/add-to-cart.js 6.42 kB
build/atomic-block-components/button-frontend.js 1.48 kB
build/atomic-block-components/button.js 851 B
build/atomic-block-components/category-list-frontend.js 457 B
build/atomic-block-components/category-list.js 458 B
build/atomic-block-components/image-frontend.js 1.37 kB
build/atomic-block-components/image.js 1.05 kB
build/atomic-block-components/price-frontend.js 1.74 kB
build/atomic-block-components/price.js 1.7 kB
build/atomic-block-components/rating-frontend.js 552 B
build/atomic-block-components/rating.js 554 B
build/atomic-block-components/sale-badge-frontend.js 625 B
build/atomic-block-components/sale-badge.js 622 B
build/atomic-block-components/sku-frontend.js 386 B
build/atomic-block-components/sku.js 385 B
build/atomic-block-components/stock-indicator-frontend.js 584 B
build/atomic-block-components/stock-indicator.js 585 B
build/atomic-block-components/summary-frontend.js 872 B
build/atomic-block-components/summary.js 871 B
build/atomic-block-components/tag-list-frontend.js 458 B
build/atomic-block-components/tag-list.js 458 B
build/atomic-block-components/title-frontend.js 1.11 kB
build/atomic-block-components/title.js 1.1 kB
build/attribute-filter-frontend.js 16.3 kB
build/attribute-filter.js 12.7 kB
build/blocks-checkout.js 17.6 kB
build/cart-blocks/accepted-payment-methods-frontend.js 1.15 kB
build/cart-blocks/checkout-button-frontend.js 1.14 kB
build/cart-blocks/empty-cart-frontend.js 345 B
build/cart-blocks/express-payment-frontend.js 4.86 kB
build/cart-blocks/filled-cart-frontend.js 766 B
build/cart-blocks/items-frontend.js 298 B
build/cart-blocks/line-items-frontend.js 5.13 kB
build/cart-blocks/order-summary-frontend.js 8.97 kB
build/cart-blocks/totals-frontend.js 320 B
build/cart-frontend.js 45.5 kB
build/cart.js 44.4 kB
build/checkout-blocks/actions-frontend.js 1.44 kB
build/checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 4.24 kB
build/checkout-blocks/billing-address-frontend.js 884 B
build/checkout-blocks/contact-information-frontend.js 2.95 kB
build/checkout-blocks/express-payment-frontend.js 5.15 kB
build/checkout-blocks/fields-frontend.js 343 B
build/checkout-blocks/order-note-frontend.js 1.13 kB
build/checkout-blocks/order-summary-frontend.js 11.4 kB
build/checkout-blocks/payment-frontend.js 7.41 kB
build/checkout-blocks/shipping-address-frontend.js 971 B
build/checkout-blocks/shipping-methods-frontend.js 4.81 kB
build/checkout-blocks/terms-frontend.js 1.21 kB
build/checkout-blocks/totals-frontend.js 324 B
build/checkout-frontend.js 47.6 kB
build/featured-product.js 9.9 kB
build/handpicked-products.js 7.32 kB
build/legacy-template.js 2.08 kB
build/mini-cart-component-frontend.js 14.2 kB
build/mini-cart-contents.js 3.58 kB
build/mini-cart-frontend.js 1.76 kB
build/mini-cart.js 6.45 kB
build/price-format.js 1.18 kB
build/product-best-sellers.js 7.51 kB
build/product-categories.js 3.47 kB
build/product-category.js 8.35 kB
build/product-new.js 7.66 kB
build/product-on-sale.js 8.05 kB
build/product-search.js 2.47 kB
build/product-tag.js 7.76 kB
build/product-top-rated.js 7.63 kB
build/products-by-attribute.js 8.48 kB
build/reviews-by-category.js 11.9 kB
build/reviews-by-product.js 12.9 kB
build/reviews-frontend.js 7.25 kB
build/single-product-frontend.js 22.1 kB
build/single-product.js 10.4 kB
build/stock-filter-frontend.js 6.81 kB
build/stock-filter.js 6.82 kB
build/vendors--atomic-block-components/add-to-cart--cart-blocks/order-summary--checkout-blocks/billing-ad--c5eb4dcd-frontend.js 19 kB
build/vendors--atomic-block-components/add-to-cart-frontend.js 6.82 kB
build/vendors--atomic-block-components/price--cart-blocks/line-items--cart-blocks/order-summary--checkout--8a3571de-frontend.js 5.71 kB
build/vendors--cart-blocks/line-items--checkout-blocks/order-summary-frontend.js 3.14 kB
build/vendors--cart-blocks/order-summary--checkout-blocks/billing-address--checkout-blocks/order-summary---eb4d2cec-frontend.js 4.75 kB
build/wc-blocks-data.js 8.84 kB
build/wc-blocks-editor-style-rtl.css 4.4 kB
build/wc-blocks-editor-style.css 4.4 kB
build/wc-blocks-google-analytics.js 1.56 kB
build/wc-blocks-middleware.js 949 B
build/wc-blocks-registry.js 2.7 kB
build/wc-blocks-shared-context.js 1.51 kB
build/wc-blocks-shared-hocs.js 1.14 kB
build/wc-blocks-style-rtl.css 21.6 kB
build/wc-blocks-style.css 21.6 kB
build/wc-blocks-vendors-style-rtl.css 1.28 kB
build/wc-blocks-vendors-style.css 1.28 kB
build/wc-blocks-vendors.js 65.5 kB
build/wc-blocks.js 2.96 kB
build/wc-payment-method-bacs.js 820 B
build/wc-payment-method-cheque.js 816 B
build/wc-payment-method-cod.js 912 B
build/wc-payment-method-paypal.js 838 B
build/wc-payment-method-stripe.js 11.1 kB
build/wc-settings.js 2.61 kB

compressed-size-action

@@ -17,6 +17,8 @@ import './style.scss';
interface FormattedMonetaryAmountProps {
className?: string;
displayType?: NumberFormatProps[ 'displayType' ];
allowNegative?: boolean;
isAllowed?: ( formattedValue: NumberFormatValues ) => boolean;
Copy link
Member

Choose a reason for hiding this comment

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

isAllowed is rather confusing, what are we supposed to allow here? what I understood from the PR is that it doesn’t allow negative numbers, for me allowNegative should be sufficient.

Also, do we need to pass a prop to FormattedMonetaryAmount just to support negative numbers? shouldn't just support them by default from what we pass to value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isAllowed is rather confusing, what are we supposed to allow here? what I understood from the PR is that it doesn’t allow negative numbers, for me allowNegative should be sufficient.

I used isAllowed because with the function withMaxValueLimit and withMinValueLimit we can validate the input when the user changes the value in the input. About the naming, I chose isAllowed to be consistent with the props of the library react-number-format.

Also, do we need to pass a prop to FormattedMonetaryAmount just to support negative numbers? shouldn't just support them by default from what we pass to value?

Do you mean that we should implement some custom logic to check if the value is negative? I preferred to use a ready solution that react-number-format give

Copy link
Member

Choose a reason for hiding this comment

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

I still don't understand the goal of isAllowed, can't allowNegative do that instead? My comment is mostly why we need two.

Copy link
Contributor Author

@gigitux gigitux Nov 19, 2021

Choose a reason for hiding this comment

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

The goal isAllowed is to do an extra check on the input field:
If on the right input the user write 30, on the left input the user can't write 31 (and this is the goal of the functions withMinValueLimit and withMaxValueLimit)

Copy link
Member

Choose a reason for hiding this comment

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

It took me sometime to understand isAllowed. I initially thought this is a prop you introduced, I see it's part of react-format-money, Not a great naming from their side really, but I see its value now.

Comment on lines 221 to 222
// When user insert in the minimum price input a value greater than the max price,
// we update the value of the slider to 0 - max.
Copy link
Member

Choose a reason for hiding this comment

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

Can't we just reject the min inserted and default to the previous min instead of reseting max as well? this is more a UX thing and not code related but if something changes, just reset it instead of reseting two things.

Copy link
Contributor Author

@gigitux gigitux Nov 19, 2021

Choose a reason for hiding this comment

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

Can't we just reject the min inserted and default to the previous min instead of reseting max as well? this is more a UX thing and not code related but if something changes, just reset it instead of reseting two things.

My comment is probably unclear. For max, I mean the current max price. So what my code does is just reset to 0 the minimum price.

What do you think about this:

Suggested change
// When user insert in the minimum price input a value greater than the max price,
// we update the value of the slider to 0 - max.
// When the user inserts in the minimum price input a value greater than the max price,
// we set to 0 the minimum price.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this is great!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I rephrased it again because I just re-checked the logic better.

// When the user inserts in the max price input a value less or equal than the current minimum price,
// we set to 0 the minimum price.

maxConstraint: number;
minorUnit: number;
} ) => ( { floatValue }: NumberFormatValues ): boolean => {
const maxPrice = maxConstraint / 10 ** minorUnit;
Copy link
Member

Choose a reason for hiding this comment

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

We probably don't want to use this logic here given that we had issues with it, preferably we just deal with raw versions 1000 and let the final UI element handle formatting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let the final UI element handle formatting.

This is code is not about formatting the number, but it is just about validation.

The code checks that on the right input the user can't insert a number that is minor then the number that there is in the left input. About the naming of the function, what do you think about checkMaxValueLimit?

Copy link
Member

Choose a reason for hiding this comment

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

Given that withMaxValueLimit is a boolean function, anything with has_ or is_ should work. Maybe isOutsideBound? Naming is hard so don't stress it.

return floatValue !== undefined && floatValue <= maxPrice;
};

export const withMinValueLimit = ( {
Copy link
Member

Choose a reason for hiding this comment

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

A doc block explaining the goal of this function would be great.
with prefix is usually used for HOCs, I find it rather confusing here. The logic in the return is also confusing from a first look and probably deserves to be dumped down or explained.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
export const withMinValueLimit = ( {
// Check if that the value is greater than the lowest available price and minor than the current max price.
export const checkMinValueLimit = ( {

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Yes! Much better, would also add a comment to withMaxValueLimit.

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.

I haven't done a feature review yet, but the code is clean! What's left is a changelog entry and a PR body description.

@github-actions github-actions bot added this to the 6.4.0 milestone Nov 19, 2021
@Aljullu Aljullu modified the milestones: 6.4.0, 6.5.0 Nov 22, 2021
@Aljullu Aljullu modified the milestones: 6.5.0, 6.6.0 Dec 7, 2021
…products-block into fix/2695-not-allow-negative-values-filter-products-by-price
@mikejolley mikejolley modified the milestones: 6.6.0, 6.7.0 Dec 20, 2021
@ralucaStan ralucaStan modified the milestones: 6.7.0, 6.8.0 Jan 3, 2022
@gigitux gigitux requested a review from senadir January 3, 2022 11:21
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.

I've retested this and it's working fine, thank you for updating the code Luigi!

@gigitux gigitux merged commit f2a2a3e into trunk Jan 6, 2022
@gigitux gigitux deleted the fix/2695-not-allow-negative-values-filter-products-by-price branch January 6, 2022 14:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: filter by price Issues related to the Filter by Price block. type: bug The issue/PR concerns a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[GlobalStep] User is able to set negative values in "Filter Products by Price" block.
5 participants