Skip to content

Commit 5f87f8d

Browse files
authored
Merge pull request Expensify#67698 from callstack-internal/bugfix/onyx-connect-removal-login-utils-part-1
[NO QA] [part 1] add new function for Onyx.connect migration
2 parents 20f78ad + ed0663f commit 5f87f8d

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

src/libs/LoginUtils.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {clearSignInData, setAccountError} from './actions/Session';
88
import Navigation from './Navigation/Navigation';
99
import {parsePhoneNumber} from './PhoneNumber';
1010

11-
let countryCodeByIP: number;
11+
let countryCodeByIPOnyx: number;
1212
Onyx.connect({
1313
key: ONYXKEYS.COUNTRY_CODE,
14-
callback: (val) => (countryCodeByIP = val ?? 1),
14+
callback: (val) => (countryCodeByIPOnyx = val ?? 1),
1515
});
1616

1717
/**
@@ -28,7 +28,38 @@ function appendCountryCode(phone: string): string {
2828
if (phone.startsWith('+')) {
2929
return phone;
3030
}
31-
const phoneWithCountryCode = `+${countryCodeByIP}${phone}`;
31+
const phoneWithCountryCode = `+${countryCodeByIPOnyx}${phone}`;
32+
if (parsePhoneNumber(phoneWithCountryCode).possible) {
33+
return phoneWithCountryCode;
34+
}
35+
return `+${phone}`;
36+
}
37+
38+
/**
39+
* MIGRATION STEP 1: Temporary function for transitioning away from Onyx.connect to useOnyx
40+
*
41+
* This function serves as a bridge during our migration from Onyx.connect to useOnyx hooks.
42+
* The main appendCountryCode() function currently uses countryCodeByIPOnyx (via Onyx.connect),
43+
* but UI components need to use useOnyx hooks for better React integration.
44+
*
45+
* Migration plan:
46+
* 1. Add this function with explicit countryCode parameter (current step)
47+
* 2. Update UI components to use useOnyx(ONYXKEYS.COUNTRY_CODE)
48+
* 3. Update UI components to call this function with explicit countryCode
49+
* 4. Remove Onyx.connect from main appendCountryCode function
50+
* 5. Remove this temporary function and update all calls to use main function
51+
*
52+
* @param phone - Phone number to append country code to
53+
* @param countryCode - Country code (e.g., "1" for US, "44" for UK)
54+
* @returns Phone number with country code appended
55+
*
56+
* TODO: Remove this function after completing Onyx.connect deprecation (issue #66329)
57+
*/
58+
function appendCountryCodeWithCountryCode(phone: string, countryCode: string): string {
59+
if (phone.startsWith('+')) {
60+
return phone;
61+
}
62+
const phoneWithCountryCode = `+${countryCode}${phone}`;
3263
if (parsePhoneNumber(phoneWithCountryCode).possible) {
3364
return phoneWithCountryCode;
3465
}
@@ -111,6 +142,7 @@ function formatE164PhoneNumber(phoneNumber: string) {
111142
export {
112143
getPhoneNumberWithoutSpecialChars,
113144
appendCountryCode,
145+
appendCountryCodeWithCountryCode,
114146
isEmailPublicDomain,
115147
validateNumber,
116148
getPhoneLogin,

0 commit comments

Comments
 (0)