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

Commit

Permalink
Ensure addresses sync when loading the checkout shipping address block
Browse files Browse the repository at this point in the history
  • Loading branch information
opr committed Jul 27, 2022
1 parent 114eae8 commit ed0fcc8
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useMemo, useEffect, Fragment } from '@wordpress/element';
import { useMemo, useEffect, Fragment, useState } from '@wordpress/element';
import { AddressForm } from '@woocommerce/base-components/cart-checkout';
import {
useCheckoutAddress,
Expand Down Expand Up @@ -48,13 +48,29 @@ const Block = ( {
const { dispatchCheckoutEvent } = useStoreEvents();
const { isEditor } = useEditorContext();

// This is used to track whether the "Use shipping as billing" checkbox was checked on first load and if we synced
// the shipping address to the billing address if it was. This is not used on further toggles of the checkbox.
const [ addressesSynced, setAddressesSynced ] = useState( false );

// Clears data if fields are hidden.
useEffect( () => {
if ( ! showPhoneField ) {
setShippingPhone( '' );
}
}, [ showPhoneField, setShippingPhone ] );

// Run this on first render to ensure addresses sync if needed, there is no need to re-run this when toggling the
// checkbox.
useEffect( () => {
if ( addressesSynced ) {
return;
}
if ( useShippingAsBilling ) {
setBillingAddress( shippingAddress );
}
setAddressesSynced( true );
}, [ setBillingAddress, shippingAddress, useShippingAsBilling ] );

const addressFieldsConfig = useMemo( () => {
return {
company: {
Expand Down

0 comments on commit ed0fcc8

Please sign in to comment.