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-03-10 15:03:26 +00:00
|
|
|
import type {
|
|
|
|
CartResponseBillingAddress,
|
|
|
|
CartResponseShippingAddress,
|
|
|
|
} 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
|
|
|
|
|
|
|
// 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.
|
|
|
|
*
|
|
|
|
* Contains special handling for email and phone so those fields are not overwritten if simply updating address.
|
|
|
|
*/
|
|
|
|
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 ) => {
|
|
|
|
setCustomerData( ( prevState ) => ( {
|
|
|
|
...prevState,
|
|
|
|
shippingAddress: newData,
|
|
|
|
} ) );
|
|
|
|
}, [] );
|
|
|
|
|
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.
|
2020-11-20 15:13:35 +00:00
|
|
|
if (
|
2020-12-17 14:52:44 +00:00
|
|
|
! shouldUpdateAddressStore(
|
|
|
|
previousCustomerData.current.billingData,
|
|
|
|
debouncedCustomerData.billingData
|
|
|
|
) &&
|
|
|
|
! shouldUpdateAddressStore(
|
|
|
|
previousCustomerData.current.shippingAddress,
|
|
|
|
debouncedCustomerData.shippingAddress
|
2020-11-20 15:13:35 +00:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-17 14:52:44 +00:00
|
|
|
previousCustomerData.current = debouncedCustomerData;
|
2020-11-20 15:13:35 +00:00
|
|
|
updateCustomerData( {
|
|
|
|
billing_address: debouncedCustomerData.billingData,
|
|
|
|
shipping_address: debouncedCustomerData.shippingAddress,
|
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,
|
|
|
|
};
|
|
|
|
};
|