2020-05-06 10:21:30 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-04-08 12:31:12 +00:00
|
|
|
import { defaultAddressFields } from '@woocommerce/settings';
|
2021-07-22 11:03:00 +00:00
|
|
|
import { useEffect, useCallback, useRef } from '@wordpress/element';
|
2021-04-08 12:31:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-05-06 10:21:30 +00:00
|
|
|
import {
|
|
|
|
useShippingDataContext,
|
2020-11-20 15:13:35 +00:00
|
|
|
useCustomerDataContext,
|
2021-04-08 12:31:12 +00:00
|
|
|
} from '../providers/cart-checkout';
|
2020-05-06 10:21:30 +00:00
|
|
|
|
|
|
|
/**
|
2020-11-20 15:13:35 +00:00
|
|
|
* Custom hook for exposing address related functionality for the checkout address form.
|
2020-05-06 10:21:30 +00:00
|
|
|
*/
|
|
|
|
export const useCheckoutAddress = () => {
|
2020-11-20 15:13:35 +00:00
|
|
|
const { needsShipping } = useShippingDataContext();
|
2020-05-06 10:21:30 +00:00
|
|
|
const {
|
2020-11-20 15:13:35 +00:00
|
|
|
billingData,
|
|
|
|
setBillingData,
|
2020-05-06 10:21:30 +00:00
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
2021-07-22 11:03:00 +00:00
|
|
|
shippingAsBilling,
|
|
|
|
setShippingAsBilling,
|
2020-11-20 15:13:35 +00:00
|
|
|
} = useCustomerDataContext();
|
2020-05-06 10:21:30 +00:00
|
|
|
|
2020-11-20 15:13:35 +00:00
|
|
|
const currentShippingAsBilling = useRef( shippingAsBilling );
|
|
|
|
const previousBillingData = useRef( billingData );
|
2020-05-06 10:21:30 +00:00
|
|
|
|
|
|
|
/**
|
2020-11-20 15:13:35 +00:00
|
|
|
* Sets shipping address data, and also billing if using the same address.
|
2020-05-06 10:21:30 +00:00
|
|
|
*/
|
2020-11-20 15:13:35 +00:00
|
|
|
const setShippingFields = useCallback(
|
|
|
|
( value ) => {
|
|
|
|
setShippingAddress( value );
|
|
|
|
|
|
|
|
if ( shippingAsBilling ) {
|
|
|
|
setBillingData( value );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[ shippingAsBilling, setShippingAddress, setBillingData ]
|
2020-05-14 00:05:09 +00:00
|
|
|
);
|
2020-05-06 10:21:30 +00:00
|
|
|
|
2020-12-23 15:10:13 +00:00
|
|
|
/**
|
|
|
|
* Sets billing address data, and also shipping if shipping is disabled.
|
|
|
|
*/
|
|
|
|
const setBillingFields = useCallback(
|
|
|
|
( value ) => {
|
|
|
|
setBillingData( value );
|
|
|
|
|
|
|
|
if ( ! needsShipping ) {
|
|
|
|
setShippingAddress( value );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[ needsShipping, setShippingAddress, setBillingData ]
|
|
|
|
);
|
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
// When the "Use same address" checkbox is toggled we need to update the current billing address to reflect this.
|
|
|
|
// This either sets the billing address to the shipping address, or restores the billing address to it's previous state.
|
2020-11-20 15:13:35 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( currentShippingAsBilling.current !== shippingAsBilling ) {
|
|
|
|
if ( shippingAsBilling ) {
|
|
|
|
previousBillingData.current = billingData;
|
|
|
|
setBillingData( shippingAddress );
|
|
|
|
} else {
|
2021-05-05 11:59:30 +00:00
|
|
|
const {
|
2021-07-22 11:03:00 +00:00
|
|
|
// We need to pluck out email from previous billing data because they can be empty, causing the current email to get emptied. See issue #4155
|
2021-05-05 11:59:30 +00:00
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
email,
|
|
|
|
/* eslint-enable no-unused-vars */
|
|
|
|
...billingAddress
|
|
|
|
} = previousBillingData.current;
|
2021-07-22 11:03:00 +00:00
|
|
|
|
2020-11-20 15:13:35 +00:00
|
|
|
setBillingData( {
|
2021-05-05 11:59:30 +00:00
|
|
|
...billingAddress,
|
2020-11-20 15:13:35 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
currentShippingAsBilling.current = shippingAsBilling;
|
|
|
|
}
|
|
|
|
}, [ shippingAsBilling, setBillingData, shippingAddress, billingData ] );
|
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
const setEmail = useCallback(
|
|
|
|
( value ) =>
|
|
|
|
void setBillingData( {
|
|
|
|
email: value,
|
|
|
|
} ),
|
|
|
|
[ setBillingData ]
|
|
|
|
);
|
|
|
|
|
|
|
|
const setPhone = useCallback(
|
|
|
|
( value ) =>
|
|
|
|
void setBillingData( {
|
|
|
|
phone: value,
|
|
|
|
} ),
|
|
|
|
[ setBillingData ]
|
|
|
|
);
|
|
|
|
|
|
|
|
const setShippingPhone = useCallback(
|
|
|
|
( value ) =>
|
|
|
|
void setShippingFields( {
|
|
|
|
phone: value,
|
|
|
|
} ),
|
|
|
|
[ setShippingFields ]
|
|
|
|
);
|
2020-05-06 10:21:30 +00:00
|
|
|
|
2020-11-20 15:13:35 +00:00
|
|
|
// Note that currentShippingAsBilling is returned rather than the current state of shippingAsBilling--this is so that
|
|
|
|
// the billing fields are not rendered before sync (billing field values are debounced and would be outdated)
|
2020-05-06 10:21:30 +00:00
|
|
|
return {
|
|
|
|
defaultAddressFields,
|
2020-05-14 00:05:09 +00:00
|
|
|
shippingFields: shippingAddress,
|
2020-11-20 15:13:35 +00:00
|
|
|
setShippingFields,
|
|
|
|
billingFields: billingData,
|
2020-12-23 15:10:13 +00:00
|
|
|
setBillingFields,
|
2020-05-06 10:21:30 +00:00
|
|
|
setEmail,
|
|
|
|
setPhone,
|
2021-07-22 11:03:00 +00:00
|
|
|
setShippingPhone,
|
2020-05-06 10:21:30 +00:00
|
|
|
shippingAsBilling,
|
|
|
|
setShippingAsBilling,
|
2021-07-22 11:03:00 +00:00
|
|
|
showShippingFields: needsShipping,
|
2020-11-20 15:13:35 +00:00
|
|
|
showBillingFields:
|
|
|
|
! needsShipping || ! currentShippingAsBilling.current,
|
2020-05-06 10:21:30 +00:00
|
|
|
};
|
|
|
|
};
|