Skip to content

Commit 3327db4

Browse files
authored
Merge pull request Expensify#68825 from callstack-internal/66333-part-2-refactor-getSearchValueForPhoneOrEmail-to-use-new-appendCountryCode
[Part 2 of Migrating LoginUtils from Onyx.connect] migrate getSearchValueForEmailOrPhone to usage on the new method for appendCountryCode
2 parents 701d2cf + af832c8 commit 3327db4

13 files changed

Lines changed: 85 additions & 36 deletions

File tree

src/components/WorkspaceMembersSelectionList.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {useMemo} from 'react';
22
import type {SectionListData} from 'react-native';
33
import useDebouncedState from '@hooks/useDebouncedState';
44
import useLocalize from '@hooks/useLocalize';
5+
import useOnyx from '@hooks/useOnyx';
56
import usePolicy from '@hooks/usePolicy';
67
import useScreenWrapperTransitionStatus from '@hooks/useScreenWrapperTransitionStatus';
78
import {canUseTouchScreen} from '@libs/DeviceCapabilities';
@@ -10,6 +11,7 @@ import {getMemberAccountIDsForWorkspace} from '@libs/PolicyUtils';
1011
import tokenizedSearch from '@libs/tokenizedSearch';
1112
import MemberRightIcon from '@pages/workspace/MemberRightIcon';
1213
import CONST from '@src/CONST';
14+
import ONYXKEYS from '@src/ONYXKEYS';
1315
import type {Icon} from '@src/types/onyx/OnyxCommon';
1416
import {FallbackAvatar} from './Icon/Expensicons';
1517
import {usePersonalDetails} from './OnyxListItemProvider';
@@ -40,6 +42,7 @@ function WorkspaceMembersSelectionList({policyID, selectedApprover, setApprover}
4042
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState('');
4143
const personalDetails = usePersonalDetails();
4244
const policy = usePolicy(policyID);
45+
const [countryCode] = useOnyx(ONYXKEYS.COUNTRY_CODE, {canBeMissing: false});
4346

4447
const sections: ApproverSection[] = useMemo(() => {
4548
const approvers: SelectionListApprover[] = [];
@@ -78,7 +81,7 @@ function WorkspaceMembersSelectionList({policyID, selectedApprover, setApprover}
7881
approvers.push(...availableApprovers);
7982
}
8083

81-
const filteredApprovers = tokenizedSearch(approvers, getSearchValueForPhoneOrEmail(debouncedSearchTerm), (approver) => [approver.text ?? '', approver.login ?? '']);
84+
const filteredApprovers = tokenizedSearch(approvers, getSearchValueForPhoneOrEmail(debouncedSearchTerm, countryCode), (approver) => [approver.text ?? '', approver.login ?? '']);
8285

8386
return [
8487
{
@@ -87,7 +90,7 @@ function WorkspaceMembersSelectionList({policyID, selectedApprover, setApprover}
8790
shouldShow: true,
8891
},
8992
];
90-
}, [debouncedSearchTerm, personalDetails, policy?.employeeList, policy?.owner, selectedApprover, localeCompare]);
93+
}, [policy?.employeeList, policy?.owner, debouncedSearchTerm, countryCode, localeCompare, personalDetails, selectedApprover]);
9194

