2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
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';
|
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 {
|
2023-10-09 11:49:09 +00:00
|
|
|
ShippingAddress,
|
2022-02-22 17:45:01 +00:00
|
|
|
AddressField,
|
|
|
|
AddressFields,
|
|
|
|
} from '@woocommerce/settings';
|
2022-12-19 15:30:13 +00:00
|
|
|
import { StoreNoticesContainer } from '@woocommerce/blocks-checkout';
|
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,
|
2023-10-09 11:49:09 +00:00
|
|
|
forceEditing = false,
|
2021-07-22 11:03:00 +00:00
|
|
|
}: {
|
|
|
|
showCompanyField: boolean;
|
|
|
|
showApartmentField: boolean;
|
|
|
|
showPhoneField: boolean;
|
|
|
|
requireCompanyField: boolean;
|
|
|
|
requirePhoneField: boolean;
|
2023-10-09 11:49:09 +00:00
|
|
|
forceEditing?: boolean;
|
2021-07-22 11:03:00 +00:00
|
|
|
} ): JSX.Element => {
|
2023-10-09 11:49:09 +00:00
|
|
|
const { billingAddress, setShippingAddress, useBillingAsShipping } =
|
|
|
|
useCheckoutAddress();
|
2021-07-22 11:03:00 +00:00
|
|
|
const { isEditor } = useEditorContext();
|
2022-12-02 11:34:27 +00:00
|
|
|
|
|
|
|
// Syncs shipping address with billing address if "Force shipping to the customer billing address" is enabled.
|
2023-10-09 11:49:09 +00:00
|
|
|
useEffectOnce( () => {
|
2022-12-23 15:05:56 +00:00
|
|
|
if ( useBillingAsShipping ) {
|
2023-10-09 11:49:09 +00:00
|
|
|
const { email, ...addressValues } = billingAddress;
|
|
|
|
const syncValues: Partial< ShippingAddress > = {
|
|
|
|
...addressValues,
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( ! showPhoneField ) {
|
|
|
|
delete syncValues.phone;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( showCompanyField ) {
|
|
|
|
delete syncValues.company;
|
|
|
|
}
|
|
|
|
|
|
|
|
setShippingAddress( syncValues );
|
2022-12-02 11:34:27 +00:00
|
|
|
}
|
2023-10-09 11:49:09 +00:00
|
|
|
} );
|
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
|
|
|
|
2023-10-09 11:49:09 +00:00
|
|
|
const WrapperComponent = isEditor ? Noninteractive : Fragment;
|
2023-02-09 13:41:18 +00:00
|
|
|
const noticeContext = useBillingAsShipping
|
|
|
|
? [ noticeContexts.BILLING_ADDRESS, noticeContexts.SHIPPING_ADDRESS ]
|
|
|
|
: [ noticeContexts.BILLING_ADDRESS ];
|
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' ),
|
|
|
|
};
|
|
|
|
} );
|
2021-07-22 11:03:00 +00:00
|
|
|
return (
|
2023-10-09 11:49:09 +00:00
|
|
|
<>
|
2023-02-09 13:41:18 +00:00
|
|
|
<StoreNoticesContainer context={ noticeContext } />
|
2023-10-09 11:49:09 +00:00
|
|
|
<WrapperComponent>
|
2023-11-09 16:25:28 +00:00
|
|
|
{ cartDataLoaded ? (
|
|
|
|
<CustomerAddress
|
|
|
|
addressFieldsConfig={ addressFieldsConfig }
|
|
|
|
showPhoneField={ showPhoneField }
|
|
|
|
requirePhoneField={ requirePhoneField }
|
|
|
|
forceEditing={ forceEditing }
|
|
|
|
/>
|
|
|
|
) : null }
|
2023-10-09 11:49:09 +00:00
|
|
|
</WrapperComponent>
|
|
|
|
</>
|
2021-07-22 11:03:00 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Block;
|