woocommerce/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/shipping-rates-control-package/package-rates.tsx

93 lines
2.5 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import RadioControl, {
RadioControlOptionLayout,
} from '@woocommerce/base-components/radio-control';
import type { CartShippingPackageShippingRate } from '@woocommerce/types';
/**
* Internal dependencies
*/
import { renderPackageRateOption } from './render-package-rate-option';
import type { PackageRateRenderOption } from '../shipping-rates-control-package';
interface PackageRates {
onSelectRate: ( selectedRateId: string ) => void;
rates: CartShippingPackageShippingRate[];
renderOption?: PackageRateRenderOption | undefined;
className?: string;
noResultsMessage: JSX.Element;
selectedRate: CartShippingPackageShippingRate | undefined;
}
const PackageRates = ( {
className = '',
noResultsMessage,
onSelectRate,
rates,
renderOption = renderPackageRateOption,
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 ] );
// Update the selected option if there is no rate selected on mount.
useEffect( () => {
Fix total shipping display info when no shipping method is available (https://github.com/woocommerce/woocommerce-blocks/pull/8819) * Fix total shipping info when no shipping are available * Fix a logical error for displaying shipping info * Fix failing unit tests * Run unit test for the Cart instead of the Checkout The calculator is only available for the Cart Block, so it doesn't make sense to run this test for the Checkout Block * Fix no shipping methods and incomplete address conflict When there are no shipping methods (except for local pickup), we would like to inform the shopper that there are no shipping options available even though the address is complete The solution we found is to check the address on the Cart Block only * Refactor code * Check whether rate is collectible without using hardcoded id * Correctly negate hasCollectibleRate result * Add notice when shipping is selected but no methods are available yet (https://github.com/woocommerce/woocommerce-blocks/pull/9171) * Create useShippingTotalWarning hook * Show notices above checkout sidebar * Call hook to show notice in Checkout block * Remove unused imports * Update hook name to useShowShippingTotalWarning * Move hook to its own file * Import shipping data internally (without alias) * Remove unused imports * Move import to correct place * Return early to avoid if else * Refactor useShowShippingTotalWarning * Get shipping rates directly from the cart instead of the hook * Show shipping cost when price information is available * Check if the passed rates are considered selected * Prevent errors when no rates are available --------- Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com> Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Tarun Vijwani <tarun.vijwani@automattic.com>
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 );
}
}, [ onSelectRate, rates, selectedOption ] );
if ( rates.length === 0 ) {
return noResultsMessage;
}
if ( rates.length > 1 ) {
return (
<RadioControl
className={ className }
onChange={ ( value: string ) => {
setSelectedOption( value );
onSelectRate( value );
} }
selected={ selectedOption }
options={ rates.map( renderOption ) }
/>
);
}
const { label, secondaryLabel, description, secondaryDescription } =
renderOption( rates[ 0 ] );
return (
<RadioControlOptionLayout
label={ label }
secondaryLabel={ secondaryLabel }
description={ description }
secondaryDescription={ secondaryDescription }
/>
);
};
export default PackageRates;