9295
const handleOnSelectRow = (approver: SelectionListApprover) => {
9396
setApprover(approver.login);

src/libs/LoginUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function appendCountryCode(phone: string): string {
5555
*
5656
* TODO: Remove this function after completing Onyx.connect deprecation (issue #66329)
5757
*/
58-
function appendCountryCodeWithCountryCode(phone: string, countryCode: string): string {
58+
function appendCountryCodeWithCountryCode(phone: string, countryCode: number): string {
5959
if (phone.startsWith('+')) {
6060
return phone;
6161
}

src/libs/OptionsListUtils/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import filterArrayByMatch from '@libs/filterArrayByMatch';
1313
import {isReportMessageAttachment} from '@libs/isReportMessageAttachment';
1414
import {formatPhoneNumber} from '@libs/LocalePhoneNumber';
1515
import {translateLocal} from '@libs/Localize';
16-
import {appendCountryCode, getPhoneNumberWithoutSpecialChars} from '@libs/LoginUtils';
16+
import {appendCountryCode, appendCountryCodeWithCountryCode, getPhoneNumberWithoutSpecialChars} from '@libs/LoginUtils';
1717
import {MaxHeap} from '@libs/MaxHeap';
1818
import {MinHeap} from '@libs/MinHeap';
1919
import ModifiedExpenseMessage from '@libs/ModifiedExpenseMessage';
@@ -1021,8 +1021,8 @@ function isMakingLastRequiredTagListOptional(policy: Policy | undefined, policyT
10211021
return false;
10221022
}
10231023

1024-
function getSearchValueForPhoneOrEmail(searchTerm: string) {
1025-
const parsedPhoneNumber = parsePhoneNumber(appendCountryCode(Str.removeSMSDomain(searchTerm)));
1024+
function getSearchValueForPhoneOrEmail(searchTerm: string, countryCode: OnyxEntry<number>) {
1025+
const parsedPhoneNumber = parsePhoneNumber(appendCountryCodeWithCountryCode(Str.removeSMSDomain(searchTerm), countryCode ?? 1));
10261026
return parsedPhoneNumber.possible ? (parsedPhoneNumber.number?.e164 ?? '') : searchTerm.toLowerCase();
10271027
}
10281028

src/pages/InviteReportParticipantsPage.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Sections = Array<SectionListData<MemberForList, Section<MemberForList>>>;
5050

5151
function InviteReportParticipantsPage({betas, report, didScreenTransitionEnd}: InviteReportParticipantsPageProps) {
5252
const route = useRoute<PlatformStackRouteProp<ParticipantsNavigatorParamList, typeof SCREENS.REPORT_PARTICIPANTS.INVITE>>();
53+
const [countryCode] = useOnyx(ONYXKEYS.COUNTRY_CODE, {canBeMissing: false});
5354
const {options, areOptionsInitialized} = useOptionsList({
5455
shouldInitialize: didScreenTransitionEnd,
5556
});
@@ -117,7 +118,7 @@ function InviteReportParticipantsPage({betas, report, didScreenTransitionEnd}: I
117118
// Filter all options that is a part of the search term or in the personal details
118119
let filterSelectedOptions = selectedOptions;
119120
if (debouncedSearchTerm !== '') {
120-
const processedSearchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm);
121+
const processedSearchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm, countryCode);
121122
filterSelectedOptions = tokenizedSearch(selectedOptions, processedSearchValue, (option) => [option.text ?? '', option.login ?? '']).filter((option) => {
122123
const accountID = option?.accountID;
123124
const isOptionInPersonalDetails = inviteOptions.personalDetails.some((personalDetail) => accountID && personalDetail?.accountID === accountID);
@@ -158,7 +159,7 @@ function InviteReportParticipantsPage({betas, report, didScreenTransitionEnd}: I
158159
}
159160

160161
return sectionsArr;
161-
}, [areOptionsInitialized, selectedOptions, debouncedSearchTerm, inviteOptions.recentReports, inviteOptions.personalDetails, inviteOptions.userToInvite, translate]);
162+
}, [areOptionsInitialized, selectedOptions, debouncedSearchTerm, inviteOptions.recentReports, inviteOptions.personalDetails, inviteOptions.userToInvite, translate, countryCode]);
162163

163164
const toggleOption = useCallback(
164165
(option: MemberForList) => {

src/pages/OnboardingWorkspaceInvite/BaseOnboardingWorkspaceInvite.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function BaseOnboardingWorkspaceInvite({shouldUseNativeStyles}: BaseOnboardingWo
5858
const [didScreenTransitionEnd, setDidScreenTransitionEnd] = useState(false);
5959
const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {canBeMissing: true, initWithStoredValues: false});
6060
const [betas] = useOnyx(ONYXKEYS.BETAS, {canBeMissing: false});
61+
const [countryCode] = useOnyx(ONYXKEYS.COUNTRY_CODE, {canBeMissing: false});
6162
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
6263
const session = useSession();
6364
const {isBetaEnabled} = usePermissions();
@@ -164,7 +165,7 @@ function BaseOnboardingWorkspaceInvite({shouldUseNativeStyles}: BaseOnboardingWo
164165
const accountID = option.accountID;
165166
const isOptionInPersonalDetails = Object.values(personalDetails).some((personalDetail) => personalDetail.accountID === accountID);
166167

167-
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm);
168+
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm, countryCode);
168169

169170
const isPartOfSearchTerm = !!option.text?.toLowerCase().includes(searchValue) || !!option.login?.toLowerCase().includes(searchValue);
170171
return isPartOfSearchTerm || isOptionInPersonalDetails;
@@ -202,7 +203,7 @@ function BaseOnboardingWorkspaceInvite({shouldUseNativeStyles}: BaseOnboardingWo
202203
});
203204

204205
return sectionsArr;
205-
}, [areOptionsInitialized, selectedOptions, debouncedSearchTerm, personalDetails, translate, usersToInvite]);
206+
}, [areOptionsInitialized, selectedOptions, debouncedSearchTerm, personalDetails, translate, usersToInvite, countryCode]);
206207

207208
const toggleOption = (option: MemberForList) => {
208209
const isOptionInList = selectedOptions.some((selectedOption) => selectedOption.login === option.login);

src/pages/workspace/WorkspaceInvitePage.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ function WorkspaceInvitePage({route, policy}: WorkspaceInvitePageProps) {
5454
const [personalDetails, setPersonalDetails] = useState<OptionData[]>([]);
5555
const [usersToInvite, setUsersToInvite] = useState<OptionData[]>([]);
5656
const [didScreenTransitionEnd, setDidScreenTransitionEnd] = useState(false);
57-
const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false});
57+
const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false, canBeMissing: true});
58+
const [countryCode] = useOnyx(ONYXKEYS.COUNTRY_CODE, {canBeMissing: false});
5859
const firstRenderRef = useRef(true);
59-
const [betas] = useOnyx(ONYXKEYS.BETAS);
60-
const [invitedEmailsToAccountIDsDraft] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_INVITE_MEMBERS_DRAFT}${route.params.policyID.toString()}`);
60+
const [betas] = useOnyx(ONYXKEYS.BETAS, {canBeMissing: false});
61+
const [invitedEmailsToAccountIDsDraft] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_INVITE_MEMBERS_DRAFT}${route.params.policyID.toString()}`, {canBeMissing: true});
6162

