2020-02-14 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-02-10 11:59:43 +00:00
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
2023-10-23 14:59:19 +00:00
|
|
|
import {
|
|
|
|
RadioControl,
|
2020-02-14 03:43:13 +00:00
|
|
|
RadioControlOptionLayout,
|
2023-10-23 14:59:19 +00:00
|
|
|
} from '@woocommerce/blocks-components';
|
2022-12-23 11:59:02 +00:00
|
|
|
import type { CartShippingPackageShippingRate } from '@woocommerce/types';
|
2023-08-08 10:56:19 +00:00
|
|
|
import { usePrevious } from '@woocommerce/base-hooks';
|
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';
|
2022-12-15 14:45:17 +00:00
|
|
|
import type { PackageRateRenderOption } from '../shipping-rates-control-package';
|
2021-01-28 14:24:01 +00:00
|
|
|
|
2021-03-09 10:55:24 +00:00
|
|
|
interface PackageRates {
|
|
|
|
onSelectRate: ( selectedRateId: string ) => void;
|
2021-05-10 09:03:30 +00:00
|
|
|
rates: CartShippingPackageShippingRate[];
|
2022-12-15 14:45:17 +00:00
|
|
|
renderOption?: PackageRateRenderOption | undefined;
|
2021-03-09 10:55:24 +00:00
|
|
|
className?: string;
|
2022-02-10 11:59:43 +00:00
|
|
|
noResultsMessage: JSX.Element;
|
|
|
|
selectedRate: CartShippingPackageShippingRate | undefined;
|
2023-08-08 10:56:19 +00:00
|
|
|
disabled?: boolean;
|
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,
|
2023-08-08 10:56:19 +00:00
|
|
|
disabled = false,
|
2022-02-10 11:59:43 +00:00
|
|
|
}: PackageRates ): JSX.Element => {
|
|
|
|
const selectedRateId = selectedRate?.rate_id || '';
|
2023-08-08 10:56:19 +00:00
|
|
|
const previousSelectedRateId = usePrevious( selectedRateId );
|
2022-02-10 11:59:43 +00:00
|
|
|
|
|
|
|
// Store selected rate ID in local state so shipping rates changes are shown in the UI instantly.
|
2023-08-08 10:56:19 +00:00
|
|
|
const [ selectedOption, setSelectedOption ] = useState( () => {
|
2022-02-10 11:59:43 +00:00
|
|
|
if ( selectedRateId ) {
|
2023-08-08 10:56:19 +00:00
|
|
|
return selectedRateId;
|
2022-02-10 11:59:43 +00:00
|
|
|
}
|
2023-08-08 10:56:19 +00:00
|
|
|
// Default to first rate if no rate is selected.
|
|
|
|
return rates[ 0 ]?.rate_id;
|
|
|
|
} );
|
2022-02-10 11:59:43 +00:00
|
|
|
|
2023-08-08 10:56:19 +00:00
|
|
|
// Update the selected option if cart state changes in the data store.
|
2022-10-18 16:31:06 +00:00
|
|
|
useEffect( () => {
|
2023-05-25 05:31:15 +00:00
|
|
|
if (
|
2023-08-08 10:56:19 +00:00
|
|
|
selectedRateId &&
|
|
|
|
selectedRateId !== previousSelectedRateId &&
|
|
|
|
selectedRateId !== selectedOption
|
2023-05-25 05:31:15 +00:00
|
|
|
) {
|
2023-08-08 10:56:19 +00:00
|
|
|
setSelectedOption( selectedRateId );
|
|
|
|
}
|
|
|
|
}, [ selectedRateId, selectedOption, previousSelectedRateId ] );
|
|
|
|
|
|
|
|
// Update the data store when the local selected rate changes.
|
|
|
|
useEffect( () => {
|
|
|
|
if ( selectedOption ) {
|
|
|
|
onSelectRate( selectedOption );
|
2022-10-18 16:31:06 +00:00
|
|
|
}
|
2023-08-08 10:56:19 +00:00
|
|
|
}, [ onSelectRate, selectedOption ] );
|
2022-10-18 16:31:06 +00:00
|
|
|
|
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
|
|
|
} }
|
2023-08-08 10:56:19 +00:00
|
|
|
disabled={ disabled }
|
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
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const { label, secondaryLabel, description, secondaryDescription } =
|
|
|
|
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;
|