2022-02-22 17:45:01 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { select, dispatch } from '@wordpress/data';
|
2023-04-28 10:29:45 +00:00
|
|
|
import {
|
|
|
|
pluckEmail,
|
|
|
|
removeAllNotices,
|
|
|
|
debounce,
|
|
|
|
} from '@woocommerce/base-utils';
|
2022-02-22 17:45:01 +00:00
|
|
|
import {
|
2023-01-24 14:52:59 +00:00
|
|
|
CartBillingAddress,
|
|
|
|
CartShippingAddress,
|
2022-12-23 11:59:02 +00:00
|
|
|
BillingAddressShippingAddress,
|
|
|
|
} from '@woocommerce/types';
|
2022-02-22 17:45:01 +00:00
|
|
|
import isShallowEqual from '@wordpress/is-shallow-equal';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { STORE_KEY } from './constants';
|
2023-01-24 14:52:59 +00:00
|
|
|
import { VALIDATION_STORE_KEY } from '../validation';
|
2022-12-19 15:30:13 +00:00
|
|
|
import { processErrorResponse } from '../utils';
|
2023-03-13 11:49:28 +00:00
|
|
|
import { shippingAddressHasValidationErrors } from './utils';
|
2022-02-22 17:45:01 +00:00
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
type CustomerData = {
|
|
|
|
billingAddress: CartBillingAddress;
|
|
|
|
shippingAddress: CartShippingAddress;
|
2022-02-22 17:45:01 +00:00
|
|
|
};
|
|
|
|
|
2023-02-09 11:53:25 +00:00
|
|
|
type BillingOrShippingAddress = CartBillingAddress | CartShippingAddress;
|
|
|
|
|
2023-04-28 10:29:45 +00:00
|
|
|
const pick = < Type >( object: Type, keys: string[] ): Type => {
|
|
|
|
return keys.reduce( ( obj, key ) => {
|
|
|
|
if ( object && object.hasOwnProperty( key ) ) {
|
|
|
|
obj[ key as keyof Type ] = object[ key as keyof Type ];
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}, {} as Type );
|
|
|
|
};
|
|
|
|
|
2022-02-22 17:45:01 +00:00
|
|
|
/**
|
|
|
|
* Checks if a cart response contains an email property.
|
|
|
|
*/
|
2023-01-24 14:52:59 +00:00
|
|
|
const isBillingAddress = (
|
2023-02-09 11:53:25 +00:00
|
|
|
address: BillingOrShippingAddress
|
2023-01-24 14:52:59 +00:00
|
|
|
): address is CartBillingAddress => {
|
2022-02-22 17:45:01 +00:00
|
|
|
return 'email' in address;
|
|
|
|
};
|
|
|
|
|
2023-02-15 12:00:16 +00:00
|
|
|
/**
|
|
|
|
* Trims and normalizes address data for comparison.
|
|
|
|
*/
|
|
|
|
export const normalizeAddress = ( address: BillingOrShippingAddress ) => {
|
|
|
|
const trimmedAddress = Object.entries( address ).reduce(
|
|
|
|
( acc, [ key, value ] ) => {
|
2023-04-17 13:57:09 +00:00
|
|
|
//Skip normalizing for any non string field
|
|
|
|
if ( typeof value !== 'string' ) {
|
|
|
|
acc[ key as keyof BillingOrShippingAddress ] = value;
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
2023-02-15 12:00:16 +00:00
|
|
|
if ( key === 'postcode' ) {
|
|
|
|
acc[ key as keyof BillingOrShippingAddress ] = value
|
|
|
|
.replace( ' ', '' )
|
|
|
|
.toUpperCase();
|
2023-04-17 13:57:09 +00:00
|
|
|
return acc;
|
2023-02-15 12:00:16 +00:00
|
|
|
}
|
2023-04-17 13:57:09 +00:00
|
|
|
|
|
|
|
acc[ key as keyof BillingOrShippingAddress ] = value.trim();
|
2023-02-15 12:00:16 +00:00
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{} as BillingOrShippingAddress
|
|
|
|
);
|
2023-02-09 11:53:25 +00:00
|
|
|
return trimmedAddress;
|
|
|
|
};
|
|
|
|
|
2022-02-22 17:45:01 +00:00
|
|
|
/**
|
2023-02-15 12:00:16 +00:00
|
|
|
* Does a shallow compare of all address data to determine if the cart needs updating on the server.
|
2022-02-22 17:45:01 +00:00
|
|
|
*/
|
2023-01-24 14:52:59 +00:00
|
|
|
const isAddressDirty = < T extends CartBillingAddress | CartShippingAddress >(
|
2022-02-22 17:45:01 +00:00
|
|
|
// An object containing all previous address information
|
|
|
|
previousAddress: T,
|
|
|
|
// An object containing all address information.
|
|
|
|
address: T
|
|
|
|
): boolean => {
|
|
|
|
if (
|
2023-01-24 14:52:59 +00:00
|
|
|
isBillingAddress( address ) &&
|
2022-02-22 17:45:01 +00:00
|
|
|
pluckEmail( address ) !==
|
2023-01-24 14:52:59 +00:00
|
|
|
pluckEmail( previousAddress as CartBillingAddress )
|
2022-02-22 17:45:01 +00:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-15 12:00:16 +00:00
|
|
|
const addressMatches = isShallowEqual(
|
|
|
|
normalizeAddress( previousAddress ),
|
|
|
|
normalizeAddress( address )
|
2022-02-22 17:45:01 +00:00
|
|
|
);
|
2023-02-15 12:00:16 +00:00
|
|
|
|
|
|
|
return ! addressMatches;
|
2022-02-22 17:45:01 +00:00
|
|
|
};
|
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
type BaseAddressKey = keyof CartBillingAddress | keyof CartShippingAddress;
|
|
|
|
|
|
|
|
const getDirtyKeys = < T extends CartBillingAddress | CartShippingAddress >(
|
|
|
|
// An object containing all previous address information
|
|
|
|
previousAddress: T,
|
|
|
|
// An object containing all address information.
|
|
|
|
address: T
|
|
|
|
): BaseAddressKey[] => {
|
|
|
|
const previousAddressKeys = Object.keys(
|
|
|
|
previousAddress
|
|
|
|
) as BaseAddressKey[];
|
|
|
|
|
|
|
|
return previousAddressKeys.filter( ( key ) => {
|
|
|
|
return previousAddress[ key ] !== address[ key ];
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2022-02-22 17:45:01 +00:00
|
|
|
/**
|
|
|
|
* Local cache of customerData used for comparisons.
|
|
|
|
*/
|
|
|
|
let customerData = <CustomerData>{
|
2022-06-10 16:59:25 +00:00
|
|
|
billingAddress: {},
|
2022-02-22 17:45:01 +00:00
|
|
|
shippingAddress: {},
|
|
|
|
};
|
2023-01-24 14:52:59 +00:00
|
|
|
|
2022-02-22 17:45:01 +00:00
|
|
|
// Tracks if customerData has been populated.
|
|
|
|
let customerDataIsInitialized = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks which props have changed so the correct data gets pushed to the server.
|
|
|
|
*/
|
2023-01-24 14:52:59 +00:00
|
|
|
const dirtyProps = <
|
|
|
|
{
|
|
|
|
billingAddress: BaseAddressKey[];
|
|
|
|
shippingAddress: BaseAddressKey[];
|
|
|
|
}
|
|
|
|
>{
|
|
|
|
billingAddress: [],
|
|
|
|
shippingAddress: [],
|
2022-02-22 17:45:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to dispatch an update to the server. This is debounced.
|
|
|
|
*/
|
|
|
|
const updateCustomerData = debounce( (): void => {
|
2022-06-10 16:59:25 +00:00
|
|
|
const { billingAddress, shippingAddress } = customerData;
|
2023-01-24 14:52:59 +00:00
|
|
|
const validationStore = select( VALIDATION_STORE_KEY );
|
2022-02-22 17:45:01 +00:00
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
// Before we push anything, we need to ensure that the data we're pushing (dirty fields) are valid, otherwise we will
|
|
|
|
// abort and wait for the validation issues to be resolved.
|
|
|
|
const invalidProps = [
|
|
|
|
...dirtyProps.billingAddress.filter( ( key ) => {
|
|
|
|
return (
|
|
|
|
validationStore.getValidationError( 'billing_' + key ) !==
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
} ),
|
|
|
|
...dirtyProps.shippingAddress.filter( ( key ) => {
|
|
|
|
return (
|
|
|
|
validationStore.getValidationError( 'shipping_' + key ) !==
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
} ),
|
|
|
|
].filter( Boolean );
|
|
|
|
|
|
|
|
if ( invalidProps.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find valid data from the list of dirtyProps and prepare to push to the server.
|
2023-03-06 15:05:38 +00:00
|
|
|
const customerDataToUpdate = {} as Partial< BillingAddressShippingAddress >;
|
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
if ( dirtyProps.billingAddress.length ) {
|
|
|
|
customerDataToUpdate.billing_address = pick(
|
|
|
|
billingAddress,
|
|
|
|
dirtyProps.billingAddress
|
|
|
|
);
|
|
|
|
dirtyProps.billingAddress = [];
|
2022-02-22 17:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
if ( dirtyProps.shippingAddress.length ) {
|
|
|
|
customerDataToUpdate.shipping_address = pick(
|
|
|
|
shippingAddress,
|
|
|
|
dirtyProps.shippingAddress
|
|
|
|
);
|
|
|
|
dirtyProps.shippingAddress = [];
|
2022-02-22 17:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 15:05:38 +00:00
|
|
|
// If there is customer data to update, push it to the server.
|
2022-02-22 17:45:01 +00:00
|
|
|
if ( Object.keys( customerDataToUpdate ).length ) {
|
|
|
|
dispatch( STORE_KEY )
|
|
|
|
.updateCustomerData( customerDataToUpdate )
|
2023-03-06 15:05:38 +00:00
|
|
|
.then( removeAllNotices )
|
2022-02-22 17:45:01 +00:00
|
|
|
.catch( ( response ) => {
|
2022-12-19 15:30:13 +00:00
|
|
|
processErrorResponse( response );
|
2023-03-06 15:05:38 +00:00
|
|
|
|
|
|
|
// Data did not persist due to an error. Make the props dirty again so they get pushed to the server.
|
|
|
|
if ( customerDataToUpdate.billing_address ) {
|
|
|
|
dirtyProps.billingAddress = [
|
|
|
|
...dirtyProps.billingAddress,
|
|
|
|
...( Object.keys(
|
|
|
|
customerDataToUpdate.billing_address
|
|
|
|
) as BaseAddressKey[] ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ( customerDataToUpdate.shipping_address ) {
|
|
|
|
dirtyProps.shippingAddress = [
|
|
|
|
...dirtyProps.shippingAddress,
|
|
|
|
...( Object.keys(
|
|
|
|
customerDataToUpdate.shipping_address
|
|
|
|
) as BaseAddressKey[] ),
|
|
|
|
];
|
|
|
|
}
|
2023-03-13 11:49:28 +00:00
|
|
|
} )
|
|
|
|
.finally( () => {
|
|
|
|
if ( ! shippingAddressHasValidationErrors() ) {
|
|
|
|
dispatch( STORE_KEY ).setFullShippingAddressPushed( true );
|
|
|
|
}
|
2022-02-22 17:45:01 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}, 1000 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* After cart has fully initialized, pushes changes to the server when data in the store is changed. Updates to the
|
|
|
|
* server are debounced to prevent excessive requests.
|
|
|
|
*/
|
|
|
|
export const pushChanges = (): void => {
|
|
|
|
const store = select( STORE_KEY );
|
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
if ( ! store.hasFinishedResolution( 'getCartData' ) ) {
|
2022-02-22 17:45:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
// Returns all current customer data from the store.
|
2022-02-22 17:45:01 +00:00
|
|
|
const newCustomerData = store.getCustomerData();
|
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
// On first run, this will populate the customerData cache with the current customer data in the store.
|
|
|
|
// This does not need to be pushed to the server because it's already there.
|
2022-02-22 17:45:01 +00:00
|
|
|
if ( ! customerDataIsInitialized ) {
|
|
|
|
customerData = newCustomerData;
|
|
|
|
customerDataIsInitialized = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
// Check if the billing and shipping addresses are "dirty"--as in, they've changed since the last push.
|
|
|
|
const billingIsDirty = isAddressDirty(
|
|
|
|
customerData.billingAddress,
|
|
|
|
newCustomerData.billingAddress
|
|
|
|
);
|
|
|
|
const shippingIsDirty = isAddressDirty(
|
|
|
|
customerData.shippingAddress,
|
|
|
|
newCustomerData.shippingAddress
|
|
|
|
);
|
2022-02-22 17:45:01 +00:00
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
// Update local cache of dirty prop keys.
|
|
|
|
if ( billingIsDirty ) {
|
|
|
|
dirtyProps.billingAddress = [
|
|
|
|
...dirtyProps.billingAddress,
|
|
|
|
...getDirtyKeys(
|
|
|
|
customerData.billingAddress,
|
|
|
|
newCustomerData.billingAddress
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ( shippingIsDirty ) {
|
|
|
|
dirtyProps.shippingAddress = [
|
|
|
|
...dirtyProps.shippingAddress,
|
|
|
|
...getDirtyKeys(
|
|
|
|
customerData.shippingAddress,
|
|
|
|
newCustomerData.shippingAddress
|
|
|
|
),
|
|
|
|
];
|
2022-02-22 17:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
// Update local cache of customer data so the next time this runs, it can compare against the latest data.
|
2022-02-22 17:45:01 +00:00
|
|
|
customerData = newCustomerData;
|
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
// Trigger the update if we have any dirty props.
|
|
|
|
if (
|
|
|
|
dirtyProps.billingAddress.length ||
|
|
|
|
dirtyProps.shippingAddress.length
|
|
|
|
) {
|
2022-02-22 17:45:01 +00:00
|
|
|
updateCustomerData();
|
|
|
|
}
|
|
|
|
};
|
2023-02-15 12:00:16 +00:00
|
|
|
|
|
|
|
export const flushChanges = (): void => {
|
|
|
|
updateCustomerData.flush();
|
|
|
|
};
|