6263
const openWorkspaceInvitePage = () => {
6364
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(policy?.employeeList);
@@ -177,7 +178,7 @@ function WorkspaceInvitePage({route, policy}: WorkspaceInvitePageProps) {
177178
const accountID = option.accountID;
178179
const isOptionInPersonalDetails = Object.values(personalDetails).some((personalDetail) => personalDetail.accountID === accountID);
179180

180-
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm);
181+
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm, countryCode);
181182

182183
const isPartOfSearchTerm = !!option.text?.toLowerCase().includes(searchValue) || !!option.login?.toLowerCase().includes(searchValue);
183184
return isPartOfSearchTerm || isOptionInPersonalDetails;
@@ -214,7 +215,7 @@ function WorkspaceInvitePage({route, policy}: WorkspaceInvitePageProps) {
214215
});
215216

216217
return sectionsArr;
217-
}, [areOptionsInitialized, selectedOptions, debouncedSearchTerm, personalDetails, translate, usersToInvite]);
218+
}, [areOptionsInitialized, selectedOptions, debouncedSearchTerm, personalDetails, translate, usersToInvite, countryCode]);
218219

219220
const toggleOption = (option: MemberForList) => {
220221
clearErrors(route.params.policyID);

src/pages/workspace/companyCards/assignCard/AssigneeStep.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function AssigneeStep({policy, feed}: AssigneeStepProps) {
4343
const {isOffline} = useNetwork();
4444
const [assignCard] = useOnyx(ONYXKEYS.ASSIGN_CARD, {canBeMissing: true});
4545
const [workspaceCardFeeds] = useOnyx(ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST, {canBeMissing: false});
46+
const [countryCode] = useOnyx(ONYXKEYS.COUNTRY_CODE, {canBeMissing: false});
4647
const [list] = useCardsList(policy?.id, feed);
4748
const [cardFeeds] = useCardFeeds(policy?.id);
4849
const filteredCardList = getFilteredCardList(list, cardFeeds?.settings?.oAuthAccountDetails?.[feed], workspaceCardFeeds);
@@ -153,7 +154,7 @@ function AssigneeStep({policy, feed}: AssigneeStepProps) {
153154
];
154155
}
155156

156-
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm).toLowerCase();
157+
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm, countryCode).toLowerCase();
157158
const filteredOptions = tokenizedSearch(membersDetails, searchValue, (option) => [option.text ?? '', option.alternateText ?? '']);
158159

