2022-10-04 12:02:28 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { useShippingData } from '@woocommerce/base-context/hooks';
|
|
|
|
import {
|
|
|
|
__experimentalRadio as Radio,
|
|
|
|
__experimentalRadioGroup as RadioGroup,
|
|
|
|
} from 'wordpress-components';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { Icon, store, shipping } from '@wordpress/icons';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2022-10-11 15:46:59 +00:00
|
|
|
import { RatePrice, getLocalPickupPrices, getShippingPrices } from './shared';
|
|
|
|
import type { minMaxPrices } from './shared';
|
2022-10-04 12:02:28 +00:00
|
|
|
|
|
|
|
const LocalPickupSelector = ( {
|
|
|
|
checked,
|
|
|
|
rate,
|
|
|
|
showPrice,
|
|
|
|
showIcon,
|
2022-10-05 10:54:48 +00:00
|
|
|
toggleText,
|
2022-11-18 13:20:04 +00:00
|
|
|
multiple,
|
2022-10-04 12:02:28 +00:00
|
|
|
}: {
|
|
|
|
checked: string;
|
2022-10-11 15:46:59 +00:00
|
|
|
rate: minMaxPrices;
|
2022-10-04 12:02:28 +00:00
|
|
|
showPrice: boolean;
|
|
|
|
showIcon: boolean;
|
2022-10-05 10:54:48 +00:00
|
|
|
toggleText: string;
|
2022-11-18 13:20:04 +00:00
|
|
|
multiple: boolean;
|
2022-10-04 12:02:28 +00:00
|
|
|
} ) => {
|
|
|
|
return (
|
|
|
|
<Radio
|
|
|
|
value="pickup"
|
2022-10-20 10:48:46 +00:00
|
|
|
className={ classnames(
|
|
|
|
'wc-block-checkout__shipping-method-option',
|
|
|
|
{
|
|
|
|
'wc-block-checkout__shipping-method-option--selected':
|
|
|
|
checked === 'pickup',
|
|
|
|
}
|
|
|
|
) }
|
2022-10-04 12:02:28 +00:00
|
|
|
>
|
|
|
|
{ showIcon === true && (
|
|
|
|
<Icon
|
|
|
|
icon={ store }
|
|
|
|
size={ 28 }
|
2022-10-20 10:48:46 +00:00
|
|
|
className="wc-block-checkout__shipping-method-option-icon"
|
2022-10-04 12:02:28 +00:00
|
|
|
/>
|
|
|
|
) }
|
2022-10-20 10:48:46 +00:00
|
|
|
<span className="wc-block-checkout__shipping-method-option-title">
|
2022-10-05 10:54:48 +00:00
|
|
|
{ toggleText }
|
2022-10-04 12:02:28 +00:00
|
|
|
</span>
|
2022-10-11 15:46:59 +00:00
|
|
|
{ showPrice === true && (
|
2022-11-18 13:20:04 +00:00
|
|
|
<RatePrice
|
|
|
|
multiple={ multiple }
|
|
|
|
minRate={ rate.min }
|
|
|
|
maxRate={ rate.max }
|
|
|
|
/>
|
2022-10-11 15:46:59 +00:00
|
|
|
) }
|
2022-10-04 12:02:28 +00:00
|
|
|
</Radio>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const ShippingSelector = ( {
|
|
|
|
checked,
|
|
|
|
rate,
|
|
|
|
showPrice,
|
|
|
|
showIcon,
|
2022-10-05 10:54:48 +00:00
|
|
|
toggleText,
|
2022-10-04 12:02:28 +00:00
|
|
|
}: {
|
|
|
|
checked: string;
|
2022-10-11 15:46:59 +00:00
|
|
|
rate: minMaxPrices;
|
2022-10-04 12:02:28 +00:00
|
|
|
showPrice: boolean;
|
|
|
|
showIcon: boolean;
|
2022-10-05 10:54:48 +00:00
|
|
|
toggleText: string;
|
2022-10-04 12:02:28 +00:00
|
|
|
} ) => {
|
|
|
|
const Price =
|
2022-10-11 15:46:59 +00:00
|
|
|
rate.min === undefined ? (
|
2022-10-20 10:48:46 +00:00
|
|
|
<span className="wc-block-checkout__shipping-method-option-price">
|
2022-10-04 12:02:28 +00:00
|
|
|
{ __(
|
|
|
|
'calculated with an address',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</span>
|
|
|
|
) : (
|
2022-10-11 15:46:59 +00:00
|
|
|
<RatePrice minRate={ rate.min } maxRate={ rate.max } />
|
2022-10-04 12:02:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Radio
|
|
|
|
value="shipping"
|
2022-10-20 10:48:46 +00:00
|
|
|
className={ classnames(
|
|
|
|
'wc-block-checkout__shipping-method-option',
|
|
|
|
{
|
|
|
|
'wc-block-checkout__shipping-method-option--selected':
|
|
|
|
checked === 'shipping',
|
|
|
|
}
|
|
|
|
) }
|
2022-10-04 12:02:28 +00:00
|
|
|
>
|
|
|
|
{ showIcon === true && (
|
|
|
|
<Icon
|
|
|
|
icon={ shipping }
|
|
|
|
size={ 28 }
|
2022-10-20 10:48:46 +00:00
|
|
|
className="wc-block-checkout__shipping-method-option-icon"
|
2022-10-04 12:02:28 +00:00
|
|
|
/>
|
|
|
|
) }
|
2022-10-20 10:48:46 +00:00
|
|
|
<span className="wc-block-checkout__shipping-method-option-title">
|
2022-10-05 10:54:48 +00:00
|
|
|
{ toggleText }
|
2022-10-04 12:02:28 +00:00
|
|
|
</span>
|
|
|
|
{ showPrice === true && Price }
|
|
|
|
</Radio>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const Block = ( {
|
|
|
|
checked,
|
|
|
|
onChange,
|
|
|
|
showPrice,
|
|
|
|
showIcon,
|
2022-10-05 10:54:48 +00:00
|
|
|
localPickupText,
|
|
|
|
shippingText,
|
2022-10-04 12:02:28 +00:00
|
|
|
}: {
|
|
|
|
checked: string;
|
|
|
|
onChange: ( value: string ) => void;
|
|
|
|
showPrice: boolean;
|
|
|
|
showIcon: boolean;
|
2022-10-05 10:54:48 +00:00
|
|
|
localPickupText: string;
|
|
|
|
shippingText: string;
|
2022-10-04 12:02:28 +00:00
|
|
|
} ): JSX.Element | null => {
|
2022-10-18 16:31:06 +00:00
|
|
|
const { shippingRates } = useShippingData();
|
2022-10-04 12:02:28 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<RadioGroup
|
2022-10-20 10:48:46 +00:00
|
|
|
id="shipping-method"
|
|
|
|
className="wc-block-checkout__shipping-method-container"
|
2022-10-04 12:02:28 +00:00
|
|
|
label="options"
|
|
|
|
onChange={ onChange }
|
|
|
|
checked={ checked }
|
|
|
|
>
|
|
|
|
<ShippingSelector
|
|
|
|
checked={ checked }
|
2022-10-11 15:46:59 +00:00
|
|
|
rate={ getShippingPrices( shippingRates[ 0 ]?.shipping_rates ) }
|
2022-10-04 12:02:28 +00:00
|
|
|
showPrice={ showPrice }
|
|
|
|
showIcon={ showIcon }
|
2022-10-05 10:54:48 +00:00
|
|
|
toggleText={ shippingText }
|
2022-10-04 12:02:28 +00:00
|
|
|
/>
|
|
|
|
<LocalPickupSelector
|
|
|
|
checked={ checked }
|
2022-10-11 15:46:59 +00:00
|
|
|
rate={ getLocalPickupPrices(
|
|
|
|
shippingRates[ 0 ]?.shipping_rates
|
|
|
|
) }
|
2022-11-18 13:20:04 +00:00
|
|
|
multiple={ shippingRates.length > 1 }
|
2022-10-04 12:02:28 +00:00
|
|
|
showPrice={ showPrice }
|
|
|
|
showIcon={ showIcon }
|
2022-10-05 10:54:48 +00:00
|
|
|
toggleText={ localPickupText }
|
2022-10-04 12:02:28 +00:00
|
|
|
/>
|
|
|
|
</RadioGroup>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Block;
|