2022-02-22 17:45:01 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
defaultAddressFields,
|
|
|
|
AddressFields,
|
|
|
|
EnteredAddress,
|
|
|
|
ShippingAddress,
|
|
|
|
BillingAddress,
|
|
|
|
} from '@woocommerce/settings';
|
|
|
|
import { useCallback } from '@wordpress/element';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-03-04 17:43:45 +00:00
|
|
|
import { useCheckoutContext } from '../providers/cart-checkout';
|
2022-02-22 17:45:01 +00:00
|
|
|
import { useCustomerData } from './use-customer-data';
|
2022-03-04 17:43:45 +00:00
|
|
|
import { useShippingData } from './shipping/use-shipping-data';
|
2022-02-22 17:45:01 +00:00
|
|
|
|
|
|
|
interface CheckoutAddress {
|
|
|
|
shippingAddress: ShippingAddress;
|
2022-06-10 16:59:25 +00:00
|
|
|
billingAddress: BillingAddress;
|
2022-02-22 17:45:01 +00:00
|
|
|
setShippingAddress: ( data: Partial< EnteredAddress > ) => void;
|
2022-06-10 16:59:25 +00:00
|
|
|
setBillingAddress: ( data: Partial< EnteredAddress > ) => void;
|
2022-02-22 17:45:01 +00:00
|
|
|
setEmail: ( value: string ) => void;
|
|
|
|
setBillingPhone: ( value: string ) => void;
|
|
|
|
setShippingPhone: ( value: string ) => void;
|
|
|
|
useShippingAsBilling: boolean;
|
|
|
|
setUseShippingAsBilling: ( useShippingAsBilling: boolean ) => void;
|
|
|
|
defaultAddressFields: AddressFields;
|
|
|
|
showShippingFields: boolean;
|
|
|
|
showBillingFields: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom hook for exposing address related functionality for the checkout address form.
|
|
|
|
*/
|
|
|
|
export const useCheckoutAddress = (): CheckoutAddress => {
|
2022-03-04 17:43:45 +00:00
|
|
|
const { needsShipping } = useShippingData();
|
2022-06-15 09:56:52 +00:00
|
|
|
const { useShippingAsBilling, setUseShippingAsBilling } =
|
|
|
|
useCheckoutContext();
|
2022-02-22 17:45:01 +00:00
|
|
|
const {
|
2022-06-10 16:59:25 +00:00
|
|
|
billingAddress,
|
|
|
|
setBillingAddress,
|
2022-02-22 17:45:01 +00:00
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
|
|
|
} = useCustomerData();
|
|
|
|
|
|
|
|
const setEmail = useCallback(
|
|
|
|
( value ) =>
|
2022-06-10 16:59:25 +00:00
|
|
|
void setBillingAddress( {
|
2022-02-22 17:45:01 +00:00
|
|
|
email: value,
|
|
|
|
} ),
|
2022-06-10 16:59:25 +00:00
|
|
|
[ setBillingAddress ]
|
2022-02-22 17:45:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const setBillingPhone = useCallback(
|
|
|
|
( value ) =>
|
2022-06-10 16:59:25 +00:00
|
|
|
void setBillingAddress( {
|
2022-02-22 17:45:01 +00:00
|
|
|
phone: value,
|
|
|
|
} ),
|
2022-06-10 16:59:25 +00:00
|
|
|
[ setBillingAddress ]
|
2022-02-22 17:45:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const setShippingPhone = useCallback(
|
|
|
|
( value ) =>
|
|
|
|
void setShippingAddress( {
|
|
|
|
phone: value,
|
|
|
|
} ),
|
|
|
|
[ setShippingAddress ]
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
shippingAddress,
|
2022-06-10 16:59:25 +00:00
|
|
|
billingAddress,
|
2022-02-22 17:45:01 +00:00
|
|
|
setShippingAddress,
|
2022-06-10 16:59:25 +00:00
|
|
|
setBillingAddress,
|
2022-02-22 17:45:01 +00:00
|
|
|
setEmail,
|
|
|
|
setBillingPhone,
|
|
|
|
setShippingPhone,
|
|
|
|
defaultAddressFields,
|
|
|
|
useShippingAsBilling,
|
|
|
|
setUseShippingAsBilling,
|
|
|
|
showShippingFields: needsShipping,
|
|
|
|
showBillingFields: ! needsShipping || ! useShippingAsBilling,
|
|
|
|
};
|
|
|
|
};
|