2020-02-14 03:43:13 +00:00
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
2021-01-28 14:24:01 +00:00
|
|
|
|
import classNames from 'classnames';
|
2020-02-25 09:32:59 +00:00
|
|
|
|
import { _n, sprintf } from '@wordpress/i18n';
|
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2023-11-10 18:56:40 +00:00
|
|
|
|
import { Label, Panel } from '@woocommerce/blocks-components';
|
2023-02-27 10:52:38 +00:00
|
|
|
|
import { useCallback } from '@wordpress/element';
|
2023-08-08 10:56:19 +00:00
|
|
|
|
import { useShippingData } from '@woocommerce/base-context/hooks';
|
2022-09-26 16:16:44 +00:00
|
|
|
|
import { sanitizeHTML } from '@woocommerce/utils';
|
2023-03-02 14:26:00 +00:00
|
|
|
|
import type { ReactElement } from 'react';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
2021-01-28 14:24:01 +00:00
|
|
|
|
import PackageRates from './package-rates';
|
2022-12-15 23:52:03 +00:00
|
|
|
|
import type { PackageProps } from './types';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
import './style.scss';
|
|
|
|
|
|
2021-04-08 12:31:12 +00:00
|
|
|
|
export const ShippingRatesControlPackage = ( {
|
2021-01-28 14:24:01 +00:00
|
|
|
|
packageId,
|
2022-02-10 11:59:43 +00:00
|
|
|
|
className = '',
|
2020-02-14 03:43:13 +00:00
|
|
|
|
noResultsMessage,
|
|
|
|
|
renderOption,
|
2021-01-28 14:24:01 +00:00
|
|
|
|
packageData,
|
2022-12-15 14:17:22 +00:00
|
|
|
|
collapsible,
|
|
|
|
|
showItems,
|
2021-03-09 10:55:24 +00:00
|
|
|
|
}: PackageProps ): ReactElement => {
|
2023-08-08 10:56:19 +00:00
|
|
|
|
const { selectShippingRate, isSelectingRate } = useShippingData();
|
2022-12-15 14:17:22 +00:00
|
|
|
|
const multiplePackages =
|
|
|
|
|
document.querySelectorAll(
|
|
|
|
|
'.wc-block-components-shipping-rates-control__package'
|
|
|
|
|
).length > 1;
|
|
|
|
|
|
|
|
|
|
// If showItems is not set, we check if we have multiple packages.
|
|
|
|
|
// We sometimes don't want to show items even if we have multiple packages.
|
|
|
|
|
const shouldShowItems = showItems ?? multiplePackages;
|
|
|
|
|
|
|
|
|
|
// If collapsible is not set, we check if we have multiple packages.
|
|
|
|
|
// We sometimes don't want to collapse even if we have multiple packages.
|
|
|
|
|
const shouldBeCollapsible = collapsible ?? multiplePackages;
|
2021-01-28 14:24:01 +00:00
|
|
|
|
|
2020-03-09 11:28:26 +00:00
|
|
|
|
const header = (
|
|
|
|
|
<>
|
2022-12-15 14:17:22 +00:00
|
|
|
|
{ ( shouldBeCollapsible || shouldShowItems ) && (
|
2022-09-26 16:16:44 +00:00
|
|
|
|
<div
|
|
|
|
|
className="wc-block-components-shipping-rates-control__package-title"
|
2022-10-05 09:59:38 +00:00
|
|
|
|
dangerouslySetInnerHTML={ {
|
|
|
|
|
__html: sanitizeHTML( packageData.name ),
|
|
|
|
|
} }
|
2022-09-26 16:16:44 +00:00
|
|
|
|
/>
|
2020-02-27 18:28:36 +00:00
|
|
|
|
) }
|
2022-12-15 14:17:22 +00:00
|
|
|
|
{ shouldShowItems && (
|
2020-06-17 09:53:42 +00:00
|
|
|
|
<ul className="wc-block-components-shipping-rates-control__package-items">
|
2021-01-28 14:24:01 +00:00
|
|
|
|
{ Object.values( packageData.items ).map( ( v ) => {
|
2020-02-25 09:32:59 +00:00
|
|
|
|
const name = decodeEntities( v.name );
|
|
|
|
|
const quantity = v.quantity;
|
|
|
|
|
return (
|
|
|
|
|
<li
|
2021-02-10 11:37:58 +00:00
|
|
|
|
key={ v.key }
|
2020-06-17 09:53:42 +00:00
|
|
|
|
className="wc-block-components-shipping-rates-control__package-item"
|
2020-02-25 09:32:59 +00:00
|
|
|
|
>
|
|
|
|
|
<Label
|
2021-01-28 14:24:01 +00:00
|
|
|
|
label={
|
|
|
|
|
quantity > 1
|
|
|
|
|
? `${ name } × ${ quantity }`
|
|
|
|
|
: `${ name }`
|
|
|
|
|
}
|
2020-02-25 09:32:59 +00:00
|
|
|
|
screenReaderLabel={ sprintf(
|
2021-02-19 11:58:44 +00:00
|
|
|
|
/* translators: %1$s name of the product (ie: Sunglasses), %2$d number of units in the current cart package */
|
2020-02-25 09:32:59 +00:00
|
|
|
|
_n(
|
2020-09-26 20:05:00 +00:00
|
|
|
|
'%1$s (%2$d unit)',
|
|
|
|
|
'%1$s (%2$d units)',
|
2020-02-25 09:32:59 +00:00
|
|
|
|
quantity,
|
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
|
),
|
|
|
|
|
name,
|
|
|
|
|
quantity
|
|
|
|
|
) }
|
|
|
|
|
/>
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
} ) }
|
|
|
|
|
</ul>
|
2020-02-14 03:43:13 +00:00
|
|
|
|
) }
|
2020-03-09 11:28:26 +00:00
|
|
|
|
</>
|
|
|
|
|
);
|
2023-04-28 10:29:45 +00:00
|
|
|
|
|
|
|
|
|
const onSelectRate = useCallback(
|
|
|
|
|
( newShippingRateId: string ) => {
|
|
|
|
|
selectShippingRate( newShippingRateId, packageId );
|
|
|
|
|
},
|
2023-08-08 10:56:19 +00:00
|
|
|
|
[ packageId, selectShippingRate ]
|
2023-02-27 10:52:38 +00:00
|
|
|
|
);
|
|
|
|
|
const packageRatesProps = {
|
|
|
|
|
className,
|
|
|
|
|
noResultsMessage,
|
|
|
|
|
rates: packageData.shipping_rates,
|
2023-08-08 10:56:19 +00:00
|
|
|
|
onSelectRate,
|
2022-12-15 23:52:03 +00:00
|
|
|
|
selectedRate: packageData.shipping_rates.find(
|
|
|
|
|
( rate ) => rate.selected
|
|
|
|
|
),
|
|
|
|
|
renderOption,
|
2023-08-08 10:56:19 +00:00
|
|
|
|
disabled: isSelectingRate,
|
2022-12-15 23:52:03 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-12-15 14:17:22 +00:00
|
|
|
|
if ( shouldBeCollapsible ) {
|
2020-03-09 11:28:26 +00:00
|
|
|
|
return (
|
2020-06-05 10:00:19 +00:00
|
|
|
|
<Panel
|
2023-08-08 10:56:19 +00:00
|
|
|
|
className={ classNames(
|
|
|
|
|
'wc-block-components-shipping-rates-control__package',
|
|
|
|
|
className,
|
|
|
|
|
{
|
|
|
|
|
'wc-block-components-shipping-rates-control__package--disabled':
|
|
|
|
|
isSelectingRate,
|
|
|
|
|
}
|
|
|
|
|
) }
|
2022-12-15 14:17:22 +00:00
|
|
|
|
// initialOpen remembers only the first value provided to it, so by the
|
|
|
|
|
// time we know we have several packages, initialOpen would be hardcoded to true.
|
|
|
|
|
// If we're rendering a panel, we're more likely rendering several
|
|
|
|
|
// packages and we want to them to be closed initially.
|
|
|
|
|
initialOpen={ false }
|
2020-06-05 10:00:19 +00:00
|
|
|
|
title={ header }
|
2020-03-09 11:28:26 +00:00
|
|
|
|
>
|
2022-12-15 23:52:03 +00:00
|
|
|
|
<PackageRates { ...packageRatesProps } />
|
2020-06-05 10:00:19 +00:00
|
|
|
|
</Panel>
|
2020-03-09 11:28:26 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
2022-12-15 23:52:03 +00:00
|
|
|
|
|
2020-03-09 11:28:26 +00:00
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={ classNames(
|
2020-06-17 09:53:42 +00:00
|
|
|
|
'wc-block-components-shipping-rates-control__package',
|
2023-08-08 10:56:19 +00:00
|
|
|
|
className,
|
|
|
|
|
{
|
|
|
|
|
'wc-block-components-shipping-rates-control__package--disabled':
|
|
|
|
|
isSelectingRate,
|
|
|
|
|
}
|
2020-03-09 11:28:26 +00:00
|
|
|
|
) }
|
|
|
|
|
>
|
|
|
|
|
{ header }
|
2022-12-15 23:52:03 +00:00
|
|
|
|
<PackageRates { ...packageRatesProps } />
|
2020-02-27 18:28:36 +00:00
|
|
|
|
</div>
|
2020-02-14 03:43:13 +00:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-08 12:31:12 +00:00
|
|
|
|
export default ShippingRatesControlPackage;
|