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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
49 changes: 39 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 @@ -13,6 +13,7 @@ import {
import {
VALIDATION_STORE_KEY,
CHECKOUT_STORE_KEY,
CART_STORE_KEY,
} from '@woocommerce/block-data'; // eslint-disable-line import/no-unresolved

/**
Expand Down Expand Up @@ -47,6 +48,16 @@ const CheckoutPageSaveUser = ( { isBlocksCheckout } ) => {
select( CHECKOUT_STORE_KEY ).isProcessing()
);

const { billingAddressPhone, isCustomerDataUpdating } = useSelect(
( select ) => {
const store = select( CART_STORE_KEY );
return {
billingAddressPhone: store.getCartData()?.billingAddress?.phone,
isCustomerDataUpdating: store.isCustomerDataUpdating(),
};
}
);

const isRegisteredUser = useWooPayUser();
const { isWCPayChosen, isNewPaymentTokenChosen } = useSelectedPaymentMethod(
isBlocksCheckout
Expand Down Expand Up @@ -86,21 +97,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 +124,7 @@ const CheckoutPageSaveUser = ( { isBlocksCheckout } ) => {
}

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

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

// Update the phone number when the billing phone is updated.
useEffect( () => {
if ( isCustomerDataUpdating ) {
setTimeout( () => {
setPhoneNumber( getPhoneFieldValue() );
}, 0 );
}
}, [
billingAddressPhone,
setPhoneNumber,
getPhoneFieldValue,
isCustomerDataUpdating,
] );
Comment on lines +240 to +252
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used this approach due to inconsistencies when using the blur event and autofill.


if (
! getConfig( 'forceNetworkSavedCards' ) ||
! isWCPayWithNewTokenChosen ||
Expand Down
2 changes: 2 additions & 0 deletions client/settings/phone-input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const PhoneNumberInput = ( {
hiddenInput: 'full',
utilsScript: utils,
dropdownContainer: document.body,
formatOnDisplay: false,
...phoneCountries,
} );
setInputInstance( iti );
Expand Down Expand Up @@ -149,6 +150,7 @@ const PhoneNumberInput = ( {
inputRef.current &&
( focusLost || inputInstance.getNumber() )
) {
inputInstance.setNumber( value );
onValidationChange( inputInstance.isValidNumber() );
}
}, [ value, inputInstance, inputRef, onValidationChange, focusLost ] );
Expand Down
Loading