From f14fc54faff4e399742ba842f8a6790c45617c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Tue, 10 Mar 2020 10:09:34 +0100 Subject: [PATCH] Refactor 'Hide shipping costs' Cart attribute logic (https://github.com/woocommerce/woocommerce-blocks/pull/1909) * Use kebab-case for Cart block attributes * Simplify Cart edit function * Refactor showShippingCosts logic * Update snapshots --- .../js/blocks/cart-checkout/cart/frontend.js | 4 ++-- .../cart-checkout/cart/full-cart/index.js | 24 ++++--------------- .../js/blocks/cart-checkout/cart/index.js | 15 ++++-------- .../backend/__snapshots__/cart.test.js.snap | 2 +- 4 files changed, 12 insertions(+), 33 deletions(-) diff --git a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/frontend.js b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/frontend.js index 0d5af3b0bd1..b3abf2bdf06 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/frontend.js +++ b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/frontend.js @@ -61,8 +61,8 @@ const CartFrontend = ( { const getProps = ( el ) => ( { emptyCart: el.innerHTML, isShippingCalculatorEnabled: - el.dataset.isshippingcalculatorenabled === 'true', - isShippingCostHidden: el.dataset.isshippingcosthidden === 'true', + el.dataset.isShippingCalculatorEnabled === 'true', + isShippingCostHidden: el.dataset.isShippingCostHidden === 'true', } ); const getErrorBoundaryProps = () => { diff --git a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/full-cart/index.js b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/full-cart/index.js index 16aaddf6846..67ab27ff799 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/full-cart/index.js +++ b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/full-cart/index.js @@ -4,7 +4,6 @@ */ import PropTypes from 'prop-types'; import { __ } from '@wordpress/i18n'; -import { useState, useEffect } from '@wordpress/element'; import { SubtotalsItem, TotalsFeesItem, @@ -106,9 +105,6 @@ const Cart = ( { } ) => { const { updateShippingAddress, shippingRatesLoading } = useShippingRates(); const shippingAddress = shippingRates[ 0 ]?.destination; - const [ showShippingCosts, setShowShippingCosts ] = useState( - ! isShippingCostHidden - ); const { applyCoupon, removeCoupon, @@ -116,21 +112,11 @@ const Cart = ( { isRemovingCoupon, } = useStoreCartCoupons(); - useEffect( () => { - if ( ! SHIPPING_ENABLED ) { - return setShowShippingCosts( false ); - } - if ( isShippingCalculatorEnabled ) { - if ( isShippingCostHidden ) { - if ( shippingAddress?.country ) { - return setShowShippingCosts( true ); - } - } else { - return setShowShippingCosts( true ); - } - } - return setShowShippingCosts( false ); - }, [ isShippingCalculatorEnabled, isShippingCostHidden, shippingAddress ] ); + const showShippingCosts = Boolean( + SHIPPING_ENABLED && + isShippingCalculatorEnabled && + ( ! isShippingCostHidden || shippingAddress?.country ) + ); const totalsCurrency = getCurrencyFromPriceResponse( cartTotals ); diff --git a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/index.js b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/index.js index 824621054c3..6bf5784b74d 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/index.js +++ b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/index.js @@ -14,7 +14,7 @@ import { /** * Internal dependencies */ -import Editor from './edit'; +import edit from './edit'; import { example } from './example'; import './style.scss'; @@ -47,14 +47,7 @@ const settings = { }, }, - /** - * Renders the edit view for a block. - * - * @param {Object} props Props to pass to block. - */ - edit( props ) { - return ; - }, + edit, /** * Save the props to post content. @@ -66,8 +59,8 @@ const settings = { isShippingCostHidden, } = attributes; const data = { - 'data-isshippingcalculatorenabled': isShippingCalculatorEnabled, - 'data-isshippingcosthidden': isShippingCostHidden, + 'data-is-shipping-calculator-enabled': isShippingCalculatorEnabled, + 'data-is-shipping-cost-hidden': isShippingCostHidden, }; return (
-
+
\\"\\"/