2021-01-28 14:24:01 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2021-02-04 15:30:28 +00:00
|
|
|
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
|
2021-03-09 10:55:24 +00:00
|
|
|
import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-monetary-amount';
|
|
|
|
import type { Rate, PackageRateOption } from '@woocommerce/type-defs/shipping';
|
2021-04-22 11:37:27 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2021-01-28 14:24:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default render function for package rate options.
|
|
|
|
*
|
2021-03-09 10:55:24 +00:00
|
|
|
* @param {Object} rate Rate data.
|
2021-01-28 14:24:01 +00:00
|
|
|
*/
|
2021-03-09 10:55:24 +00:00
|
|
|
export const renderPackageRateOption = ( rate: Rate ): PackageRateOption => {
|
2021-04-22 11:37:27 +00:00
|
|
|
const priceWithTaxes: number = getSetting(
|
|
|
|
'displayCartPricesIncludingTax',
|
|
|
|
false
|
|
|
|
)
|
2021-03-09 10:55:24 +00:00
|
|
|
? parseInt( rate.price, 10 ) + parseInt( rate.taxes, 10 )
|
|
|
|
: parseInt( rate.price, 10 );
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
return {
|
2021-03-09 10:55:24 +00:00
|
|
|
label: decodeEntities( rate.name ),
|
|
|
|
value: rate.rate_id,
|
2021-01-28 14:24:01 +00:00
|
|
|
description: (
|
|
|
|
<>
|
|
|
|
{ Number.isFinite( priceWithTaxes ) && (
|
|
|
|
<FormattedMonetaryAmount
|
2021-03-09 10:55:24 +00:00
|
|
|
currency={ getCurrencyFromPriceResponse( rate ) }
|
2021-01-28 14:24:01 +00:00
|
|
|
value={ priceWithTaxes }
|
|
|
|
/>
|
|
|
|
) }
|
2021-03-09 10:55:24 +00:00
|
|
|
{ Number.isFinite( priceWithTaxes ) && rate.delivery_time
|
2021-01-28 14:24:01 +00:00
|
|
|
? ' — '
|
|
|
|
: null }
|
2021-03-09 10:55:24 +00:00
|
|
|
{ decodeEntities( rate.delivery_time ) }
|
2021-01-28 14:24:01 +00:00
|
|
|
</>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default renderPackageRateOption;
|