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

Commit

Permalink
Push address changes for email, name and address fields (#8400)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexflorisca committed Feb 10, 2023
1 parent f7d784e commit f0b10d9
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions assets/js/data/cart/push-changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -57,8 +71,8 @@ const isAddressDirty = < T extends CartBillingAddress | CartShippingAddress >(
return (
!! address.country &&
! isShallowEqual(
pluckAddress( previousAddress ),
pluckAddress( address )
trimAddress( previousAddress ),
trimAddress( address )
)
);
};
Expand Down

0 comments on commit f0b10d9

Please sign in to comment.