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';
|
|
|
|
|
import Label from '@woocommerce/base-components/label';
|
2020-06-05 10:00:19 +00:00
|
|
|
|
import Title from '@woocommerce/base-components/title';
|
2021-03-09 10:55:24 +00:00
|
|
|
|
import type { ReactElement } from 'react';
|
|
|
|
|
import type { Rate, PackageRateOption } from '@woocommerce/type-defs/shipping';
|
2021-04-08 12:31:12 +00:00
|
|
|
|
import { Panel } from '@woocommerce/blocks-checkout';
|
|
|
|
|
import { useSelectShippingRate } from '@woocommerce/base-context/hooks';
|
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 {
|
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
|
address_1: string;
|
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
|
address_2: string;
|
|
|
|
|
city: string;
|
|
|
|
|
state: string;
|
|
|
|
|
postcode: string;
|
|
|
|
|
country: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface PackageProps {
|
|
|
|
|
packageId: string;
|
|
|
|
|
renderOption: ( option: Rate ) => PackageRateOption;
|
|
|
|
|
collapse: boolean;
|
|
|
|
|
packageData: {
|
|
|
|
|
destination: Destination;
|
|
|
|
|
name: string;
|
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
|
shipping_rates: Rate[];
|
|
|
|
|
items: PackageItem[];
|
|
|
|
|
};
|
|
|
|
|
className?: string;
|
|
|
|
|
collapsible?: boolean;
|
|
|
|
|
noResultsMessage: ReactElement;
|
|
|
|
|
showItems: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
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 ) && (
|
2020-06-05 10:00:19 +00:00
|
|
|
|
<Title
|
2020-06-17 09:53:42 +00:00
|
|
|
|
className="wc-block-components-shipping-rates-control__package-title"
|
2020-06-05 10:00:19 +00:00
|
|
|
|
headingLevel="3"
|
|
|
|
|
>
|
2021-01-28 14:24:01 +00:00
|
|
|
|
{ packageData.name }
|
2020-06-05 10:00:19 +00:00
|
|
|
|
</Title>
|
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;
|