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
This commit is contained in:
Albert Juhé Lluveras 2020-03-10 10:09:34 +01:00 committed by GitHub
parent 12f942948e
commit f14fc54faf
4 changed files with 12 additions and 33 deletions

View File

@ -61,8 +61,8 @@ const CartFrontend = ( {
const getProps = ( el ) => ( { const getProps = ( el ) => ( {
emptyCart: el.innerHTML, emptyCart: el.innerHTML,
isShippingCalculatorEnabled: isShippingCalculatorEnabled:
el.dataset.isshippingcalculatorenabled === 'true', el.dataset.isShippingCalculatorEnabled === 'true',
isShippingCostHidden: el.dataset.isshippingcosthidden === 'true', isShippingCostHidden: el.dataset.isShippingCostHidden === 'true',
} ); } );
const getErrorBoundaryProps = () => { const getErrorBoundaryProps = () => {

View File

@ -4,7 +4,6 @@
*/ */
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { useState, useEffect } from '@wordpress/element';
import { import {
SubtotalsItem, SubtotalsItem,
TotalsFeesItem, TotalsFeesItem,
@ -106,9 +105,6 @@ const Cart = ( {
} ) => { } ) => {
const { updateShippingAddress, shippingRatesLoading } = useShippingRates(); const { updateShippingAddress, shippingRatesLoading } = useShippingRates();
const shippingAddress = shippingRates[ 0 ]?.destination; const shippingAddress = shippingRates[ 0 ]?.destination;
const [ showShippingCosts, setShowShippingCosts ] = useState(
! isShippingCostHidden
);
const { const {
applyCoupon, applyCoupon,
removeCoupon, removeCoupon,
@ -116,21 +112,11 @@ const Cart = ( {
isRemovingCoupon, isRemovingCoupon,
} = useStoreCartCoupons(); } = useStoreCartCoupons();
useEffect( () => { const showShippingCosts = Boolean(
if ( ! SHIPPING_ENABLED ) { SHIPPING_ENABLED &&
return setShowShippingCosts( false ); isShippingCalculatorEnabled &&
} ( ! isShippingCostHidden || shippingAddress?.country )
if ( isShippingCalculatorEnabled ) { );
if ( isShippingCostHidden ) {
if ( shippingAddress?.country ) {
return setShowShippingCosts( true );
}
} else {
return setShowShippingCosts( true );
}
}
return setShowShippingCosts( false );
}, [ isShippingCalculatorEnabled, isShippingCostHidden, shippingAddress ] );
const totalsCurrency = getCurrencyFromPriceResponse( cartTotals ); const totalsCurrency = getCurrencyFromPriceResponse( cartTotals );

View File

@ -14,7 +14,7 @@ import {
/** /**
* Internal dependencies * Internal dependencies
*/ */
import Editor from './edit'; import edit from './edit';
import { example } from './example'; import { example } from './example';
import './style.scss'; import './style.scss';
@ -47,14 +47,7 @@ const settings = {
}, },
}, },
/** edit,
* Renders the edit view for a block.
*
* @param {Object} props Props to pass to block.
*/
edit( props ) {
return <Editor { ...props } />;
},
/** /**
* Save the props to post content. * Save the props to post content.
@ -66,8 +59,8 @@ const settings = {
isShippingCostHidden, isShippingCostHidden,
} = attributes; } = attributes;
const data = { const data = {
'data-isshippingcalculatorenabled': isShippingCalculatorEnabled, 'data-is-shipping-calculator-enabled': isShippingCalculatorEnabled,
'data-isshippingcosthidden': isShippingCostHidden, 'data-is-shipping-cost-hidden': isShippingCostHidden,
}; };
return ( return (
<div <div

View File

@ -2,7 +2,7 @@
exports[`Cart Block can be created 1`] = ` exports[`Cart Block can be created 1`] = `
"<!-- wp:woocommerce/cart --> "<!-- wp:woocommerce/cart -->
<div class=\\"wp-block-woocommerce-cart is-loading\\" data-isshippingcalculatorenabled=\\"true\\" data-isshippingcosthidden=\\"false\\"><!-- wp:image {\\"align\\":\\"center\\",\\"sizeSlug\\":\\"small\\"} --> <div class=\\"wp-block-woocommerce-cart is-loading\\" data-is-shipping-calculator-enabled=\\"true\\" data-is-shipping-cost-hidden=\\"false\\"><!-- wp:image {\\"align\\":\\"center\\",\\"sizeSlug\\":\\"small\\"} -->
<div class=\\"wp-block-image\\"><figure class=\\"aligncenter size-small\\"><img src=\\"data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzgiIGhlaWdodD0iMz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