2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { useMemo, useEffect, Fragment } from '@wordpress/element';
|
|
|
|
import { AddressForm } from '@woocommerce/base-components/cart-checkout';
|
|
|
|
import {
|
|
|
|
useCheckoutAddress,
|
|
|
|
useStoreEvents,
|
|
|
|
useEditorContext,
|
|
|
|
} from '@woocommerce/base-context';
|
2021-11-09 16:47:46 +00:00
|
|
|
import { CheckboxControl } from '@woocommerce/blocks-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,
|
|
|
|
ShippingAddress,
|
|
|
|
AddressField,
|
|
|
|
AddressFields,
|
|
|
|
} from '@woocommerce/settings';
|
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-02-22 17:45:01 +00:00
|
|
|
setShippingAddress,
|
|
|
|
setBillingData,
|
|
|
|
shippingAddress,
|
2021-07-22 11:03:00 +00:00
|
|
|
setShippingPhone,
|
2022-02-22 17:45:01 +00:00
|
|
|
useShippingAsBilling,
|
|
|
|
setUseShippingAsBilling,
|
2021-07-22 11:03:00 +00:00
|
|
|
} = useCheckoutAddress();
|
|
|
|
const { dispatchCheckoutEvent } = useStoreEvents();
|
|
|
|
const { isEditor } = useEditorContext();
|
|
|
|
|
|
|
|
// Clears data if fields are hidden.
|
|
|
|
useEffect( () => {
|
|
|
|
if ( ! showPhoneField ) {
|
|
|
|
setShippingPhone( '' );
|
|
|
|
}
|
|
|
|
}, [ showPhoneField, setShippingPhone ] );
|
|
|
|
|
|
|
|
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>
|
|
|
|
<AddressForm
|
|
|
|
id="shipping"
|
|
|
|
type="shipping"
|
2022-02-22 17:45:01 +00:00
|
|
|
onChange={ ( values: Partial< ShippingAddress > ) => {
|
|
|
|
setShippingAddress( values );
|
|
|
|
if ( useShippingAsBilling ) {
|
|
|
|
setBillingData( values );
|
|
|
|
}
|
2021-07-22 11:03:00 +00:00
|
|
|
dispatchCheckoutEvent( 'set-shipping-address' );
|
|
|
|
} }
|
2022-02-22 17:45:01 +00:00
|
|
|
values={ shippingAddress }
|
|
|
|
fields={
|
|
|
|
Object.keys(
|
|
|
|
defaultAddressFields
|
|
|
|
) as ( keyof AddressFields )[]
|
|
|
|
}
|
2021-07-22 11:03:00 +00:00
|
|
|
fieldConfig={ addressFieldsConfig }
|
|
|
|
/>
|
|
|
|
{ showPhoneField && (
|
|
|
|
<PhoneNumber
|
|
|
|
id="shipping-phone"
|
|
|
|
isRequired={ requirePhoneField }
|
2022-02-22 17:45:01 +00:00
|
|
|
value={ shippingAddress.phone }
|
2021-07-22 11:03:00 +00:00
|
|
|
onChange={ ( value ) => {
|
|
|
|
setShippingPhone( value );
|
|
|
|
dispatchCheckoutEvent( 'set-phone-number', {
|
|
|
|
step: 'shipping',
|
|
|
|
} );
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</AddressFormWrapperComponent>
|
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__use-address-for-billing"
|
|
|
|
label={ __(
|
|
|
|
'Use same address for billing',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2022-02-22 17:45:01 +00:00
|
|
|
checked={ useShippingAsBilling }
|
|
|
|
onChange={ ( checked: boolean ) => {
|
|
|
|
setUseShippingAsBilling( checked );
|
|
|
|
if ( checked ) {
|
|
|
|
setBillingData( shippingAddress as BillingAddress );
|
|
|
|
}
|
|
|
|
} }
|
2021-07-22 11:03:00 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Block;
|