Push address changes for email, name and address fields (https://github.com/woocommerce/woocommerce-blocks/pull/8400)

This commit is contained in:
Alex Florisca 2023-02-09 11:53:25 +00:00 committed by GitHub
parent aeed7b22b0
commit 495339a073
1 changed files with 22 additions and 8 deletions

View File

@ -3,11 +3,7 @@
*/
import { debounce, pick } from 'lodash';
import { select, dispatch } from '@wordpress/data';
import {
pluckAddress,
pluckEmail,
removeAllNotices,
} from '@woocommerce/base-utils';
import { pluckEmail, removeAllNotices } from '@woocommerce/base-utils';
import {
CartBillingAddress,
CartShippingAddress,
@ -27,15 +23,33 @@ type CustomerData = {
shippingAddress: CartShippingAddress;
};
type BillingOrShippingAddress = CartBillingAddress | CartShippingAddress;
/**
* Checks if a cart response contains an email property.
*/
const isBillingAddress = (
address: CartBillingAddress | CartShippingAddress
address: BillingOrShippingAddress
): address is CartBillingAddress => {
return 'email' in address;
};
export const trimAddress = ( address: BillingOrShippingAddress ) => {
const trimmedAddress = {
...address,
};
Object.keys( address ).forEach( ( key ) => {
trimmedAddress[ key as keyof BillingOrShippingAddress ] =
address[ key as keyof BillingOrShippingAddress ].trim();
} );
trimmedAddress.postcode = trimmedAddress.postcode
? trimmedAddress.postcode.replace( ' ', '' ).toUpperCase()
: '';
return trimmedAddress;
};
/**
* Does a shallow compare of important address data to determine if the cart needs updating on the server. This takes
* the current and previous address into account, as well as the billing email field.
@ -57,8 +71,8 @@ const isAddressDirty = < T extends CartBillingAddress | CartShippingAddress >(
return (
!! address.country &&
! isShallowEqual(
pluckAddress( previousAddress ),
pluckAddress( address )
trimAddress( previousAddress ),
trimAddress( address )
)
);
};