2020-02-14 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-12-15 23:52:03 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-01-28 14:24:01 +00:00
|
|
|
import { useEffect } from '@wordpress/element';
|
2020-03-30 13:04:27 +00:00
|
|
|
import LoadingMask from '@woocommerce/base-components/loading-mask';
|
2022-12-15 23:52:03 +00:00
|
|
|
import {
|
|
|
|
ExperimentalOrderShippingPackages,
|
|
|
|
StoreNotice,
|
|
|
|
} from '@woocommerce/blocks-checkout';
|
2020-04-08 10:19:05 +00:00
|
|
|
import {
|
|
|
|
getShippingRatesPackageCount,
|
|
|
|
getShippingRatesRateCount,
|
|
|
|
} from '@woocommerce/base-utils';
|
2022-12-15 23:52:03 +00:00
|
|
|
import {
|
|
|
|
useStoreCart,
|
|
|
|
useEditorContext,
|
|
|
|
useShippingData,
|
|
|
|
} from '@woocommerce/base-context';
|
2021-04-08 12:31:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-12-15 23:52:03 +00:00
|
|
|
import ShippingRatesControlPackage from '../shipping-rates-control-package';
|
|
|
|
import { speakFoundShippingOptions } from './utils';
|
|
|
|
import type { PackagesProps, ShippingRatesControlProps } from './types';
|
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.
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the shipping rates control element.
|
|
|
|
*/
|
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;
|
|
|
|
}
|
2022-12-15 23:52:03 +00:00
|
|
|
speakFoundShippingOptions(
|
|
|
|
getShippingRatesPackageCount( shippingRates ),
|
|
|
|
getShippingRatesRateCount( shippingRates )
|
|
|
|
);
|
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
|
|
|
};
|
2022-12-15 23:52:03 +00:00
|
|
|
const { isEditor } = useEditorContext();
|
|
|
|
const { hasSelectedLocalPickup } = useShippingData();
|
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 }
|
|
|
|
>
|
2023-01-06 14:53:53 +00:00
|
|
|
<ExperimentalOrderShippingPackages.Slot { ...slotFillProps } />
|
2023-01-12 11:43:34 +00:00
|
|
|
{ hasSelectedLocalPickup &&
|
|
|
|
shippingRates.length > 1 &&
|
|
|
|
! isEditor && (
|
|
|
|
<StoreNotice
|
|
|
|
className="wc-block-components-notice"
|
|
|
|
isDismissible={ false }
|
|
|
|
status="warning"
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Multiple shipments must have the same pickup location',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</StoreNotice>
|
|
|
|
) }
|
2023-01-06 14:53:53 +00:00
|
|
|
<ExperimentalOrderShippingPackages>
|
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
|
|
|
/>
|
2023-01-06 14:53:53 +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;
|