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';
|
2021-05-10 09:03:30 +00:00
|
|
|
|
import type { PackageRateOption } from '@woocommerce/type-defs/shipping';
|
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';
|
2021-05-10 09:03:30 +00:00
|
|
|
|
import type { CartShippingPackageShippingRate } from '@woocommerce/type-defs/cart';
|
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;
|
|
|
|
|
|
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;
|
|
|
|
|
renderOption: PackageRateRenderOption;
|
|
|
|
|
collapse?: boolean;
|
|
|
|
|
packageData: PackageData;
|
2021-03-09 10:55:24 +00:00
|
|
|
|
className?: string;
|
|
|
|
|
collapsible?: boolean;
|
|
|
|
|
noResultsMessage: ReactElement;
|
2021-05-10 09:03:30 +00:00
|
|
|
|
showItems?: boolean;
|
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,
|
2020-02-14 03:43:13 +00:00
|
|
|
|
className,
|
|
|
|
|
noResultsMessage,
|
|
|
|
|
renderOption,
|
2021-01-28 14:24:01 +00:00
|
|
|
|
packageData,
|
|
|
|
|
collapsible = false,
|
|
|
|
|
collapse = false,
|
|
|
|
|
showItems = false,
|
2021-03-09 10:55:24 +00:00
|
|
|
|
}: PackageProps ): ReactElement => {
|
2021-01-28 14:24:01 +00:00
|
|
|
|
const { selectShippingRate, selectedShippingRate } = useSelectShippingRate(
|
|
|
|
|
packageId,
|
|
|
|
|
packageData.shipping_rates
|
|
|
|
|
);
|
|
|
|
|
|
2020-03-09 11:28:26 +00:00
|
|
|
|
const header = (
|
|
|
|
|
<>
|
2021-01-28 14:24:01 +00:00
|
|
|
|
{ ( showItems || collapsible ) && (
|
2021-05-17 14:00:57 +00:00
|
|
|
|
<div className="wc-block-components-shipping-rates-control__package-title">
|
2021-01-28 14:24:01 +00:00
|
|
|
|
{ packageData.name }
|
2021-05-17 14:00:57 +00:00
|
|
|
|
</div>
|
2020-02-27 18:28:36 +00:00
|
|
|
|
) }
|
2020-02-14 03:43:13 +00:00
|
|
|
|
{ showItems && (
|
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 }
|
|
|
|
|
onSelectRate={ selectShippingRate }
|
|
|
|
|
selected={ selectedShippingRate }
|
2020-03-09 11:28:26 +00:00
|
|
|
|
renderOption={ renderOption }
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
if ( collapsible ) {
|
|
|
|
|
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"
|
2021-01-28 14:24:01 +00:00
|
|
|
|
initialOpen={ ! collapse }
|
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;
|