2020-02-14 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-02-10 11:59:43 +00:00
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
2024-04-16 12:39:55 +00:00
|
|
|
import { RadioControl } 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';
|
2024-04-10 09:54:05 +00:00
|
|
|
import type { PackageRateRenderOption } from '../shipping-rates-control-package/types';
|
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;
|
2024-04-10 09:54:05 +00:00
|
|
|
// Should the selected rate be highlighted.
|
|
|
|
highlightChecked?: 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,
|
2024-04-10 09:54:05 +00:00
|
|
|
highlightChecked = 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.
|
2024-05-13 15:29:04 +00:00
|
|
|
if ( rates.length > 0 ) {
|
|
|
|
return rates[ 0 ].rate_id;
|
|
|
|
}
|
2023-08-08 10:56:19 +00:00
|
|
|
} );
|
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 ] );
|
|
|
|
|
2024-05-13 15:29:04 +00:00
|
|
|
// Update the selected option if there is no rate selected on mount.
|
|
|
|
useEffect( () => {
|
|
|
|
// The useState callback run only once, so we need this to update it right fetching new rates.
|
|
|
|
if ( ! selectedOption && rates.length > 0 ) {
|
|
|
|
setSelectedOption( rates[ 0 ].rate_id );
|
|
|
|
onSelectRate( rates[ 0 ].rate_id );
|
|
|
|
}
|
|
|
|
}, [ onSelectRate, rates, selectedOption ] );
|
|
|
|
|
2023-08-08 10:56:19 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-04-16 12:39:55 +00:00
|
|
|
<RadioControl
|
|
|
|
className={ className }
|
|
|
|
onChange={ ( value: string ) => {
|
|
|
|
setSelectedOption( value );
|
|
|
|
onSelectRate( value );
|
|
|
|
} }
|
|
|
|
highlightChecked={ highlightChecked }
|
|
|
|
disabled={ disabled }
|
|
|
|
selected={ selectedOption }
|
|
|
|
options={ rates.map( renderOption ) }
|
2020-02-14 03:43:13 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
export default PackageRates;
|