From db4f7820c63a58615f989754f24e24c8f0dda2c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Wed, 8 Apr 2020 12:19:05 +0200 Subject: [PATCH] Use new util functions in ShippingRatesControl (https://github.com/woocommerce/woocommerce-blocks/pull/2153) * Use new util functions in ShippingRatesControl * Import from base-utils --- .../cart-checkout/shipping-rates-control/index.js | 11 ++++++----- .../woocommerce-blocks/assets/js/base/utils/index.js | 1 + .../utils.js => base/utils/shipping-rates.js} | 2 +- .../assets/js/blocks/cart-checkout/checkout/block.js | 10 +++++----- 4 files changed, 13 insertions(+), 11 deletions(-) rename plugins/woocommerce-blocks/assets/js/{blocks/cart-checkout/checkout/utils.js => base/utils/shipping-rates.js} (92%) diff --git a/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/shipping-rates-control/index.js b/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/shipping-rates-control/index.js index 9145ac5233f..e7911152a70 100644 --- a/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/shipping-rates-control/index.js +++ b/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/shipping-rates-control/index.js @@ -6,6 +6,10 @@ import { useEffect } from 'react'; import PropTypes from 'prop-types'; import { speak } from '@wordpress/a11y'; import LoadingMask from '@woocommerce/base-components/loading-mask'; +import { + getShippingRatesPackageCount, + getShippingRatesRateCount, +} from '@woocommerce/base-utils'; /** * Internal dependencies @@ -25,11 +29,8 @@ const ShippingRatesControl = ( { if ( shippingRatesLoading ) { return; } - const packages = shippingRates.length; - const shippingOptions = shippingRates.reduce( - ( acc, shippingRate ) => acc + shippingRate.shipping_rates.length, - 0 - ); + const packages = getShippingRatesPackageCount( shippingRates ); + const shippingOptions = getShippingRatesRateCount( shippingRates ); if ( shippingOptions === 0 ) { speak( __( diff --git a/plugins/woocommerce-blocks/assets/js/base/utils/index.js b/plugins/woocommerce-blocks/assets/js/base/utils/index.js index 1d3036b6ac5..3a9c2fda870 100644 --- a/plugins/woocommerce-blocks/assets/js/base/utils/index.js +++ b/plugins/woocommerce-blocks/assets/js/base/utils/index.js @@ -1,3 +1,4 @@ export * from './errors'; export * from './price'; export * from './address'; +export * from './shipping-rates'; diff --git a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/utils.js b/plugins/woocommerce-blocks/assets/js/base/utils/shipping-rates.js similarity index 92% rename from plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/utils.js rename to plugins/woocommerce-blocks/assets/js/base/utils/shipping-rates.js index f3e9ea6cd83..19e5799dd68 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/utils.js +++ b/plugins/woocommerce-blocks/assets/js/base/utils/shipping-rates.js @@ -4,7 +4,7 @@ * @param {Array} shippingRates Shipping rates and packages array. */ export const getShippingRatesPackageCount = ( shippingRates ) => { - return Object.keys( shippingRates ).length; + return shippingRates.length; }; /** diff --git a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/block.js b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/block.js index 57d3a28718c..c4f8c1163cd 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/block.js +++ b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/block.js @@ -15,7 +15,11 @@ import { } from '@woocommerce/base-components/cart-checkout'; import { ValidatedTextInput } from '@woocommerce/base-components/text-input'; import CheckboxControl from '@woocommerce/base-components/checkbox-control'; -import { getCurrencyFromPriceResponse } from '@woocommerce/base-utils'; +import { + getCurrencyFromPriceResponse, + getShippingRatesPackageCount, + getShippingRatesRateCount, +} from '@woocommerce/base-utils'; import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-monetary-amount'; import { CheckoutProvider, @@ -46,10 +50,6 @@ import CheckoutSidebar from './sidebar'; import CheckoutOrderError from './checkout-order-error'; import NoShippingPlaceholder from './no-shipping-placeholder'; import './style.scss'; -import { - getShippingRatesPackageCount, - getShippingRatesRateCount, -} from './utils'; const Block = ( props ) => (