2020-02-14 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-30 12:43:42 +00:00
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
|
|
|
import { useEffect } from 'react';
|
2020-02-14 03:43:13 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-03-30 12:43:42 +00:00
|
|
|
import { speak } from '@wordpress/a11y';
|
2020-03-30 13:04:27 +00:00
|
|
|
import LoadingMask from '@woocommerce/base-components/loading-mask';
|
2020-04-08 10:19:05 +00:00
|
|
|
import {
|
|
|
|
getShippingRatesPackageCount,
|
|
|
|
getShippingRatesRateCount,
|
|
|
|
} from '@woocommerce/base-utils';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-02-25 09:32:59 +00:00
|
|
|
import Packages from './packages';
|
|
|
|
import './style.scss';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
const ShippingRatesControl = ( {
|
2020-03-05 19:54:05 +00:00
|
|
|
shippingRates,
|
|
|
|
shippingRatesLoading,
|
2020-02-14 03:43:13 +00:00
|
|
|
className,
|
2020-03-09 11:28:26 +00:00
|
|
|
collapsibleWhenMultiple = false,
|
2020-02-14 03:43:13 +00:00
|
|
|
noResultsMessage,
|
|
|
|
renderOption,
|
|
|
|
} ) => {
|
2020-03-30 12:43:42 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( shippingRatesLoading ) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-08 10:19:05 +00:00
|
|
|
const packages = getShippingRatesPackageCount( shippingRates );
|
|
|
|
const shippingOptions = getShippingRatesRateCount( shippingRates );
|
2020-03-30 12:43:42 +00:00
|
|
|
if ( shippingOptions === 0 ) {
|
|
|
|
speak(
|
|
|
|
__(
|
|
|
|
'No shipping options were found.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else if ( packages === 1 ) {
|
|
|
|
speak(
|
|
|
|
sprintf(
|
|
|
|
// translators: %d number of shipping options found.
|
|
|
|
_n(
|
|
|
|
'%d shipping option was found.',
|
|
|
|
'%d shipping options were found.',
|
|
|
|
shippingOptions,
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
shippingOptions
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
speak(
|
|
|
|
sprintf(
|
|
|
|
// translators: %d number of shipping packages packages.
|
|
|
|
_n(
|
|
|
|
'Shipping option searched for %d package.',
|
|
|
|
'Shipping options searched for %d packages.',
|
|
|
|
packages,
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
packages
|
|
|
|
) +
|
|
|
|
' ' +
|
|
|
|
sprintf(
|
|
|
|
// translators: %d number of shipping options available.
|
|
|
|
_n(
|
|
|
|
'%d shipping option was found',
|
|
|
|
'%d shipping options were found',
|
|
|
|
shippingOptions,
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
shippingOptions
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [ shippingRatesLoading, shippingRates ] );
|
|
|
|
|
2020-02-25 09:32:59 +00:00
|
|
|
return (
|
2020-02-27 18:28:36 +00:00
|
|
|
<LoadingMask
|
|
|
|
isLoading={ shippingRatesLoading }
|
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Loading shipping rates…',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
showSpinner={ true }
|
|
|
|
>
|
|
|
|
<Packages
|
|
|
|
className={ className }
|
2020-03-09 11:28:26 +00:00
|
|
|
collapsible={
|
2020-03-26 12:43:11 +00:00
|
|
|
shippingRates.length > 1 && collapsibleWhenMultiple
|
2020-03-09 11:28:26 +00:00
|
|
|
}
|
2020-02-27 18:28:36 +00:00
|
|
|
noResultsMessage={ noResultsMessage }
|
|
|
|
renderOption={ renderOption }
|
2020-03-26 12:43:11 +00:00
|
|
|
shippingRates={ shippingRates }
|
2020-02-27 18:28:36 +00:00
|
|
|
/>
|
|
|
|
</LoadingMask>
|
2020-02-25 09:32:59 +00:00
|
|
|
);
|
2020-02-14 03:43:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ShippingRatesControl.propTypes = {
|
|
|
|
noResultsMessage: PropTypes.string.isRequired,
|
|
|
|
renderOption: PropTypes.func.isRequired,
|
|
|
|
className: PropTypes.string,
|
2020-03-09 11:28:26 +00:00
|
|
|
collapsibleWhenMultiple: PropTypes.bool,
|
2020-03-10 10:55:19 +00:00
|
|
|
shippingRates: PropTypes.array,
|
|
|
|
shippingRatesLoading: PropTypes.bool,
|
2020-02-14 03:43:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ShippingRatesControl;
|
2020-02-25 09:32:59 +00:00
|
|
|
export { Packages };
|