2022-02-22 17:45:01 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-08-09 17:24:51 +00:00
|
|
|
import { removeAllNotices, debounce, pick } 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';
|
2023-08-09 17:24:51 +00:00
|
|
|
import { select, dispatch } from '@wordpress/data';
|
2022-02-22 17:45:01 +00:00
|
|
|
import isShallowEqual from '@wordpress/is-shallow-equal';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { STORE_KEY } from './constants';
|
2022-12-19 15:30:13 +00:00
|
|
|
import { processErrorResponse } from '../utils';
|
2023-08-09 17:24:51 +00:00
|
|
|
import { getDirtyKeys, validateDirtyProps, BaseAddressKey } from './utils';
|
|
|
|
|
|
|
|
// This is used to track and cache the local state of push changes.
|
|
|
|
const localState = {
|
|
|
|
// True when the customer data has been initialized.
|
|
|
|
customerDataIsInitialized: false,
|
|
|
|
// True when a push is currently happening to avoid simultaneous pushes.
|
|
|
|
doingPush: false,
|
|
|
|
// Local cache of the last pushed customerData used for comparisons.
|
|
|
|
customerData: {
|
|
|
|
billingAddress: {} as CartBillingAddress,
|
|
|
|
shippingAddress: {} as CartShippingAddress,
|
|
|
|
},
|
|
|
|
// Tracks which props have changed so the correct data gets pushed to the server.
|
|
|
|
dirtyProps: {
|
|
|
|
billingAddress: [] as BaseAddressKey[],
|
|
|
|
shippingAddress: [] as BaseAddressKey[],
|
|
|
|
},
|
2023-04-28 10:29:45 +00:00
|
|
|
};
|
|
|
|
|
2022-02-22 17:45:01 +00:00
|
|
|
/**
|
2023-08-09 17:24:51 +00:00
|
|
|
* Initializes the customer data cache on the first run.
|
2022-02-22 17:45:01 +00:00
|
|
|
*/
|
2023-08-09 17:24:51 +00:00
|
|
|
const initialize = () => {
|
|
|
|
localState.customerData = select( STORE_KEY ).getCustomerData();
|
|
|
|
localState.customerDataIsInitialized = true;
|
2022-02-22 17:45:01 +00:00
|
|
|
};
|
|
|
|
|
2023-02-15 12:00:16 +00:00
|
|
|
/**
|
2023-08-09 17:24:51 +00:00
|
|
|
* Checks customer data against new customer data to get a list of dirty props.
|
2023-02-15 12:00:16 +00:00
|
|
|
*/
|
2023-08-09 17:24:51 +00:00
|
|
|
const updateDirtyProps = () => {
|
|
|
|
// Returns all current customer data from the store.
|
|
|
|
const newCustomerData = select( STORE_KEY ).getCustomerData();
|
|
|
|
|
|
|
|
localState.dirtyProps.billingAddress = [
|
|
|
|
...localState.dirtyProps.billingAddress,
|
|
|
|
...getDirtyKeys(
|
|
|
|
localState.customerData.billingAddress,
|
|
|
|
newCustomerData.billingAddress
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
localState.dirtyProps.shippingAddress = [
|
|
|
|
...localState.dirtyProps.shippingAddress,
|
|
|
|
...getDirtyKeys(
|
|
|
|
localState.customerData.shippingAddress,
|
|
|
|
newCustomerData.shippingAddress
|
|
|
|
),
|
|
|
|
];
|
2023-04-17 13:57:09 +00:00
|
|
|
|
2023-08-09 17:24:51 +00:00
|
|
|
// Update local cache of customer data so the next time this runs, it can compare against the latest data.
|
|
|
|
localState.customerData = newCustomerData;
|
2023-02-09 11:53:25 +00:00
|
|
|
};
|
|
|
|
|
2022-02-22 17:45:01 +00:00
|
|
|
/**
|
2023-08-09 17:24:51 +00:00
|
|
|
* Function to dispatch an update to the server.
|
2022-02-22 17:45:01 +00:00
|
|
|
*/
|
2023-08-09 17:24:51 +00:00
|
|
|
const updateCustomerData = (): void => {
|
|
|
|
if ( localState.doingPush ) {
|
|
|
|
return;
|
2022-02-22 17:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 17:24:51 +00:00
|
|
|
// Prevent multiple pushes from happening at the same time.
|
|
|
|
localState.doingPush = true;
|
2023-01-24 14:52:59 +00:00
|
|
|
|
2023-08-09 17:24:51 +00:00
|
|
|
// Get updated list of dirty props by comparing customer data.
|
|
|
|
updateDirtyProps();
|
2023-01-24 14:52:59 +00:00
|
|
|
|
2023-08-09 17:24:51 +00:00
|
|
|
// Do we need to push anything?
|
|
|
|
const needsPush =
|
|
|
|
localState.dirtyProps.billingAddress.length > 0 ||
|
|
|
|
localState.dirtyProps.shippingAddress.length > 0;
|
2022-02-22 17:45:01 +00:00
|
|
|
|
2023-08-09 17:24:51 +00:00
|
|
|
if ( ! needsPush ) {
|
|
|
|
localState.doingPush = false;
|
|
|
|
return;
|
2023-01-24 14:52:59 +00:00
|
|
|
}
|
2022-02-22 17:45:01 +00:00
|
|
|
|
2023-08-09 17:24:51 +00:00
|
|
|
// Check props are valid, or abort.
|
|
|
|
if ( ! validateDirtyProps( localState.dirtyProps ) ) {
|
|
|
|
localState.doingPush = false;
|
2023-01-24 14:52:59 +00:00
|
|
|
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-08-09 17:24:51 +00:00
|
|
|
if ( localState.dirtyProps.billingAddress.length ) {
|
2023-01-24 14:52:59 +00:00
|
|
|
customerDataToUpdate.billing_address = pick(
|
2023-08-09 17:24:51 +00:00
|
|
|
localState.customerData.billingAddress,
|
|
|
|
localState.dirtyProps.billingAddress
|
2023-01-24 14:52:59 +00:00
|
|
|
);
|
2022-02-22 17:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 17:24:51 +00:00
|
|
|
if ( localState.dirtyProps.shippingAddress.length ) {
|
2023-01-24 14:52:59 +00:00
|
|
|
customerDataToUpdate.shipping_address = pick(
|
2023-08-09 17:24:51 +00:00
|
|
|
localState.customerData.shippingAddress,
|
|
|
|
localState.dirtyProps.shippingAddress
|
2023-01-24 14:52:59 +00:00
|
|
|
);
|
2022-02-22 17:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 17:24:51 +00:00
|
|
|
dispatch( STORE_KEY )
|
|
|
|
.updateCustomerData( customerDataToUpdate )
|
|
|
|
.then( () => {
|
|
|
|
localState.dirtyProps.billingAddress = [];
|
|
|
|
localState.dirtyProps.shippingAddress = [];
|
|
|
|
localState.doingPush = false;
|
|
|
|
removeAllNotices();
|
|
|
|
} )
|
|
|
|
.catch( ( response ) => {
|
|
|
|
localState.doingPush = false;
|
|
|
|
processErrorResponse( response );
|
|
|
|
} );
|
|
|
|
};
|
2023-03-06 15:05:38 +00:00
|
|
|
|
2023-08-09 17:24:51 +00:00
|
|
|
/**
|
|
|
|
* Function to dispatch an update to the server. This is debounced.
|
|
|
|
*/
|
|
|
|
const debouncedUpdateCustomerData = debounce( () => {
|
|
|
|
if ( localState.doingPush ) {
|
|
|
|
debouncedUpdateCustomerData();
|
|
|
|
return;
|
2022-02-22 17:45:01 +00:00
|
|
|
}
|
2023-08-09 17:24:51 +00:00
|
|
|
updateCustomerData();
|
|
|
|
}, 1500 );
|
2022-02-22 17:45:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2023-08-09 17:24:51 +00:00
|
|
|
*
|
|
|
|
* Any update to the store triggers this, so we do a shallow compare on the important data to know if we really need to
|
|
|
|
* schedule a push.
|
2022-02-22 17:45:01 +00:00
|
|
|
*/
|
2023-08-09 17:24:51 +00:00
|
|
|
export const pushChanges = ( debounced = true ): void => {
|
|
|
|
if ( ! select( STORE_KEY ).hasFinishedResolution( 'getCartData' ) ) {
|
2022-02-22 17:45:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-09 17:24:51 +00:00
|
|
|
if ( ! localState.customerDataIsInitialized ) {
|
|
|
|
initialize();
|
2022-02-22 17:45:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-24 14:52:59 +00:00
|
|
|
if (
|
2023-08-09 17:24:51 +00:00
|
|
|
isShallowEqual(
|
|
|
|
localState.customerData,
|
|
|
|
select( STORE_KEY ).getCustomerData()
|
|
|
|
)
|
2023-01-24 14:52:59 +00:00
|
|
|
) {
|
2023-08-09 17:24:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( debounced ) {
|
|
|
|
debouncedUpdateCustomerData();
|
|
|
|
} else {
|
2022-02-22 17:45:01 +00:00
|
|
|
updateCustomerData();
|
|
|
|
}
|
|
|
|
};
|
2023-02-15 12:00:16 +00:00
|
|
|
|
2023-08-09 17:24:51 +00:00
|
|
|
// Cancel the debounced updateCustomerData function and trigger it immediately.
|
2023-02-15 12:00:16 +00:00
|
|
|
export const flushChanges = (): void => {
|
2023-08-09 17:24:51 +00:00
|
|
|
debouncedUpdateCustomerData.flush();
|
2023-02-15 12:00:16 +00:00
|
|
|
};
|