woocommerce/plugins/woocommerce-blocks/assets/js/blocks/checkout/inner-blocks/checkout-shipping-method-block/block.tsx

164 lines
3.4 KiB
TypeScript
Raw Normal View History

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';
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,
toggleText,
multiple,
2022-10-04 12:02:28 +00:00
}: {
checked: string;
rate: minMaxPrices;
2022-10-04 12:02:28 +00:00
showPrice: boolean;
showIcon: boolean;
toggleText: string;
multiple: boolean;
2022-10-04 12:02:28 +00:00
} ) => {
return (
<Radio
value="pickup"
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 }
className="wc-block-checkout__shipping-method-option-icon"
2022-10-04 12:02:28 +00:00
/>
) }
<span className="wc-block-checkout__shipping-method-option-title">
{ toggleText }
2022-10-04 12:02:28 +00:00
</span>
{ showPrice === true && (
<RatePrice
multiple={ multiple }
minRate={ rate.min }
maxRate={ rate.max }
/>
) }
2022-10-04 12:02:28 +00:00
</Radio>
);
};
const ShippingSelector = ( {
checked,
rate,
showPrice,
showIcon,
toggleText,
2022-10-04 12:02:28 +00:00
}: {
checked: string;
rate: minMaxPrices;
2022-10-04 12:02:28 +00:00
showPrice: boolean;
showIcon: boolean;
toggleText: string;
2022-10-04 12:02:28 +00:00
} ) => {
const Price =
rate.min === undefined ? (
<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>
) : (
<RatePrice minRate={ rate.min } maxRate={ rate.max } />
2022-10-04 12:02:28 +00:00
);
return (
<Radio
value="shipping"
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 }
className="wc-block-checkout__shipping-method-option-icon"
2022-10-04 12:02:28 +00:00
/>
) }
<span className="wc-block-checkout__shipping-method-option-title">
{ toggleText }
2022-10-04 12:02:28 +00:00
</span>
{ showPrice === true && Price }
</Radio>
);
};
const Block = ( {
checked,
onChange,
showPrice,
showIcon,
localPickupText,
shippingText,
2022-10-04 12:02:28 +00:00
}: {
checked: string;
onChange: ( value: string ) => void;
showPrice: boolean;
showIcon: boolean;
localPickupText: string;
shippingText: string;
2022-10-04 12:02:28 +00:00
} ): JSX.Element | null => {
const { shippingRates } = useShippingData();
2022-10-04 12:02:28 +00:00
return (
<RadioGroup
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 }
rate={ getShippingPrices( shippingRates[ 0 ]?.shipping_rates ) }
2022-10-04 12:02:28 +00:00
showPrice={ showPrice }
showIcon={ showIcon }
toggleText={ shippingText }
2022-10-04 12:02:28 +00:00
/>
<LocalPickupSelector
checked={ checked }
rate={ getLocalPickupPrices(
shippingRates[ 0 ]?.shipping_rates
) }
multiple={ shippingRates.length > 1 }
2022-10-04 12:02:28 +00:00
showPrice={ showPrice }
showIcon={ showIcon }
toggleText={ localPickupText }
2022-10-04 12:02:28 +00:00
/>
</RadioGroup>
);
};
export default Block;