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-13 23:09:51 +02:00
|
|
|
import useCustomerData from './useCustomerData';
|
2024-09-12 13:53:35 +02:00
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
/**
|
|
|
|
* Custom hook to handle the 'Choose a different shipping address' selection.
|
|
|
|
*
|
|
|
|
* @param {Object} fastlaneSdk - The Fastlane SDK instance.
|
|
|
|
* @param {Function} setShippingAddress - Function to update the shipping address state.
|
|
|
|
* @return {Function} Callback function to trigger shipping address selection and update.
|
|
|
|
*/
|
2024-09-13 23:09:51 +02:00
|
|
|
export const useShippingAddressChange = ( fastlaneSdk, setShippingAddress ) => {
|
2024-09-13 22:30:04 +02:00
|
|
|
const { setShippingAddressEditing } = useAddressEditing();
|
2024-09-13 23:09:51 +02:00
|
|
|
const { setShippingAddress: setWooShippingAddress } = useCustomerData();
|
2024-09-13 22:30:04 +02:00
|
|
|
|
2024-09-12 13:53:35 +02:00
|
|
|
return useCallback( async () => {
|
|
|
|
if ( fastlaneSdk ) {
|
2024-10-03 14:09:12 +02:00
|
|
|
// Show shipping address selector and get the user's selection
|
2024-09-12 13:53:35 +02:00
|
|
|
const { selectionChanged, selectedAddress } =
|
|
|
|
await fastlaneSdk.profile.showShippingAddressSelector();
|
2024-10-03 14:09:12 +02:00
|
|
|
|
2024-09-12 13:53:35 +02:00
|
|
|
if ( selectionChanged ) {
|
2024-10-03 14:09:12 +02:00
|
|
|
// Update the shipping address in the custom store with the selected address
|
2024-09-12 13:53:35 +02:00
|
|
|
setShippingAddress( selectedAddress );
|
|
|
|
|
|
|
|
const { address, name, phoneNumber } = selectedAddress;
|
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
// Transform the selected address into WooCommerce format
|
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,
|
2025-07-16 12:36:53 +02:00
|
|
|
state: address.adminArea1 || '',
|
2024-09-12 13:53:35 +02:00
|
|
|
postcode: address.postalCode,
|
|
|
|
country: address.countryCode,
|
|
|
|
phone: phoneNumber.nationalNumber,
|
2024-09-13 22:30:04 +02:00
|
|
|
};
|
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
// Update the WooCommerce shipping address in the WooCommerce store
|
2024-09-13 22:30:04 +02:00
|
|
|
await new Promise( ( resolve ) => {
|
|
|
|
setWooShippingAddress( newShippingAddress );
|
|
|
|
resolve();
|
|
|
|
} );
|
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
// Trigger the Address Card view by setting the shipping address editing state to false
|
2024-09-13 22:30:04 +02:00
|
|
|
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;
|