2023-10-09 11:49:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-10-10 21:07:58 +00:00
|
|
|
import { useState, useCallback, useEffect } from '@wordpress/element';
|
2024-01-05 17:35:28 +00:00
|
|
|
import { Form } from '@woocommerce/base-components/cart-checkout';
|
2023-11-15 15:22:36 +00:00
|
|
|
import { useCheckoutAddress, useStoreEvents } from '@woocommerce/base-context';
|
2023-10-09 11:49:09 +00:00
|
|
|
import type {
|
2024-01-05 17:35:28 +00:00
|
|
|
FormFieldsConfig,
|
|
|
|
AddressFormValues,
|
2023-10-09 11:49:09 +00:00
|
|
|
} from '@woocommerce/settings';
|
2023-10-10 21:07:58 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
|
2024-01-05 17:35:28 +00:00
|
|
|
import { ADDRESS_FORM_KEYS } from '@woocommerce/block-settings';
|
2023-10-09 11:49:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import AddressWrapper from '../../address-wrapper';
|
|
|
|
import AddressCard from '../../address-card';
|
|
|
|
|
|
|
|
const CustomerAddress = ( {
|
|
|
|
addressFieldsConfig,
|
2023-11-15 15:22:36 +00:00
|
|
|
defaultEditing = false,
|
2023-10-09 11:49:09 +00:00
|
|
|
}: {
|
2024-01-05 17:35:28 +00:00
|
|
|
addressFieldsConfig: FormFieldsConfig;
|
2023-11-15 15:22:36 +00:00
|
|
|
defaultEditing?: boolean;
|
2023-10-09 11:49:09 +00:00
|
|
|
} ) => {
|
|
|
|
const {
|
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
|
|
|
setBillingAddress,
|
|
|
|
useShippingAsBilling,
|
|
|
|
} = useCheckoutAddress();
|
|
|
|
const { dispatchCheckoutEvent } = useStoreEvents();
|
2023-11-15 15:22:36 +00:00
|
|
|
const [ editing, setEditing ] = useState( defaultEditing );
|
2023-10-10 21:07:58 +00:00
|
|
|
|
|
|
|
// 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 ] );
|
|
|
|
|
2023-10-09 11:49:09 +00:00
|
|
|
const onChangeAddress = useCallback(
|
2024-01-05 17:35:28 +00:00
|
|
|
( values: AddressFormValues ) => {
|
2023-10-09 11:49:09 +00:00
|
|
|
setShippingAddress( values );
|
|
|
|
if ( useShippingAsBilling ) {
|
2023-11-14 18:30:23 +00:00
|
|
|
setBillingAddress( values );
|
2023-10-09 11:49:09 +00:00
|
|
|
dispatchCheckoutEvent( 'set-billing-address' );
|
|
|
|
}
|
|
|
|
dispatchCheckoutEvent( 'set-shipping-address' );
|
|
|
|
},
|
|
|
|
[
|
|
|
|
dispatchCheckoutEvent,
|
|
|
|
setBillingAddress,
|
|
|
|
setShippingAddress,
|
|
|
|
useShippingAsBilling,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
const renderAddressCardComponent = useCallback(
|
|
|
|
() => (
|
|
|
|
<AddressCard
|
|
|
|
address={ shippingAddress }
|
|
|
|
target="shipping"
|
|
|
|
onEdit={ () => {
|
|
|
|
setEditing( true );
|
|
|
|
} }
|
2023-11-14 18:30:23 +00:00
|
|
|
fieldConfig={ addressFieldsConfig }
|
2023-10-09 11:49:09 +00:00
|
|
|
/>
|
|
|
|
),
|
2023-11-14 18:30:23 +00:00
|
|
|
[ shippingAddress, addressFieldsConfig ]
|
2023-10-09 11:49:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const renderAddressFormComponent = useCallback(
|
|
|
|
() => (
|
2024-01-05 17:35:28 +00:00
|
|
|
<Form< AddressFormValues >
|
2023-11-14 18:30:23 +00:00
|
|
|
id="shipping"
|
2024-01-05 17:35:28 +00:00
|
|
|
addressType="shipping"
|
2023-11-14 18:30:23 +00:00
|
|
|
onChange={ onChangeAddress }
|
|
|
|
values={ shippingAddress }
|
2024-01-05 17:35:28 +00:00
|
|
|
fields={ ADDRESS_FORM_KEYS }
|
2023-11-14 18:30:23 +00:00
|
|
|
fieldConfig={ addressFieldsConfig }
|
|
|
|
/>
|
2023-10-09 11:49:09 +00:00
|
|
|
),
|
2023-12-15 12:45:38 +00:00
|
|
|
[ addressFieldsConfig, onChangeAddress, shippingAddress ]
|
2023-10-09 11:49:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AddressWrapper
|
|
|
|
isEditing={ editing }
|
|
|
|
addressCard={ renderAddressCardComponent }
|
|
|
|
addressForm={ renderAddressFormComponent }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CustomerAddress;
|