Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync phone changes with WooPay phone field #9464

Merged
merged 12 commits into from
Sep 28, 2024
4 changes: 4 additions & 0 deletions changelog/fix-sync-woopay-phone-field
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Sync phone changes with WooPay phone field.
46 changes: 36 additions & 10 deletions client/components/woopay/save-user/checkout-page-save-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,25 @@ const CheckoutPageSaveUser = ( { isBlocksCheckout } ) => {
rememberMe.removeAttribute( 'disabled', 'disabled' );
}, [ checkoutIsProcessing, isBlocksCheckout ] );

const getPhoneFieldValue = useCallback( () => {
let phoneFieldValue = '';
const getPhoneField = useCallback( () => {
let phoneField = null;
if ( isBlocksCheckout ) {
phoneFieldValue =
document.getElementById( 'phone' )?.value ||
document.getElementById( 'shipping-phone' )?.value ||
phoneField =
document.getElementById( 'phone' ) ||
document.getElementById( 'shipping-phone' ) ||
// in case of virtual products, the shipping phone is not available. So we also need to check the billing phone.
document.getElementById( 'billing-phone' )?.value ||
'';
document.getElementById( 'billing-phone' );
} else {
// for classic checkout.
phoneFieldValue =
document.getElementById( 'billing_phone' )?.value || '';
phoneField = document.getElementById( 'billing_phone' );
}

return phoneField;
}, [ isBlocksCheckout ] );

const getPhoneFieldValue = useCallback( () => {
let phoneFieldValue = getPhoneField()?.value || '';

// Take out any non-digit characters, except +.
phoneFieldValue = phoneFieldValue.replace( /[^\d+]*/g, '' );

Expand All @@ -109,7 +113,7 @@ const CheckoutPageSaveUser = ( { isBlocksCheckout } ) => {
}

return phoneFieldValue;
}, [ isBlocksCheckout ] );
}, [ getPhoneField ] );

const sendExtensionData = useCallback(
( shouldClearData = false ) => {
Expand Down Expand Up @@ -222,6 +226,28 @@ const CheckoutPageSaveUser = ( { isBlocksCheckout } ) => {
setPhoneNumber( getPhoneFieldValue() );
}, [ getPhoneFieldValue, isWCPayWithNewTokenChosen ] );

useEffect( () => {
const phoneField = getPhoneField();

if ( ! phoneField ) {
return;
}

const phoneFieldBlurHandler = () => {
// Add timeout to prevent cart update racing condition
// while updating the billing phone.
setTimeout( () => {
setPhoneNumber( getPhoneFieldValue() );
}, 0 );
};

phoneField.addEventListener( 'blur', phoneFieldBlurHandler );

return () => {
phoneField.removeEventListener( 'blur', phoneFieldBlurHandler );
};
}, [ getPhoneField, getPhoneFieldValue, isBlocksCheckout ] );

if (
! getConfig( 'forceNetworkSavedCards' ) ||
! isWCPayWithNewTokenChosen ||
Expand Down
Loading