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-07-22 11:03:00 +00:00
|
|
|
import { useStoreCart, useEditorContext } from '@woocommerce/base-context';
|
2022-12-23 11:59:02 +00:00
|
|
|
import { CartResponseShippingRate } from '@woocommerce/types';
|
2021-05-10 09:03:30 +00:00
|
|
|
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,
|
2022-12-15 14:17:22 +00:00
|
|
|
TernaryFlag,
|
2021-05-10 09:03:30 +00:00
|
|
|
} from '../shipping-rates-control-package';
|
|
|
|
|
|
|
|
interface PackagesProps {
|
|
|
|
packages: CartResponseShippingRate[];
|
2022-12-15 14:17:22 +00:00
|
|
|
collapsible?: TernaryFlag;
|
|
|
|
showItems?: TernaryFlag;
|
2021-05-10 09:03:30 +00:00
|
|
|
noResultsMessage: ReactElement;
|
2022-12-15 14:45:17 +00:00
|
|
|
renderOption?: PackageRateRenderOption | undefined;
|
2021-05-10 09:03:30 +00:00
|
|
|
}
|
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.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} props Incoming props.
|
|
|
|
* @param {Array} props.packages Array of packages.
|
2022-12-15 14:17:22 +00:00
|
|
|
* @param {boolean|undefined} props.collapsible If the package should be rendered as a
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {ReactElement} props.noResultsMessage Rendered when there are no rates in a package.
|
|
|
|
* collapsible panel.
|
2022-12-15 14:17:22 +00:00
|
|
|
* @param {boolean|undefined} props.showItems If we should items below the package name.
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {PackageRateRenderOption} [props.renderOption] Function to render a shipping rate.
|
2021-05-10 09:03:30 +00:00
|
|
|
* @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,
|
|
|
|
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 }
|
2022-12-15 14:17:22 +00:00
|
|
|
collapsible={ collapsible }
|
|
|
|
showItems={ showItems }
|
2021-05-10 09:03:30 +00:00
|
|
|
noResultsMessage={ noResultsMessage }
|
|
|
|
renderOption={ renderOption }
|
|
|
|
/>
|
|
|
|
) ) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2021-01-28 14:24:01 +00:00
|
|
|
|
2021-05-10 09:03:30 +00:00
|
|
|
interface ShippingRatesControlProps {
|
2022-12-15 14:17:22 +00:00
|
|
|
collapsible?: TernaryFlag;
|
|
|
|
showItems?: TernaryFlag;
|
2021-05-10 09:03:30 +00:00
|
|
|
shippingRates: CartResponseShippingRate[];
|
|
|
|
className?: string;
|
2022-03-14 11:29:25 +00:00
|
|
|
isLoadingRates: boolean;
|
2021-05-10 09:03:30 +00:00
|
|
|
noResultsMessage: ReactElement;
|
2022-12-15 14:45:17 +00:00
|
|
|
renderOption?: PackageRateRenderOption | undefined;
|
2022-02-22 09:33:52 +00:00
|
|
|
context: 'woocommerce/cart' | 'woocommerce/checkout';
|
2021-05-10 09:03:30 +00:00
|
|
|
}
|
2021-01-28 14:24:01 +00:00
|
|
|
/**
|
|
|
|
* Renders the shipping rates control element.
|
|
|
|
*
|
2022-12-15 14:17:22 +00:00
|
|
|
* @param {Object} props Incoming props.
|
|
|
|
* @param {Array} props.shippingRates Array of packages containing shipping rates.
|
|
|
|
* @param {boolean} props.isLoadingRates True when rates are being loaded.
|
|
|
|
* @param {string} props.className Class name for package rates.
|
|
|
|
* @param {boolean|undefined} [props.collapsible] If true, when multiple packages are rendered they can be toggled open and closed.
|
|
|
|
* @param {boolean|undefined} [props.showItems] If true, when multiple packages are rendered, you can see each package's items.
|
|
|
|
* @param {ReactElement} props.noResultsMessage Rendered when there are no packages.
|
|
|
|
* @param {Function} [props.renderOption] Function to render a shipping rate.
|
|
|
|
* @param {string} [props.context] String equal to the block name where the Slot is rendered
|
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,
|
2022-03-14 11:29:25 +00:00
|
|
|
isLoadingRates,
|
2020-02-14 03:43:13 +00:00
|
|
|
className,
|
2022-12-15 14:17:22 +00:00
|
|
|
collapsible,
|
|
|
|
showItems,
|
2020-02-14 03:43:13 +00:00
|
|
|
noResultsMessage,
|
|
|
|
renderOption,
|
2022-02-22 09:33:52 +00:00
|
|
|
context,
|
2021-05-10 09:03:30 +00:00
|
|
|
}: ShippingRatesControlProps ): JSX.Element => {
|
2020-03-30 12:43:42 +00:00
|
|
|
useEffect( () => {
|
2022-03-14 11:29:25 +00:00
|
|
|
if ( isLoadingRates ) {
|
2020-03-30 12:43:42 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-03-14 11:29:25 +00:00
|
|
|
}, [ isLoadingRates, shippingRates ] );
|
2020-03-30 12:43:42 +00:00
|
|
|
|
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-09-13 13:49:46 +00:00
|
|
|
const { extensions, receiveCart, ...cart } = useStoreCart();
|
2021-04-08 12:31:12 +00:00
|
|
|
const slotFillProps = {
|
|
|
|
className,
|
|
|
|
collapsible,
|
2022-12-15 14:17:22 +00:00
|
|
|
showItems,
|
2021-04-08 12:31:12 +00:00
|
|
|
noResultsMessage,
|
|
|
|
renderOption,
|
|
|
|
extensions,
|
|
|
|
cart,
|
|
|
|
components: {
|
|
|
|
ShippingRatesControlPackage,
|
|
|
|
},
|
2022-02-22 09:33:52 +00:00
|
|
|
context,
|
2021-04-08 12:31:12 +00:00
|
|
|
};
|
2021-07-22 11:03:00 +00:00
|
|
|
const { isEditor } = useEditorContext();
|
2021-04-08 12:31:12 +00:00
|
|
|
|
2020-02-25 09:32:59 +00:00
|
|
|
return (
|
2020-02-27 18:28:36 +00:00
|
|
|
<LoadingMask
|
2022-03-14 11:29:25 +00:00
|
|
|
isLoading={ isLoadingRates }
|
2020-02-27 18:28:36 +00:00
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Loading shipping rates…',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
showSpinner={ true }
|
|
|
|
>
|
2021-07-22 11:03:00 +00:00
|
|
|
{ isEditor ? (
|
2021-01-28 14:24:01 +00:00
|
|
|
<Packages
|
|
|
|
packages={ shippingRates }
|
|
|
|
noResultsMessage={ noResultsMessage }
|
2021-02-09 08:10:45 +00:00
|
|
|
renderOption={ renderOption }
|
2021-01-28 14:24:01 +00:00
|
|
|
/>
|
2021-07-22 11:03:00 +00:00
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<ExperimentalOrderShippingPackages.Slot
|
|
|
|
{ ...slotFillProps }
|
|
|
|
/>
|
|
|
|
<ExperimentalOrderShippingPackages>
|
|
|
|
<Packages
|
|
|
|
packages={ shippingRates }
|
|
|
|
noResultsMessage={ noResultsMessage }
|
|
|
|
renderOption={ renderOption }
|
|
|
|
/>
|
|
|
|
</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;
|