2020-02-14 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-02-10 11:59:43 +00:00
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
2020-02-14 03:43:13 +00:00
|
|
|
import RadioControl, {
|
|
|
|
RadioControlOptionLayout,
|
|
|
|
} from '@woocommerce/base-components/radio-control';
|
2021-05-10 09:03:30 +00:00
|
|
|
import type { PackageRateOption } from '@woocommerce/type-defs/shipping';
|
|
|
|
import type { CartShippingPackageShippingRate } from '@woocommerce/type-defs/cart';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { renderPackageRateOption } from './render-package-rate-option';
|
|
|
|
|
2021-03-09 10:55:24 +00:00
|
|
|
interface PackageRates {
|
|
|
|
onSelectRate: ( selectedRateId: string ) => void;
|
2021-05-10 09:03:30 +00:00
|
|
|
rates: CartShippingPackageShippingRate[];
|
|
|
|
renderOption?: (
|
|
|
|
option: CartShippingPackageShippingRate
|
|
|
|
) => PackageRateOption;
|
2021-03-09 10:55:24 +00:00
|
|
|
className?: string;
|
2022-02-10 11:59:43 +00:00
|
|
|
noResultsMessage: JSX.Element;
|
|
|
|
selectedRate: CartShippingPackageShippingRate | undefined;
|
2021-03-09 10:55:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
const PackageRates = ( {
|
2022-02-10 11:59:43 +00:00
|
|
|
className = '',
|
2020-02-14 03:43:13 +00:00
|
|
|
noResultsMessage,
|
2021-01-28 14:24:01 +00:00
|
|
|
onSelectRate,
|
|
|
|
rates,
|
|
|
|
renderOption = renderPackageRateOption,
|
2022-02-10 11:59:43 +00:00
|
|
|
selectedRate,
|
|
|
|
}: PackageRates ): JSX.Element => {
|
|
|
|
const selectedRateId = selectedRate?.rate_id || '';
|
|
|
|
|
|
|
|
// Store selected rate ID in local state so shipping rates changes are shown in the UI instantly.
|
|
|
|
const [ selectedOption, setSelectedOption ] = useState( selectedRateId );
|
|
|
|
|
|
|
|
// Update the selected option if cart state changes in the data stores.
|
|
|
|
useEffect( () => {
|
|
|
|
if ( selectedRateId ) {
|
|
|
|
setSelectedOption( selectedRateId );
|
|
|
|
}
|
|
|
|
}, [ selectedRateId ] );
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
if ( rates.length === 0 ) {
|
2020-11-17 11:58:38 +00:00
|
|
|
return noResultsMessage;
|
2020-02-14 03:43:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
if ( rates.length > 1 ) {
|
2020-02-14 03:43:13 +00:00
|
|
|
return (
|
|
|
|
<RadioControl
|
|
|
|
className={ className }
|
2022-02-10 11:59:43 +00:00
|
|
|
onChange={ ( value: string ) => {
|
|
|
|
setSelectedOption( value );
|
|
|
|
onSelectRate( value );
|
2021-01-28 14:24:01 +00:00
|
|
|
} }
|
2022-02-10 11:59:43 +00:00
|
|
|
selected={ selectedOption }
|
2021-01-28 14:24:01 +00:00
|
|
|
options={ rates.map( renderOption ) }
|
2020-02-14 03:43:13 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
label,
|
|
|
|
secondaryLabel,
|
|
|
|
description,
|
|
|
|
secondaryDescription,
|
2021-01-28 14:24:01 +00:00
|
|
|
} = renderOption( rates[ 0 ] );
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<RadioControlOptionLayout
|
|
|
|
label={ label }
|
|
|
|
secondaryLabel={ secondaryLabel }
|
|
|
|
description={ description }
|
|
|
|
secondaryDescription={ secondaryDescription }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
export default PackageRates;
|