Correctly detect selected location on first mount
This commit is contained in:
parent
5076541597
commit
7196a76577
|
@ -108,7 +108,16 @@ const renderPickupLocation = (
|
||||||
|
|
||||||
const Block = (): JSX.Element | null => {
|
const Block = (): JSX.Element | null => {
|
||||||
const { shippingRates, selectShippingRate } = useShippingData();
|
const { shippingRates, selectShippingRate } = useShippingData();
|
||||||
const [ selectedOption, setSelectedOption ] = useState< string >( '' );
|
|
||||||
|
// Get pickup locations from the first shipping package.
|
||||||
|
const pickupLocations = ( shippingRates[ 0 ]?.shipping_rates || [] ).filter(
|
||||||
|
( { method_id: methodId } ) => methodId === 'pickup_location'
|
||||||
|
);
|
||||||
|
|
||||||
|
const [ selectedOption, setSelectedOption ] = useState< string >(
|
||||||
|
() => pickupLocations.find( ( rate ) => rate.selected )?.rate_id || ''
|
||||||
|
);
|
||||||
|
|
||||||
const onSelectRate = useCallback(
|
const onSelectRate = useCallback(
|
||||||
( rateId: string ) => {
|
( rateId: string ) => {
|
||||||
selectShippingRate( rateId );
|
selectShippingRate( rateId );
|
||||||
|
@ -116,11 +125,6 @@ const Block = (): JSX.Element | null => {
|
||||||
[ selectShippingRate ]
|
[ selectShippingRate ]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Get pickup locations from the first shipping package.
|
|
||||||
const pickupLocations = ( shippingRates[ 0 ]?.shipping_rates || [] ).filter(
|
|
||||||
( { method_id: methodId } ) => methodId === 'pickup_location'
|
|
||||||
);
|
|
||||||
|
|
||||||
// Update the selected option if there is no rate selected on mount.
|
// Update the selected option if there is no rate selected on mount.
|
||||||
useEffect( () => {
|
useEffect( () => {
|
||||||
if ( ! selectedOption && pickupLocations[ 0 ] ) {
|
if ( ! selectedOption && pickupLocations[ 0 ] ) {
|
||||||
|
|
Loading…
Reference in New Issue