From ec8e39fe0fd766ea79b89333fed3f7b72b07884e Mon Sep 17 00:00:00 2001 From: "nikoloz.gabisonia" Date: Thu, 23 Jun 2022 13:54:18 +0400 Subject: [PATCH 1/4] Move event propagation cancellers from async event --- .../AddToCart/AddToCart.component.js | 14 ++++++- .../AddToCart/AddToCart.container.js | 4 +- .../component/Product/Product.container.js | 5 +++ .../ProductCard/ProductCard.component.js | 40 ++++++++++--------- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/packages/scandipwa/src/component/AddToCart/AddToCart.component.js b/packages/scandipwa/src/component/AddToCart/AddToCart.component.js index 423856a21d..fd03c4b338 100644 --- a/packages/scandipwa/src/component/AddToCart/AddToCart.component.js +++ b/packages/scandipwa/src/component/AddToCart/AddToCart.component.js @@ -35,6 +35,8 @@ export class AddToCart extends PureComponent { layout: LayoutType.isRequired }; + handleButtonClick = this.handleButtonClick.bind(this); + renderCartIcon() { const { isIconEnabled } = this.props; @@ -45,10 +47,18 @@ export class AddToCart extends PureComponent { return ; } + handleButtonClick(e) { + // Prevent container Link from triggering redirect + e.stopPropagation(); + e.preventDefault(); + + const { addProductToCart } = this.props; + addProductToCart(); + } + render() { const { mix, - addProductToCart, layout, isDisabled, isAdding @@ -56,7 +66,7 @@ export class AddToCart extends PureComponent { return ( - ); + const requiresConfiguration = this.requiresConfiguration(); + + if (inStock) { + if (requiresConfiguration) { + return ( + + ); + } + + return this.renderAddToCartButton(layout); } - if (!inStock) { - return ( + return (

{ __('Out of stock') }

- ); - } - - return this.renderAddToCartButton(layout); + ); } getConfigurableAttributes() { From c6e993e8f24f57c3f117c4622fea05c51913c6e1 Mon Sep 17 00:00:00 2001 From: "nikoloz.gabisonia" Date: Thu, 23 Jun 2022 14:01:59 +0400 Subject: [PATCH 2/4] removed useless function that I wrote --- .../scandipwa/src/component/Product/Product.container.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/scandipwa/src/component/Product/Product.container.js b/packages/scandipwa/src/component/Product/Product.container.js index 988883c7ee..34fa2d1311 100644 --- a/packages/scandipwa/src/component/Product/Product.container.js +++ b/packages/scandipwa/src/component/Product/Product.container.js @@ -391,11 +391,6 @@ export class ProductContainer extends PureComponent { } } - addToCartWithoutRedirect(e) { - e.stopPropagation(); - this.addToCart(); - } - /** * Event that validates and invokes product adding into cart * @returns {*} From 30b5ebcae5d048ff244f2a8010d75ca2a299a8de Mon Sep 17 00:00:00 2001 From: "nikoloz.gabisonia" Date: Thu, 23 Jun 2022 16:20:41 +0400 Subject: [PATCH 3/4] Grouped items also shouldn't redirect --- .../ProductCard/ProductCard.component.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/scandipwa/src/component/ProductCard/ProductCard.component.js b/packages/scandipwa/src/component/ProductCard/ProductCard.component.js index f6265a1d8d..5998ae8329 100644 --- a/packages/scandipwa/src/component/ProductCard/ProductCard.component.js +++ b/packages/scandipwa/src/component/ProductCard/ProductCard.component.js @@ -249,16 +249,18 @@ export class ProductCard extends Product { } } = this.props; - const configureBundleAndGrouped = type === PRODUCT_TYPE.bundle || type === PRODUCT_TYPE.grouped; + const configureBundle = type === PRODUCT_TYPE.bundle; + const configureConfig = (type === PRODUCT_TYPE.configurable && Object.keys(super.getConfigurableAttributes()) .length !== Object.keys(this.getConfigurableAttributes()).length) || (type === PRODUCT_TYPE.configurable && Object.values(this.getConfigurableAttributes()).some((value) => value.attribute_values.length === 0)); const configureCustomize = options.some(({ required = false }) => required); + const configureDownloadableLinks = PRODUCT_TYPE.downloadable && links_purchased_separately === 1; - return configureBundleAndGrouped || configureConfig || configureCustomize || configureDownloadableLinks; + return configureBundle || configureConfig || configureCustomize || configureDownloadableLinks; } renderAddToCart() { @@ -270,9 +272,8 @@ export class ProductCard extends Product { const requiresConfiguration = this.requiresConfiguration(); - if (inStock) { - if (requiresConfiguration) { - return ( + if (inStock && requiresConfiguration) { + return ( - ); - } - - return this.renderAddToCartButton(layout); + ); } - return ( + if (!inStock) { + return (

{ __('Out of stock') }

- ); + ); + } + + return this.renderAddToCartButton(layout); } getConfigurableAttributes() { From 3a7a7ebe1e50785dd8f5b3011a989d7a666d0e85 Mon Sep 17 00:00:00 2001 From: "nikoloz.gabisonia" Date: Thu, 23 Jun 2022 17:42:48 +0400 Subject: [PATCH 4/4] remove IF/else nesting --- .../src/component/ProductCard/ProductCard.component.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/scandipwa/src/component/ProductCard/ProductCard.component.js b/packages/scandipwa/src/component/ProductCard/ProductCard.component.js index 5998ae8329..f76d47f553 100644 --- a/packages/scandipwa/src/component/ProductCard/ProductCard.component.js +++ b/packages/scandipwa/src/component/ProductCard/ProductCard.component.js @@ -249,8 +249,7 @@ export class ProductCard extends Product { } } = this.props; - const configureBundle = type === PRODUCT_TYPE.bundle; - + const configureBundleAndGrouped = type === PRODUCT_TYPE.bundle || type === PRODUCT_TYPE.grouped; const configureConfig = (type === PRODUCT_TYPE.configurable && Object.keys(super.getConfigurableAttributes()) .length !== Object.keys(this.getConfigurableAttributes()).length) @@ -260,7 +259,7 @@ export class ProductCard extends Product { const configureDownloadableLinks = PRODUCT_TYPE.downloadable && links_purchased_separately === 1; - return configureBundle || configureConfig || configureCustomize || configureDownloadableLinks; + return configureBundleAndGrouped || configureConfig || configureCustomize || configureDownloadableLinks; } renderAddToCart() {