159160
return [
@@ -163,7 +164,7 @@ function AssigneeStep({policy, feed}: AssigneeStepProps) {
163164
shouldShow: true,
164165
},
165166
];
166-
}, [membersDetails, debouncedSearchTerm]);
167+
}, [membersDetails, debouncedSearchTerm, countryCode]);
167168

168169
const headerMessage = useMemo(() => {
169170
const searchValue = debouncedSearchTerm.trim().toLowerCase();

src/pages/workspace/expensifyCard/issueNew/AssigneeStep.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function AssigneeStep({policy, stepNames, startStepIndex}: AssigneeStepProps) {
4242
const {isOffline} = useNetwork();
4343
const policyID = policy?.id;
4444
const [issueNewCard] = useOnyx(`${ONYXKEYS.COLLECTION.ISSUE_NEW_EXPENSIFY_CARD}${policyID}`, {canBeMissing: true});
45+
const [countryCode] = useOnyx(ONYXKEYS.COUNTRY_CODE, {canBeMissing: false});
4546
const currency = useCurrencyForExpensifyCard({policyID});
4647

4748
const isEditing = issueNewCard?.isEditing;
@@ -123,7 +124,7 @@ function AssigneeStep({policy, stepNames, startStepIndex}: AssigneeStepProps) {
123124
];
124125
}
125126

126-
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm).toLowerCase();
127+
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm, countryCode).toLowerCase();
127128
const filteredOptions = tokenizedSearch(membersDetails, searchValue, (option) => [option.text ?? '', option.alternateText ?? '']);
128129

129130
return [
@@ -133,7 +134,7 @@ function AssigneeStep({policy, stepNames, startStepIndex}: AssigneeStepProps) {
133134
shouldShow: true,
134135
},
135136
];
136-
}, [membersDetails, debouncedSearchTerm]);
137+
}, [debouncedSearchTerm, countryCode, membersDetails]);
137138

