From f0b10d9b0817e593e3b20e9079b0acb2d726f751 Mon Sep 17 00:00:00 2001 From: Alex Florisca Date: Thu, 9 Feb 2023 11:53:25 +0000 Subject: [PATCH] Push address changes for email, name and address fields (#8400) --- assets/js/data/cart/push-changes.ts | 30 +++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/assets/js/data/cart/push-changes.ts b/assets/js/data/cart/push-changes.ts index 4484b978e29..06228265a8e 100644 --- a/assets/js/data/cart/push-changes.ts +++ b/assets/js/data/cart/push-changes.ts @@ -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 ) ) ); };