2020-02-14 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-30 12:43:42 +00:00
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2021-01-28 14:24:01 +00:00
|
|
|
import { useEffect } from '@wordpress/element';
|
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';
|
2021-04-08 12:31:12 +00:00
|
|
|
import { ExperimentalOrderShippingPackages } from '@woocommerce/blocks-checkout';
|
2020-04-08 10:19:05 +00:00
|
|
|
import {
|
|
|
|
getShippingRatesPackageCount,
|
|
|
|
getShippingRatesRateCount,
|
|
|
|
} from '@woocommerce/base-utils';
|
2021-04-08 12:31:12 +00:00
|
|
|
import { useStoreCart } from '@woocommerce/base-context/hooks';
|
2021-05-10 09:03:30 +00:00
|
|
|
import { CartResponseShippingRate } from '@woocommerce/type-defs/cart-response';
|
|
|
|
import { ReactElement } from 'react';
|
2021-04-08 12:31:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-05-10 09:03:30 +00:00
|
|
|
import ShippingRatesControlPackage, {
|
|
|
|
PackageRateRenderOption,
|
|
|
|
} from '../shipping-rates-control-package';
|
|
|
|
|
|
|
|
interface PackagesProps {
|
|
|
|
packages: CartResponseShippingRate[];
|
|
|
|
collapse?: boolean;
|
|
|
|
collapsible?: boolean;
|
|
|
|
showItems?: boolean;
|
|
|
|
noResultsMessage: ReactElement;
|
|
|
|
renderOption: PackageRateRenderOption;
|
|
|
|
}
|
2020-02-14 03:43:13 +00:00
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
/**
|
2021-05-10 09:03:30 +00:00
|
|
|
* Renders multiple packages within the slotfill.
|
|
|
|
*
|
|
|
|
* @param {Object} props Incoming props.
|
|
|
|
* @param {Array} props.packages Array of packages.
|
|
|
|
* @param {boolean} props.collapsible If the package should be rendered as a
|
|
|
|
* @param {ReactElement} props.noResultsMessage Rendered when there are no rates in a package.
|
|
|
|
* collapsible panel.
|
|
|
|
* @param {boolean} props.collapse If the panel should be collapsed by default,
|
|
|
|
* only works if collapsible is true.
|
|
|
|
* @param {boolean} props.showItems If we should items below the package name.
|
|
|
|
* @param {PackageRateRenderOption} [props.renderOption] Function to render a shipping rate.
|
|
|
|
* @return {JSX.Element|null} Rendered components.
|
2021-01-28 14:24:01 +00:00
|
|
|
*/
|
2021-05-10 09:03:30 +00:00
|
|
|
const Packages = ( {
|
|
|
|
packages,
|
|
|
|
collapse,
|
|
|
|
showItems,
|
|
|
|
collapsible,
|
|
|
|
noResultsMessage,
|
|
|
|
renderOption,
|
|
|
|
}: PackagesProps ): JSX.Element | null => {
|
|
|
|
// If there are no packages, return nothing.
|
|
|
|
if ( ! packages.length ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ packages.map( ( { package_id: packageId, ...packageData } ) => (
|
|
|
|
<ShippingRatesControlPackage
|
|
|
|
key={ packageId }
|
|
|
|
packageId={ packageId }
|
|
|
|
packageData={ packageData }
|
|
|
|
collapsible={ collapsible }
|
|
|
|
collapse={ collapse }
|
|
|
|
showItems={ showItems }
|
|
|
|
noResultsMessage={ noResultsMessage }
|
|
|
|
renderOption={ renderOption }
|
|
|
|
/>
|
|
|
|
) ) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2021-01-28 14:24:01 +00:00
|
|
|
|
2021-05-10 09:03:30 +00:00
|
|
|
interface ShippingRatesControlProps {
|
|
|
|
collapsible?: boolean;
|
|
|
|
shippingRates: CartResponseShippingRate[];
|
|
|
|
className?: string;
|
|
|
|
shippingRatesLoading: boolean;
|
|
|
|
noResultsMessage: ReactElement;
|
|
|
|
renderOption: PackageRateRenderOption;
|
|
|
|
}
|
2021-01-28 14:24:01 +00:00
|
|
|
/**
|
|
|
|
* Renders the shipping rates control element.
|
|
|
|
*
|
|
|
|
* @param {Object} props Incoming props.
|
|
|
|
* @param {Array} props.shippingRates Array of packages containing shipping rates.
|
|
|
|
* @param {boolean} props.shippingRatesLoading True when rates are being loaded.
|
|
|
|
* @param {string} props.className Class name for package rates.
|
2021-02-09 08:10:45 +00:00
|
|
|
* @param {boolean} [props.collapsible] If true, when multiple packages are rendered they can be toggled open and closed.
|
2021-05-10 09:03:30 +00:00
|
|
|
* @param {ReactElement} props.noResultsMessage Rendered when there are no packages.
|
2021-02-09 08:10:45 +00:00
|
|
|
* @param {Function} [props.renderOption] Function to render a shipping rate.
|
2021-01-28 14:24:01 +00:00
|
|
|
*/
|
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,
|
2021-01-28 14:24:01 +00:00
|
|
|
collapsible = false,
|
2020-02-14 03:43:13 +00:00
|
|
|
noResultsMessage,
|
|
|
|
renderOption,
|
2021-05-10 09:03:30 +00:00
|
|
|
}: ShippingRatesControlProps ): JSX.Element => {
|
2020-03-30 12:43:42 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( shippingRatesLoading ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-28 14:24:01 +00:00
|
|
|
const packageCount = getShippingRatesPackageCount( shippingRates );
|
2020-04-08 10:19:05 +00:00
|
|
|
const shippingOptions = getShippingRatesRateCount( shippingRates );
|
2021-02-03 10:19:09 +00:00
|
|
|
if ( packageCount === 1 ) {
|
2020-03-30 12:43:42 +00:00
|
|
|
speak(
|
|
|
|
sprintf(
|
2021-02-19 11:58:44 +00:00
|
|
|
/* translators: %d number of shipping options found. */
|
2020-03-30 12:43:42 +00:00
|
|
|
_n(
|
|
|
|
'%d shipping option was found.',
|
|
|
|
'%d shipping options were found.',
|
|
|
|
shippingOptions,
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
shippingOptions
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
speak(
|
|
|
|
sprintf(
|
2021-02-19 11:58:44 +00:00
|
|
|
/* translators: %d number of shipping packages packages. */
|
2020-03-30 12:43:42 +00:00
|
|
|
_n(
|
|
|
|
'Shipping option searched for %d package.',
|
|
|
|
'Shipping options searched for %d packages.',
|
2021-01-28 14:24:01 +00:00
|
|
|
packageCount,
|
2020-03-30 12:43:42 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
2021-01-28 14:24:01 +00:00
|
|
|
packageCount
|
2020-03-30 12:43:42 +00:00
|
|
|
) +
|
|
|
|
' ' +
|
|
|
|
sprintf(
|
2021-02-19 11:58:44 +00:00
|
|
|
/* translators: %d number of shipping options available. */
|
2020-03-30 12:43:42 +00:00
|
|
|
_n(
|
|
|
|
'%d shipping option was found',
|
|
|
|
'%d shipping options were found',
|
|
|
|
shippingOptions,
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
shippingOptions
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [ shippingRatesLoading, shippingRates ] );
|
|
|
|
|
2021-04-08 12:31:12 +00:00
|
|
|
// Prepare props to pass to the ExperimentalOrderShippingPackages slot fill.
|
|
|
|
// We need to pluck out receiveCart.
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2021-05-10 09:03:30 +00:00
|
|
|
const { extensions, ...cart } = useStoreCart();
|
2021-04-08 12:31:12 +00:00
|
|
|
const slotFillProps = {
|
|
|
|
className,
|
|
|
|
collapsible,
|
|
|
|
noResultsMessage,
|
|
|
|
renderOption,
|
|
|
|
extensions,
|
|
|
|
cart,
|
|
|
|
components: {
|
|
|
|
ShippingRatesControlPackage,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
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 }
|
|
|
|
>
|
2021-04-08 12:31:12 +00:00
|
|
|
<ExperimentalOrderShippingPackages.Slot { ...slotFillProps } />
|
2021-01-28 14:24:01 +00:00
|
|
|
<ExperimentalOrderShippingPackages>
|
|
|
|
<Packages
|
|
|
|
packages={ shippingRates }
|
|
|
|
noResultsMessage={ noResultsMessage }
|
2021-02-09 08:10:45 +00:00
|
|
|
renderOption={ renderOption }
|
2021-01-28 14:24:01 +00:00
|
|
|
/>
|
|
|
|
</ExperimentalOrderShippingPackages>
|
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
|
|
|
};
|
|
|
|
|
|
|
|
export default ShippingRatesControl;
|