138139
const headerMessage = useMemo(() => {
139140
const searchValue = debouncedSearchTerm.trim().toLowerCase();

src/pages/workspace/receiptPartners/InviteReceiptPartnerPolicyPage.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Text from '@components/Text';
1010
import useDebouncedState from '@hooks/useDebouncedState';
1111
import useLocalize from '@hooks/useLocalize';
1212
import useNetwork from '@hooks/useNetwork';
13+
import useOnyx from '@hooks/useOnyx';
1314
import usePolicy from '@hooks/usePolicy';
1415
import useThemeStyles from '@hooks/useThemeStyles';
1516
import {clearErrors, inviteWorkspaceEmployeesToUber} from '@libs/actions/Policy/Policy';
@@ -24,6 +25,7 @@ import tokenizedSearch from '@libs/tokenizedSearch';
2425
import type {WorkspaceSplitNavigatorParamList} from '@navigation/types';
2526
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
2627
import CONST from '@src/CONST';
28+
import ONYXKEYS from '@src/ONYXKEYS';
2729
import type SCREENS from '@src/SCREENS';
2830

2931
type InviteReceiptPartnerPolicyPageProps = PlatformStackScreenProps<WorkspaceSplitNavigatorParamList, typeof SCREENS.WORKSPACE.RECEIPT_PARTNERS_INVITE>;
@@ -35,6 +37,7 @@ function InviteReceiptPartnerPolicyPage({route}: InviteReceiptPartnerPolicyPageP
3537
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState('');
3638
const [selectedOptions, setSelectedOptions] = useState<MemberForList[]>([]);
3739
const [isInvitationSent, setIsInvitationSent] = useState(false);
40+
const [countryCode] = useOnyx(ONYXKEYS.COUNTRY_CODE, {canBeMissing: false});
3841

3942
const policyID = route.params?.policyID;
4043
const policy = usePolicy(policyID);
@@ -90,14 +93,14 @@ function InviteReceiptPartnerPolicyPage({route}: InviteReceiptPartnerPolicyPageP
9093

9194
// Apply search filter if there's a search term
9295
if (debouncedSearchTerm) {
93-
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm).toLowerCase();
96+
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm, countryCode).toLowerCase();
9497
membersToDisplay = tokenizedSearch(workspaceMembers, searchValue, (option) => [option.text ?? '', option.alternateText ?? '']);
9598
}
9699

97100
// Filter to show selected members first, then apply search filter to selected members
98101
let filterSelectedOptions = selectedOptions;
99102
if (debouncedSearchTerm !== '') {
100-
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm).toLowerCase();
103+
const searchValue = getSearchValueForPhoneOrEmail(debouncedSearchTerm, countryCode).toLowerCase();
101104
filterSelectedOptions = selectedOptions.filter((option) => {
102105
const isPartOfSearchTerm = !!option.text?.toLowerCase().includes(searchValue) || !!option.login?.toLowerCase().includes(searchValue);
103106
return isPartOfSearchTerm;

src/pages/workspace/workflows/WorkspaceWorkflowsPayerPage.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {ListItem, Section} from '@components/SelectionList/types';
1111
import UserListItem from '@components/SelectionList/UserListItem';
1212
import useLocalize from '@hooks/useLocalize';
1313
import useNetwork from '@hooks/useNetwork';
14+
import useOnyx from '@hooks/useOnyx';
1415
import Log from '@libs/Log';
1516
import Navigation from '@libs/Navigation/Navigation';
1617
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
@@ -24,6 +25,7 @@ import withPolicyAndFullscreenLoading from '@pages/workspace/withPolicyAndFullsc
2425
import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPolicyAndFullscreenLoading';
2526
import {setWorkspacePayer} from '@userActions/Policy/Policy';
2627
import CONST from '@src/CONST';
28+
import ONYXKEYS from '@src/ONYXKEYS';
2729
import type SCREENS from '@src/SCREENS';
2830
import type {PersonalDetailsList, PolicyEmployee} from '@src/types/onyx';
2931
import {isEmptyObject} from '@src/types/utils/EmptyObject';
@@ -43,6 +45,7 @@ function WorkspaceWorkflowsPayerPage({route, policy, personalDetails, isLoadingR
4345
const {translate, formatPhoneNumber} = useLocalize();
4446
const policyName = policy?.name ?? '';
4547
const {isOffline} = useNetwork();
48+
const [countryCode] = useOnyx(ONYXKEYS.COUNTRY_CODE, {canBeMissing: false});
4649

4750
const [searchTerm, setSearchTerm] = useState('');
4851

@@ -110,7 +113,7 @@ function WorkspaceWorkflowsPayerPage({route, policy, personalDetails, isLoadingR
110113
const sectionsArray: MembersSection[] = [];
111114

112115
if (searchTerm !== '') {
113-
const searchValue = getSearchValueForPhoneOrEmail(searchTerm);
116+
const searchValue = getSearchValueForPhoneOrEmail(searchTerm, countryCode);
114117
const filteredOptions = tokenizedSearch([...formattedPolicyAdmins, ...formattedAuthorizedPayer], searchValue, (option) => [option.text ?? '', option.login ?? '']);
115118

116119
return [
@@ -133,7 +136,7 @@ function WorkspaceWorkflowsPayerPage({route, policy, personalDetails, isLoadingR
133136
shouldShow: true,
134137
});
135138
return sectionsArray;
136-
}, [formattedPolicyAdmins, formattedAuthorizedPayer, translate, searchTerm]);
139+
}, [searchTerm, formattedAuthorizedPayer, translate, formattedPolicyAdmins, countryCode]);
137140

138141
const headerMessage = useMemo(
139142
() => (searchTerm && !sections.at(0)?.data.length ? translate('common.noResultsFound') : ''),

0 commit comments

Comments
 (0)