Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Correctly detect selected location on first mount
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Jan 12, 2023
1 parent f0d4f80 commit a5ce9ef
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,23 @@ const renderPickupLocation = (

const Block = (): JSX.Element | null => {
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(
( rateId: string ) => {
selectShippingRate( rateId );
},
[ 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.
useEffect( () => {
if ( ! selectedOption && pickupLocations[ 0 ] ) {
Expand Down

0 comments on commit a5ce9ef

Please sign in to comment.