2020-11-20 15:13:35 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useDispatch } from '@wordpress/data';
|
|
|
|
import { useEffect, useState, useCallback, useRef } from '@wordpress/element';
|
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
|
|
|
import { useDebounce } from 'use-debounce';
|
|
|
|
import isShallowEqual from '@wordpress/is-shallow-equal';
|
2021-04-08 12:31:12 +00:00
|
|
|
import {
|
|
|
|
formatStoreApiErrorMessage,
|
|
|
|
pluckAddress,
|
2021-05-13 10:20:37 +00:00
|
|
|
pluckEmail,
|
2021-04-08 12:31:12 +00:00
|
|
|
} from '@woocommerce/base-utils';
|
2021-11-16 12:39:43 +00:00
|
|
|
import {
|
2021-03-10 15:03:26 +00:00
|
|
|
CartResponseBillingAddress,
|
|
|
|
CartResponseShippingAddress,
|
2021-11-16 12:39:43 +00:00
|
|
|
BillingAddressShippingAddress,
|
|
|
|
CartBillingAddress,
|
|
|
|
CartShippingAddress,
|
2021-03-10 15:03:26 +00:00
|
|
|
} from '@woocommerce/types';
|
2020-11-20 15:13:35 +00:00
|
|
|
|
2021-03-10 15:03:26 +00:00
|
|
|
declare type CustomerData = {
|
|
|
|
billingData: CartResponseBillingAddress;
|
|
|
|
shippingAddress: CartResponseShippingAddress;
|
|
|
|
};
|
2021-04-08 12:31:12 +00:00
|
|
|
|
2020-11-20 15:13:35 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-04-08 12:31:12 +00:00
|
|
|
import { useStoreCart } from './cart/use-store-cart';
|
|
|
|
import { useStoreNotices } from './use-store-notices';
|
|
|
|
|
2021-05-13 10:20:37 +00:00
|
|
|
function instanceOfCartResponseBillingAddress(
|
|
|
|
address: CartResponseBillingAddress | CartResponseShippingAddress
|
|
|
|
): address is CartResponseBillingAddress {
|
|
|
|
return 'email' in address;
|
|
|
|
}
|
|
|
|
|
2021-04-08 12:31:12 +00:00
|
|
|
/**
|
2021-05-13 10:20:37 +00:00
|
|
|
* 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.
|
2021-04-08 12:31:12 +00:00
|
|
|
*
|
|
|
|
* @param {Object} previousAddress An object containing all previous address information
|
|
|
|
* @param {Object} address An object containing all address information
|
|
|
|
*
|
|
|
|
* @return {boolean} True if the store needs updating due to changed data.
|
|
|
|
*/
|
|
|
|
const shouldUpdateAddressStore = <
|
|
|
|
T extends CartResponseBillingAddress | CartResponseShippingAddress
|
|
|
|
>(
|
|
|
|
previousAddress: T,
|
|
|
|
address: T
|
|
|
|
): boolean => {
|
2021-05-13 10:20:37 +00:00
|
|
|
if (
|
|
|
|
instanceOfCartResponseBillingAddress( address ) &&
|
|
|
|
pluckEmail( address ) !==
|
|
|
|
pluckEmail( previousAddress as CartResponseBillingAddress )
|
|
|
|
) {
|
|
|
|
return true;
|
2021-04-08 12:31:12 +00:00
|
|
|
}
|
2021-05-13 10:20:37 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
!! address.country &&
|
|
|
|
! isShallowEqual(
|
|
|
|
pluckAddress( previousAddress ),
|
|
|
|
pluckAddress( address )
|
|
|
|
)
|
2021-04-08 12:31:12 +00:00
|
|
|
);
|
|
|
|
};
|
2020-11-20 15:13:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a custom hook for syncing customer address data (billing and shipping) with the server.
|
|
|
|
*/
|
2021-03-10 15:03:26 +00:00
|
|
|
export const useCustomerData = (): {
|
|
|
|
billingData: CartResponseBillingAddress;
|
|
|
|
shippingAddress: CartResponseShippingAddress;
|
|
|
|
setBillingData: ( data: CartResponseBillingAddress ) => void;
|
2021-05-25 11:49:13 +00:00
|
|
|
setShippingAddress: ( data: CartResponseShippingAddress ) => void;
|
2021-03-10 15:03:26 +00:00
|
|
|
} => {
|
2020-11-20 15:13:35 +00:00
|
|
|
const { updateCustomerData } = useDispatch( storeKey );
|
|
|
|
const { addErrorNotice, removeNotice } = useStoreNotices();
|
2020-12-17 14:52:44 +00:00
|
|
|
|
|
|
|
// Grab the initial values from the store cart hook.
|
2020-11-20 15:13:35 +00:00
|
|
|
const {
|
2020-12-17 14:52:44 +00:00
|
|
|
billingAddress: initialBillingAddress,
|
|
|
|
shippingAddress: initialShippingAddress,
|
2021-03-10 15:03:26 +00:00
|
|
|
}: Omit< CustomerData, 'billingData' > & {
|
|
|
|
billingAddress: CartResponseBillingAddress;
|
2020-11-20 15:13:35 +00:00
|
|
|
} = useStoreCart();
|
|
|
|
|
2020-12-17 14:52:44 +00:00
|
|
|
// State of customer data is tracked here from this point, using the initial values from the useStoreCart hook.
|
2021-03-10 15:03:26 +00:00
|
|
|
const [ customerData, setCustomerData ] = useState< CustomerData >( {
|
2020-12-17 14:52:44 +00:00
|
|
|
billingData: initialBillingAddress,
|
|
|
|
shippingAddress: initialShippingAddress,
|
2020-11-20 15:13:35 +00:00
|
|
|
} );
|
2020-12-17 14:52:44 +00:00
|
|
|
|
2021-11-16 12:39:43 +00:00
|
|
|
// We only want to update the local state once, otherwise the data on the checkout page gets overwritten
|
|
|
|
// with the initial state of the addresses here
|
|
|
|
const [ hasCustomerDataSynced, setHasCustomerDataSynced ] = useState<
|
|
|
|
boolean
|
|
|
|
>( false );
|
|
|
|
|
|
|
|
if (
|
|
|
|
! hasCustomerDataSynced &&
|
|
|
|
shouldUpdateAddressStore(
|
|
|
|
customerData.shippingAddress,
|
|
|
|
initialShippingAddress
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
setCustomerData( {
|
|
|
|
billingData: initialBillingAddress,
|
|
|
|
shippingAddress: initialShippingAddress,
|
|
|
|
} );
|
|
|
|
setHasCustomerDataSynced( true );
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:52:44 +00:00
|
|
|
// Store values last sent to the server in a ref to avoid requests unless important fields are changed.
|
2021-03-10 15:03:26 +00:00
|
|
|
const previousCustomerData = useRef< CustomerData >( customerData );
|
2020-12-17 14:52:44 +00:00
|
|
|
|
|
|
|
// Debounce updates to the customerData state so it's not triggered excessively.
|
|
|
|
const [ debouncedCustomerData ] = useDebounce( customerData, 1000, {
|
|
|
|
// Default equalityFn is prevData === newData.
|
2020-11-20 15:13:35 +00:00
|
|
|
equalityFn: ( prevData, newData ) => {
|
2020-12-17 14:52:44 +00:00
|
|
|
return (
|
|
|
|
isShallowEqual( prevData.billingData, newData.billingData ) &&
|
2020-11-20 15:13:35 +00:00
|
|
|
isShallowEqual(
|
|
|
|
prevData.shippingAddress,
|
|
|
|
newData.shippingAddress
|
|
|
|
)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set billing data.
|
|
|
|
*
|
2021-07-22 11:03:00 +00:00
|
|
|
* Contains special handling for email so those fields are not overwritten if simply updating address.
|
2020-11-20 15:13:35 +00:00
|
|
|
*/
|
|
|
|
const setBillingData = useCallback( ( newData ) => {
|
|
|
|
setCustomerData( ( prevState ) => {
|
|
|
|
return {
|
|
|
|
...prevState,
|
|
|
|
billingData: {
|
|
|
|
...prevState.billingData,
|
|
|
|
...newData,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
}, [] );
|
|
|
|
|
2020-12-17 14:52:44 +00:00
|
|
|
/**
|
|
|
|
* Set shipping data.
|
|
|
|
*/
|
2020-11-20 15:13:35 +00:00
|
|
|
const setShippingAddress = useCallback( ( newData ) => {
|
2021-07-22 11:03:00 +00:00
|
|
|
setCustomerData( ( prevState ) => {
|
|
|
|
return {
|
|
|
|
...prevState,
|
|
|
|
shippingAddress: {
|
|
|
|
...prevState.shippingAddress,
|
|
|
|
...newData,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} );
|
2020-11-20 15:13:35 +00:00
|
|
|
}, [] );
|
|
|
|
|
2020-12-17 14:52:44 +00:00
|
|
|
/**
|
|
|
|
* This pushes changes to the API when the local state differs from the address in the cart.
|
|
|
|
*/
|
2020-11-20 15:13:35 +00:00
|
|
|
useEffect( () => {
|
2020-12-17 14:52:44 +00:00
|
|
|
// Only push updates when enough fields are populated.
|
2021-11-16 12:39:43 +00:00
|
|
|
const shouldUpdateBillingAddress = shouldUpdateAddressStore(
|
|
|
|
previousCustomerData.current.billingData,
|
|
|
|
debouncedCustomerData.billingData
|
|
|
|
);
|
|
|
|
|
|
|
|
const shouldUpdateShippingAddress = shouldUpdateAddressStore(
|
|
|
|
previousCustomerData.current.shippingAddress,
|
|
|
|
debouncedCustomerData.shippingAddress
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! shouldUpdateBillingAddress && ! shouldUpdateShippingAddress ) {
|
2020-11-20 15:13:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-11-16 12:39:43 +00:00
|
|
|
|
|
|
|
const customerDataToUpdate:
|
|
|
|
| Partial< BillingAddressShippingAddress >
|
|
|
|
| Record<
|
|
|
|
keyof BillingAddressShippingAddress,
|
|
|
|
CartBillingAddress | CartShippingAddress
|
|
|
|
> = {};
|
|
|
|
|
|
|
|
if ( shouldUpdateBillingAddress ) {
|
|
|
|
customerDataToUpdate.billing_address =
|
|
|
|
debouncedCustomerData.billingData;
|
|
|
|
}
|
|
|
|
if ( shouldUpdateShippingAddress ) {
|
|
|
|
customerDataToUpdate.shipping_address =
|
|
|
|
debouncedCustomerData.shippingAddress;
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:52:44 +00:00
|
|
|
previousCustomerData.current = debouncedCustomerData;
|
2021-11-16 12:39:43 +00:00
|
|
|
updateCustomerData(
|
|
|
|
customerDataToUpdate as Partial< BillingAddressShippingAddress >
|
|
|
|
)
|
2020-12-17 14:52:44 +00:00
|
|
|
.then( () => {
|
|
|
|
removeNotice( 'checkout' );
|
|
|
|
} )
|
|
|
|
.catch( ( response ) => {
|
|
|
|
addErrorNotice( formatStoreApiErrorMessage( response ), {
|
|
|
|
id: 'checkout',
|
|
|
|
} );
|
2020-11-20 15:13:35 +00:00
|
|
|
} );
|
|
|
|
}, [
|
|
|
|
debouncedCustomerData,
|
|
|
|
addErrorNotice,
|
|
|
|
removeNotice,
|
|
|
|
updateCustomerData,
|
|
|
|
] );
|
|
|
|
return {
|
|
|
|
billingData: customerData.billingData,
|
|
|
|
shippingAddress: customerData.shippingAddress,
|
|
|
|
setBillingData,
|
|
|
|
setShippingAddress,
|
|
|
|
};
|
|
|
|
};
|