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';
|
2021-12-31 15:10:43 +00:00
|
|
|
import { useDebouncedCallback } from 'use-debounce';
|
2020-11-20 15:13:35 +00:00
|
|
|
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,
|
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.
|
2021-12-31 15:10:43 +00:00
|
|
|
// NOTE: The initial values may not be current if the cart has not yet finished loading. See cartIsLoading.
|
2020-11-20 15:13:35 +00:00
|
|
|
const {
|
2020-12-17 14:52:44 +00:00
|
|
|
billingAddress: initialBillingAddress,
|
|
|
|
shippingAddress: initialShippingAddress,
|
2021-12-31 15:10:43 +00:00
|
|
|
cartIsLoading,
|
2020-11-20 15:13:35 +00:00
|
|
|
} = useStoreCart();
|
|
|
|
|
2021-12-31 15:10: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. We also only want to start triggering updates to the server when the
|
|
|
|
// initial data has fully initialized. Track that header.
|
|
|
|
const [ isInitialized, setIsInitialized ] = useState< boolean >( false );
|
|
|
|
|
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
|
|
|
|
2021-12-31 15:10:43 +00:00
|
|
|
// When the cart data is resolved from server for the first time (using cartIsLoading) we need to update
|
|
|
|
// the initial billing and shipping values to respect customer data from the server.
|
|
|
|
useEffect( () => {
|
|
|
|
if ( isInitialized || cartIsLoading ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const initializedCustomerData = {
|
|
|
|
billingData: initialBillingAddress,
|
|
|
|
shippingAddress: initialShippingAddress,
|
|
|
|
};
|
|
|
|
// Updates local state to the now-resolved cart address.
|
|
|
|
previousCustomerData.current = initializedCustomerData;
|
|
|
|
setCustomerData( initializedCustomerData );
|
|
|
|
// We are now initialized.
|
|
|
|
setIsInitialized( true );
|
|
|
|
}, [
|
|
|
|
cartIsLoading,
|
|
|
|
isInitialized,
|
|
|
|
initialBillingAddress,
|
|
|
|
initialShippingAddress,
|
|
|
|
] );
|
2020-11-20 15:13:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set billing data.
|
|
|
|
*
|
2021-12-31 15:10:43 +00:00
|
|
|
* Callback used to set billing data for the customer. This merges the previous and new state, and in turn,
|
|
|
|
* will trigger an update to the server if enough data has changed (see the useEffect call below).
|
|
|
|
*
|
|
|
|
* This callback contains special handling for the "email" address field so that field is never overwritten if
|
|
|
|
* simply updating the billing address and not the email 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
|
|
|
/**
|
2021-12-31 15:10:43 +00:00
|
|
|
* Set shipping address.
|
|
|
|
*
|
|
|
|
* Callback used to set shipping data for the customer. This merges the previous and new state, and in turn, will
|
|
|
|
* trigger an update to the server if enough data has changed (see the useEffect call below).
|
2020-12-17 14:52:44 +00:00
|
|
|
*/
|
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.
|
2021-12-31 15:10:43 +00:00
|
|
|
*
|
|
|
|
* The function shouldUpdateAddressStore() determines if enough data has changed to trigger the update.
|
2020-12-17 14:52:44 +00:00
|
|
|
*/
|
2021-12-31 15:10:43 +00:00
|
|
|
const pushCustomerData = () => {
|
|
|
|
const customerDataToUpdate: Partial< BillingAddressShippingAddress > = {};
|
|
|
|
|
|
|
|
if (
|
|
|
|
shouldUpdateAddressStore(
|
|
|
|
previousCustomerData.current.billingData,
|
|
|
|
customerData.billingData
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
customerDataToUpdate.billing_address = customerData.billingData;
|
2020-11-20 15:13:35 +00:00
|
|
|
}
|
2021-11-16 12:39:43 +00:00
|
|
|
|
2021-12-31 15:10:43 +00:00
|
|
|
if (
|
|
|
|
shouldUpdateAddressStore(
|
|
|
|
previousCustomerData.current.shippingAddress,
|
|
|
|
customerData.shippingAddress
|
|
|
|
)
|
|
|
|
) {
|
2021-11-16 12:39:43 +00:00
|
|
|
customerDataToUpdate.shipping_address =
|
2021-12-31 15:10:43 +00:00
|
|
|
customerData.shippingAddress;
|
2021-11-16 12:39:43 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 15:10:43 +00:00
|
|
|
if ( Object.keys( customerDataToUpdate ).length === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousCustomerData.current = customerData;
|
|
|
|
|
|
|
|
updateCustomerData( customerDataToUpdate )
|
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
|
|
|
} );
|
2021-12-31 15:10:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const debouncedPushCustomerData = useDebouncedCallback(
|
|
|
|
pushCustomerData,
|
|
|
|
1000
|
|
|
|
);
|
|
|
|
|
|
|
|
// If data changes, trigger an update to the server only if initialized.
|
|
|
|
useEffect( () => {
|
|
|
|
if ( ! isInitialized ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
debouncedPushCustomerData();
|
|
|
|
}, [ isInitialized, customerData, debouncedPushCustomerData ] );
|
|
|
|
|
2020-11-20 15:13:35 +00:00
|
|
|
return {
|
|
|
|
billingData: customerData.billingData,
|
|
|
|
shippingAddress: customerData.shippingAddress,
|
|
|
|
setBillingData,
|
|
|
|
setShippingAddress,
|
|
|
|
};
|
|
|
|
};
|