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';
|
2022-12-23 11:59:02 +00:00
|
|
|
import type { CartShippingPackageShippingRate } from '@woocommerce/types';
|
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;
|
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 ] );
|
|
|
|
|
2022-10-18 16:31:06 +00:00
|
|
|
// Update the selected option if there is no rate selected on mount.
|
|
|
|
useEffect( () => {
|
2023-05-25 05:31:15 +00:00
|
|
|
// Check the rates to see if any are marked as selected. At least one should be. If no rate is selected, it could be
|
|
|
|
// that the user toggled quickly from local pickup back to shipping.
|
|
|
|
const isRateSelectedInDataStore = rates.some(
|
|
|
|
( { selected } ) => selected
|
|
|
|
);
|
|
|
|
if (
|
|
|
|
( ! selectedOption && rates[ 0 ] ) ||
|
|
|
|
! isRateSelectedInDataStore
|
|
|
|
) {
|
|
|
|
setSelectedOption( rates[ 0 ]?.rate_id );
|
|
|
|
onSelectRate( rates[ 0 ]?.rate_id );
|
2022-10-18 16:31:06 +00:00
|
|
|
}
|
|
|
|
}, [ onSelectRate, rates, selectedOption ] );
|
|
|
|
|
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
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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;
|