2024-09-12 13:53:35 +02:00
|
|
|
import { useCallback } from '@wordpress/element';
|
2024-09-13 22:30:04 +02:00
|
|
|
import { useAddressEditing } from './useAddressEditing';
|
2024-09-12 13:53:35 +02:00
|
|
|
|
|
|
|
export const useShippingAddressChange = (
|
|
|
|
fastlaneSdk,
|
|
|
|
setShippingAddress,
|
|
|
|
setWooShippingAddress
|
|
|
|
) => {
|
2024-09-13 22:30:04 +02:00
|
|
|
const { setShippingAddressEditing } = useAddressEditing();
|
|
|
|
|
2024-09-12 13:53:35 +02:00
|
|
|
return useCallback( async () => {
|
|
|
|
if ( fastlaneSdk ) {
|
|
|
|
const { selectionChanged, selectedAddress } =
|
|
|
|
await fastlaneSdk.profile.showShippingAddressSelector();
|
|
|
|
if ( selectionChanged ) {
|
|
|
|
setShippingAddress( selectedAddress );
|
|
|
|
console.log(
|
|
|
|
'Selected shipping address changed:',
|
|
|
|
selectedAddress
|
|
|
|
);
|
|
|
|
|
|
|
|
const { address, name, phoneNumber } = selectedAddress;
|
|
|
|
|
2024-09-13 22:30:04 +02:00
|
|
|
const newShippingAddress = {
|
2024-09-12 13:53:35 +02:00
|
|
|
first_name: name.firstName,
|
|
|
|
last_name: name.lastName,
|
|
|
|
address_1: address.addressLine1,
|
|
|
|
address_2: address.addressLine2 || '',
|
|
|
|
city: address.adminArea2,
|
|
|
|
state: address.adminArea1,
|
|
|
|
postcode: address.postalCode,
|
|
|
|
country: address.countryCode,
|
|
|
|
phone: phoneNumber.nationalNumber,
|
2024-09-13 22:30:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
await new Promise( ( resolve ) => {
|
|
|
|
setWooShippingAddress( newShippingAddress );
|
|
|
|
resolve();
|
|
|
|
} );
|
|
|
|
|
|
|
|
await new Promise( ( resolve ) => {
|
|
|
|
setShippingAddressEditing( false );
|
|
|
|
resolve();
|
2024-09-12 13:53:35 +02:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 22:30:04 +02:00
|
|
|
}, [
|
|
|
|
fastlaneSdk,
|
|
|
|
setShippingAddress,
|
|
|
|
setWooShippingAddress,
|
|
|
|
setShippingAddressEditing,
|
|
|
|
] );
|
2024-09-12 13:53:35 +02:00
|
|
|
};
|
2024-09-13 15:27:10 +02:00
|
|
|
|
|
|
|
export default useShippingAddressChange;
|