/** * External dependencies */ import { useCallback, useEffect } from '@wordpress/element'; import { Form } from '@woocommerce/base-components/cart-checkout'; import { useCheckoutAddress, useStoreEvents } from '@woocommerce/base-context'; import type { FormFieldsConfig, AddressFormValues, } from '@woocommerce/settings'; import { useSelect } from '@wordpress/data'; import { VALIDATION_STORE_KEY } from '@woocommerce/block-data'; import { ADDRESS_FORM_KEYS } from '@woocommerce/block-settings'; /** * Internal dependencies */ import AddressWrapper from '../../address-wrapper'; import AddressCard from '../../address-card'; const CustomerAddress = ( { addressFieldsConfig, }: { addressFieldsConfig: FormFieldsConfig; } ) => { const { shippingAddress, setShippingAddress, setBillingAddress, useShippingAsBilling, editingShippingAddress: editing, setEditingShippingAddress: setEditing, } = useCheckoutAddress(); const { dispatchCheckoutEvent } = useStoreEvents(); // Forces editing state if store has errors. const { hasValidationErrors, invalidProps } = useSelect( ( select ) => { const store = select( VALIDATION_STORE_KEY ); return { hasValidationErrors: store.hasValidationErrors(), invalidProps: Object.keys( shippingAddress ) .filter( ( key ) => { return ( store.getValidationError( 'shipping_' + key ) !== undefined ); } ) .filter( Boolean ), }; } ); useEffect( () => { if ( invalidProps.length > 0 && editing === false ) { setEditing( true ); } }, [ editing, hasValidationErrors, invalidProps.length, setEditing ] ); const onChangeAddress = useCallback( ( values: AddressFormValues ) => { setShippingAddress( values ); if ( useShippingAsBilling ) { setBillingAddress( values ); dispatchCheckoutEvent( 'set-billing-address' ); } dispatchCheckoutEvent( 'set-shipping-address' ); }, [ dispatchCheckoutEvent, setBillingAddress, setShippingAddress, useShippingAsBilling, ] ); const renderAddressCardComponent = useCallback( () => ( { setEditing( true ); } } fieldConfig={ addressFieldsConfig } isExpanded={ editing } /> ), [ shippingAddress, addressFieldsConfig, editing, setEditing ] ); const renderAddressFormComponent = useCallback( () => ( id="shipping" addressType="shipping" onChange={ onChangeAddress } values={ shippingAddress } fields={ ADDRESS_FORM_KEYS } fieldConfig={ addressFieldsConfig } isEditing={ editing } /> ), [ addressFieldsConfig, onChangeAddress, shippingAddress, editing ] ); return ( ); }; export default CustomerAddress;