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';
|
2021-03-09 10:55:24 +00:00
|
|
|
|
import type { ReactElement } from 'react';
|
2022-12-23 11:59:02 +00:00
|
|
|
|
import type {
|
|
|
|
|
PackageRateOption,
|
|
|
|
|
CartShippingPackageShippingRate,
|
|
|
|
|
} from '@woocommerce/types';
|
2021-07-02 09:24:07 +00:00
|
|
|
|
import { Panel } from '@woocommerce/blocks-checkout';
|
|
|
|
|
import Label from '@woocommerce/base-components/label';
|
2021-04-08 12:31:12 +00:00
|
|
|
|
import { useSelectShippingRate } from '@woocommerce/base-context/hooks';
|
2022-09-26 16:16:44 +00:00
|
|
|
|
import { sanitizeHTML } from '@woocommerce/utils';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
2021-01-28 14:24:01 +00:00
|
|
|
|
import PackageRates from './package-rates';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
import './style.scss';
|
|
|
|
|
|
2021-03-09 10:55:24 +00:00
|
|
|
|
interface PackageItem {
|
|
|
|
|
name: string;
|
|
|
|
|
key: string;
|
|
|
|
|
quantity: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface Destination {
|
|
|
|
|
address_1: string;
|
|
|
|
|
address_2: string;
|
|
|
|
|
city: string;
|
|
|
|
|
state: string;
|
|
|
|
|
postcode: string;
|
|
|
|
|
country: string;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 09:03:30 +00:00
|
|
|
|
export interface PackageData {
|
|
|
|
|
destination: Destination;
|
|
|
|
|
name: string;
|
|
|
|
|
shipping_rates: CartShippingPackageShippingRate[];
|
|
|
|
|
items: PackageItem[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type PackageRateRenderOption = (
|
|
|
|
|
option: CartShippingPackageShippingRate
|
|
|
|
|
) => PackageRateOption;
|
|
|
|
|
|
2022-12-15 14:17:22 +00:00
|
|
|
|
// A flag can be ternary if true, false, and undefined are all valid options.
|
|
|
|
|
// In our case, we use this for collapsible and showItems, having a boolean will force that
|
|
|
|
|
// option, having undefined will let the component decide the logic based on other factors.
|
|
|
|
|
export type TernaryFlag = boolean | undefined;
|
2021-03-09 10:55:24 +00:00
|
|
|
|
interface PackageProps {
|
2021-05-10 09:03:30 +00:00
|
|
|
|
/* PackageId can be a string, WooCommerce Subscriptions uses strings for example, but WooCommerce core uses numbers */
|
|
|
|
|
packageId: string | number;
|
2022-12-15 14:45:17 +00:00
|
|
|
|
renderOption?: PackageRateRenderOption | undefined;
|
2021-05-10 09:03:30 +00:00
|
|
|
|
collapse?: boolean;
|
|
|
|
|
packageData: PackageData;
|
2021-03-09 10:55:24 +00:00
|
|
|
|
className?: string;
|
2022-12-15 14:17:22 +00:00
|
|
|
|
collapsible?: TernaryFlag;
|
2021-03-09 10:55:24 +00:00
|
|
|
|
noResultsMessage: ReactElement;
|
2022-12-15 14:17:22 +00:00
|
|
|
|
showItems?: TernaryFlag;
|
2021-03-09 10:55:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
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 => {
|
2022-02-10 11:59:43 +00:00
|
|
|
|
const { selectShippingRate } = useSelectShippingRate();
|
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
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
const body = (
|
2021-01-28 14:24:01 +00:00
|
|
|
|
<PackageRates
|
2020-03-09 11:28:26 +00:00
|
|
|
|
className={ className }
|
|
|
|
|
noResultsMessage={ noResultsMessage }
|
2021-01-28 14:24:01 +00:00
|
|
|
|
rates={ packageData.shipping_rates }
|
2022-02-10 11:59:43 +00:00
|
|
|
|
onSelectRate={ ( newShippingRateId ) =>
|
|
|
|
|
selectShippingRate( newShippingRateId, packageId )
|
|
|
|
|
}
|
|
|
|
|
selectedRate={ packageData.shipping_rates.find(
|
|
|
|
|
( rate ) => rate.selected
|
|
|
|
|
) }
|
2020-03-09 11:28:26 +00:00
|
|
|
|
renderOption={ renderOption }
|
|
|
|
|
/>
|
|
|
|
|
);
|
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
|
2020-06-17 09:53:42 +00:00
|
|
|
|
className="wc-block-components-shipping-rates-control__package"
|
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
|
|
|
|
>
|
2020-06-05 10:00:19 +00:00
|
|
|
|
{ body }
|
|
|
|
|
</Panel>
|
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',
|
2020-03-09 11:28:26 +00:00
|
|
|
|
className
|
|
|
|
|
) }
|
|
|
|
|
>
|
|
|
|
|
{ header }
|
|
|
|
|
{ body }
|
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;
|