2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-12-02 11:34:27 +00:00
|
|
|
import { useMemo, useEffect, Fragment, useState } from '@wordpress/element';
|
2021-07-22 11:03:00 +00:00
|
|
|
import {
|
|
|
|
useCheckoutAddress,
|
|
|
|
useStoreEvents,
|
|
|
|
useEditorContext,
|
2022-12-19 15:30:13 +00:00
|
|
|
noticeContexts,
|
2021-07-22 11:03:00 +00:00
|
|
|
} from '@woocommerce/base-context';
|
|
|
|
import { AddressForm } from '@woocommerce/base-components/cart-checkout';
|
2021-11-26 14:47:37 +00:00
|
|
|
import Noninteractive from '@woocommerce/base-components/noninteractive';
|
2022-02-22 17:45:01 +00:00
|
|
|
import type {
|
|
|
|
BillingAddress,
|
|
|
|
AddressField,
|
|
|
|
AddressFields,
|
|
|
|
} from '@woocommerce/settings';
|
2022-12-19 15:30:13 +00:00
|
|
|
import { StoreNoticesContainer } from '@woocommerce/blocks-checkout';
|
2021-07-22 11:03:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import PhoneNumber from '../../phone-number';
|
|
|
|
|
|
|
|
const Block = ( {
|
|
|
|
showCompanyField = false,
|
|
|
|
showApartmentField = false,
|
|
|
|
showPhoneField = false,
|
|
|
|
requireCompanyField = false,
|
|
|
|
requirePhoneField = false,
|
|
|
|
}: {
|
|
|
|
showCompanyField: boolean;
|
|
|
|
showApartmentField: boolean;
|
|
|
|
showPhoneField: boolean;
|
|
|
|
requireCompanyField: boolean;
|
|
|
|
requirePhoneField: boolean;
|
|
|
|
} ): JSX.Element => {
|
|
|
|
const {
|
|
|
|
defaultAddressFields,
|
2022-06-10 16:59:25 +00:00
|
|
|
billingAddress,
|
|
|
|
setBillingAddress,
|
2022-10-07 07:50:26 +00:00
|
|
|
setShippingAddress,
|
2022-02-22 17:45:01 +00:00
|
|
|
setBillingPhone,
|
2022-12-23 15:05:56 +00:00
|
|
|
setShippingPhone,
|
|
|
|
useBillingAsShipping,
|
2021-07-22 11:03:00 +00:00
|
|
|
} = useCheckoutAddress();
|
|
|
|
const { dispatchCheckoutEvent } = useStoreEvents();
|
|
|
|
const { isEditor } = useEditorContext();
|
|
|
|
// Clears data if fields are hidden.
|
|
|
|
useEffect( () => {
|
|
|
|
if ( ! showPhoneField ) {
|
2022-02-22 17:45:01 +00:00
|
|
|
setBillingPhone( '' );
|
2021-07-22 11:03:00 +00:00
|
|
|
}
|
2022-02-22 17:45:01 +00:00
|
|
|
}, [ showPhoneField, setBillingPhone ] );
|
2021-07-22 11:03:00 +00:00
|
|
|
|
2022-12-02 11:34:27 +00:00
|
|
|
const [ addressesSynced, setAddressesSynced ] = useState( false );
|
|
|
|
|
|
|
|
// Syncs shipping address with billing address if "Force shipping to the customer billing address" is enabled.
|
|
|
|
useEffect( () => {
|
|
|
|
if ( addressesSynced ) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-23 15:05:56 +00:00
|
|
|
if ( useBillingAsShipping ) {
|
2022-12-02 11:34:27 +00:00
|
|
|
setShippingAddress( billingAddress );
|
|
|
|
}
|
|
|
|
setAddressesSynced( true );
|
|
|
|
}, [
|
|
|
|
addressesSynced,
|
|
|
|
setShippingAddress,
|
|
|
|
billingAddress,
|
2022-12-23 15:05:56 +00:00
|
|
|
useBillingAsShipping,
|
2022-12-02 11:34:27 +00:00
|
|
|
] );
|
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
const addressFieldsConfig = useMemo( () => {
|
|
|
|
return {
|
|
|
|
company: {
|
|
|
|
hidden: ! showCompanyField,
|
|
|
|
required: requireCompanyField,
|
|
|
|
},
|
|
|
|
address_2: {
|
|
|
|
hidden: ! showApartmentField,
|
|
|
|
},
|
|
|
|
};
|
2022-02-22 17:45:01 +00:00
|
|
|
}, [
|
|
|
|
showCompanyField,
|
|
|
|
requireCompanyField,
|
|
|
|
showApartmentField,
|
|
|
|
] ) as Record< keyof AddressFields, Partial< AddressField > >;
|
2021-07-22 11:03:00 +00:00
|
|
|
|
2021-11-26 14:47:37 +00:00
|
|
|
const AddressFormWrapperComponent = isEditor ? Noninteractive : Fragment;
|
2021-07-22 11:03:00 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<AddressFormWrapperComponent>
|
2022-12-19 15:30:13 +00:00
|
|
|
<StoreNoticesContainer context={ noticeContexts.BILLING_ADDRESS } />
|
2021-07-22 11:03:00 +00:00
|
|
|
<AddressForm
|
|
|
|
id="billing"
|
|
|
|
type="billing"
|
2022-02-22 17:45:01 +00:00
|
|
|
onChange={ ( values: Partial< BillingAddress > ) => {
|
2022-06-10 16:59:25 +00:00
|
|
|
setBillingAddress( values );
|
2022-12-23 15:05:56 +00:00
|
|
|
if ( useBillingAsShipping ) {
|
2022-10-07 07:50:26 +00:00
|
|
|
setShippingAddress( values );
|
2022-12-02 11:34:27 +00:00
|
|
|
dispatchCheckoutEvent( 'set-shipping-address' );
|
2022-10-07 07:50:26 +00:00
|
|
|
}
|
2021-07-22 11:03:00 +00:00
|
|
|
dispatchCheckoutEvent( 'set-billing-address' );
|
|
|
|
} }
|
2022-06-10 16:59:25 +00:00
|
|
|
values={ billingAddress }
|
2022-02-22 17:45:01 +00:00
|
|
|
fields={
|
|
|
|
Object.keys(
|
|
|
|
defaultAddressFields
|
|
|
|
) as ( keyof AddressFields )[]
|
|
|
|
}
|
2021-07-22 11:03:00 +00:00
|
|
|
fieldConfig={ addressFieldsConfig }
|
|
|
|
/>
|
|
|
|
{ showPhoneField && (
|
|
|
|
<PhoneNumber
|
|
|
|
isRequired={ requirePhoneField }
|
2022-06-10 16:59:25 +00:00
|
|
|
value={ billingAddress.phone }
|
2021-07-22 11:03:00 +00:00
|
|
|
onChange={ ( value ) => {
|
2022-02-22 17:45:01 +00:00
|
|
|
setBillingPhone( value );
|
2021-07-22 11:03:00 +00:00
|
|
|
dispatchCheckoutEvent( 'set-phone-number', {
|
|
|
|
step: 'billing',
|
|
|
|
} );
|
2022-12-23 15:05:56 +00:00
|
|
|
if ( useBillingAsShipping ) {
|
|
|
|
setShippingPhone( value );
|
|
|
|
dispatchCheckoutEvent( 'set-phone-number', {
|
|
|
|
step: 'shipping',
|
|
|
|
} );
|
|
|
|
}
|
2021-07-22 11:03:00 +00:00
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</AddressFormWrapperComponent>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Block;
|