2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2023-10-09 11:49:09 +00:00
|
|
|
import { useMemo, Fragment } from '@wordpress/element';
|
|
|
|
import { useEffectOnce } from 'usehooks-ts';
|
2021-07-22 11:03:00 +00:00
|
|
|
import {
|
|
|
|
useCheckoutAddress,
|
|
|
|
useEditorContext,
|
2022-12-19 15:30:13 +00:00
|
|
|
noticeContexts,
|
2021-07-22 11:03:00 +00:00
|
|
|
} from '@woocommerce/base-context';
|
2023-11-14 16:32:53 +00:00
|
|
|
import { CheckboxControl } from '@woocommerce/blocks-checkout';
|
|
|
|
import { StoreNoticesContainer } from '@woocommerce/blocks-components';
|
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';
|
2023-11-09 16:25:28 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { CART_STORE_KEY } from '@woocommerce/block-data';
|
2021-07-22 11:03:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-10-09 11:49:09 +00:00
|
|
|
import CustomerAddress from './customer-address';
|
2021-07-22 11:03:00 +00:00
|
|
|
|
|
|
|
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 {
|
2022-06-10 16:59:25 +00:00
|
|
|
setBillingAddress,
|
2022-02-22 17:45:01 +00:00
|
|
|
shippingAddress,
|
|
|
|
useShippingAsBilling,
|
|
|
|
setUseShippingAsBilling,
|
2021-07-22 11:03:00 +00:00
|
|
|
} = useCheckoutAddress();
|
|
|
|
const { isEditor } = useEditorContext();
|
|
|
|
|
2023-10-09 11:49:09 +00:00
|
|
|
// Syncs the billing address with the shipping address.
|
|
|
|
const syncBillingWithShipping = () => {
|
|
|
|
const syncValues: Partial< BillingAddress > = {
|
|
|
|
...shippingAddress,
|
|
|
|
};
|
2022-07-28 14:38:29 +00:00
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
if ( ! showPhoneField ) {
|
2023-10-09 11:49:09 +00:00
|
|
|
delete syncValues.phone;
|
2021-07-22 11:03:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 11:49:09 +00:00
|
|
|
if ( showCompanyField ) {
|
|
|
|
delete syncValues.company;
|
|
|
|
}
|
|
|
|
|
|
|
|
setBillingAddress( syncValues );
|
|
|
|
};
|
2022-07-28 14:38:29 +00:00
|
|
|
|
2023-10-09 11:49:09 +00:00
|
|
|
// Run this on first render to ensure addresses sync if needed (this is not re-ran when toggling the checkbox).
|
|
|
|
useEffectOnce( () => {
|
|
|
|
if ( useShippingAsBilling ) {
|
|
|
|
syncBillingWithShipping();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Create address fields config from block attributes.
|
2021-07-22 11:03:00 +00:00
|
|
|
const addressFieldsConfig = useMemo( () => {
|
|
|
|
return {
|
|
|
|
company: {
|
|
|
|
hidden: ! showCompanyField,
|
|
|
|
required: requireCompanyField,
|
|
|
|
},
|
|
|
|
address_2: {
|
|
|
|
hidden: ! showApartmentField,
|
|
|
|
},
|
2023-11-14 18:30:23 +00:00
|
|
|
phone: {
|
|
|
|
hidden: ! showPhoneField,
|
|
|
|
required: requirePhoneField,
|
|
|
|
},
|
2021-07-22 11:03:00 +00:00
|
|
|
};
|
2022-02-22 17:45:01 +00:00
|
|
|
}, [
|
|
|
|
showCompanyField,
|
|
|
|
requireCompanyField,
|
|
|
|
showApartmentField,
|
2023-11-14 18:30:23 +00:00
|
|
|
showPhoneField,
|
|
|
|
requirePhoneField,
|
2022-02-22 17:45:01 +00:00
|
|
|
] ) as Record< keyof AddressFields, Partial< AddressField > >;
|
2021-07-22 11:03:00 +00:00
|
|
|
|
2023-10-09 11:49:09 +00:00
|
|
|
const WrapperComponent = isEditor ? Noninteractive : Fragment;
|
2023-02-09 13:41:18 +00:00
|
|
|
const noticeContext = useShippingAsBilling
|
|
|
|
? [ noticeContexts.SHIPPING_ADDRESS, noticeContexts.BILLING_ADDRESS ]
|
|
|
|
: [ noticeContexts.SHIPPING_ADDRESS ];
|
2023-10-09 11:49:09 +00:00
|
|
|
const hasAddress = !! (
|
|
|
|
shippingAddress.address_1 &&
|
|
|
|
( shippingAddress.first_name || shippingAddress.last_name )
|
|
|
|
);
|
2021-07-22 11:03:00 +00:00
|
|
|
|
2023-11-09 16:25:28 +00:00
|
|
|
const { cartDataLoaded } = useSelect( ( select ) => {
|
|
|
|
const store = select( CART_STORE_KEY );
|
|
|
|
return {
|
|
|
|
cartDataLoaded: store.hasFinishedResolution( 'getCartData' ),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2023-11-15 15:22:36 +00:00
|
|
|
// Default editing state for CustomerAddress component comes from the current address and whether or not we're in the editor.
|
|
|
|
const defaultEditingAddress = isEditor || ! hasAddress;
|
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-10-09 11:49:09 +00:00
|
|
|
<StoreNoticesContainer context={ noticeContext } />
|
|
|
|
<WrapperComponent>
|
2023-11-09 16:25:28 +00:00
|
|
|
{ cartDataLoaded ? (
|
|
|
|
<CustomerAddress
|
|
|
|
addressFieldsConfig={ addressFieldsConfig }
|
2023-11-15 15:22:36 +00:00
|
|
|
defaultEditing={ defaultEditingAddress }
|
2023-11-09 16:25:28 +00:00
|
|
|
/>
|
|
|
|
) : null }
|
2023-10-09 11:49:09 +00:00
|
|
|
</WrapperComponent>
|
|
|
|
{ hasAddress && (
|
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__use-address-for-billing"
|
|
|
|
label={ __(
|
|
|
|
'Use same address for billing',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ useShippingAsBilling }
|
|
|
|
onChange={ ( checked: boolean ) => {
|
|
|
|
setUseShippingAsBilling( checked );
|
|
|
|
if ( checked ) {
|
|
|
|
syncBillingWithShipping();
|
|
|
|
}
|
|
|
|
} }
|
2021-07-22 11:03:00 +00:00
|
|
|
/>
|
2023-10-09 11:49:09 +00:00
|
|
|
) }
|
2021-07-22 11:03:00 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Block;
|