From fad567c1071c523ca2b432477b56731a146f0aed Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Wed, 17 Jan 2024 16:00:38 +0000 Subject: [PATCH 1/3] refactor: move methods to personalDetailsUtils, remove unused one --- src/libs/PersonalDetailsUtils.ts | 69 +++++++++++++++++ src/libs/actions/PersonalDetails.ts | 74 +------------------ src/libs/actions/User.ts | 4 +- .../EnablePayments/AdditionalDetailsStep.js | 6 +- 4 files changed, 75 insertions(+), 78 deletions(-) diff --git a/src/libs/PersonalDetailsUtils.ts b/src/libs/PersonalDetailsUtils.ts index 3346094adeec..10b966a1a688 100644 --- a/src/libs/PersonalDetailsUtils.ts +++ b/src/libs/PersonalDetailsUtils.ts @@ -1,5 +1,6 @@ import type {OnyxEntry} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; +import Str from 'expensify-common/lib/str'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {PersonalDetails, PersonalDetailsList, PrivatePersonalDetails} from '@src/types/onyx'; @@ -7,6 +8,12 @@ import * as LocalePhoneNumber from './LocalePhoneNumber'; import * as Localize from './Localize'; import * as UserUtils from './UserUtils'; + +type FirstAndLastName = { + firstName: string; + lastName: string; +}; + let personalDetails: Array = []; let allPersonalDetails: OnyxEntry = {}; Onyx.connect({ @@ -198,6 +205,66 @@ function getEffectiveDisplayName(personalDetail?: PersonalDetails): string | und return undefined; } +/** + * Creates a new displayName for a user based on passed personal details or login. + * + * @param login - user's login + * @param passedPersonalDetails - details object with firstName and lastName + * @returns - The effective display name + */ +function createDisplayName(login: string, passedPersonalDetails: Pick | OnyxEntry): string { + // If we have a number like +15857527441@expensify.sms then let's remove @expensify.sms and format it + // so that the option looks cleaner in our UI. + const userLogin = LocalePhoneNumber.formatPhoneNumber(login); + + if (!passedPersonalDetails) { + return userLogin; + } + + const firstName = passedPersonalDetails.firstName ?? ''; + const lastName = passedPersonalDetails.lastName ?? ''; + const fullName = `${firstName} ${lastName}`.trim(); + + // It's possible for fullName to be empty string, so we must use "||" to fallback to userLogin. + return fullName || userLogin; +} + +/** + * Gets the first and last name from the user's personal details. + * If the login is the same as the displayName, then they don't exist, + * so we return empty strings instead. + * + * @param login - user's login + * @param displayName - user display name + * @param firstName + * @param lastName + */ +function extractFirstAndLastNameFromAvailableDetails({login, displayName, firstName, lastName}: PersonalDetails): FirstAndLastName { + // It's possible for firstName to be empty string, so we must use "||" to consider lastName instead. + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + if (firstName || lastName) { + return {firstName: firstName ?? '', lastName: lastName ?? ''}; + } + if (login && Str.removeSMSDomain(login) === displayName) { + return {firstName: '', lastName: ''}; + } + + if (displayName) { + const firstSpaceIndex = displayName.indexOf(' '); + const lastSpaceIndex = displayName.lastIndexOf(' '); + if (firstSpaceIndex === -1) { + return {firstName: displayName, lastName: ''}; + } + + return { + firstName: displayName.substring(0, firstSpaceIndex).trim(), + lastName: displayName.substring(lastSpaceIndex).trim(), + }; + } + + return {firstName: '', lastName: ''}; +} + export { getDisplayNameOrDefault, getPersonalDetailsByIDs, @@ -208,4 +275,6 @@ export { getFormattedStreet, getStreetLines, getEffectiveDisplayName, + createDisplayName, + extractFirstAndLastNameFromAvailableDetails }; diff --git a/src/libs/actions/PersonalDetails.ts b/src/libs/actions/PersonalDetails.ts index 508cca34fb88..b09b664ff745 100644 --- a/src/libs/actions/PersonalDetails.ts +++ b/src/libs/actions/PersonalDetails.ts @@ -1,10 +1,8 @@ -import Str from 'expensify-common/lib/str'; import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import * as API from '@libs/API'; import type {CustomRNImageManipulatorResult} from '@libs/cropOrRotateImage/types'; import DateUtils from '@libs/DateUtils'; -import * as LocalePhoneNumber from '@libs/LocalePhoneNumber'; import Navigation from '@libs/Navigation/Navigation'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import * as UserUtils from '@libs/UserUtils'; @@ -15,11 +13,6 @@ import type {DateOfBirthForm, PersonalDetails, PersonalDetailsList, PrivatePerso import type {SelectedTimezone, Timezone} from '@src/types/onyx/PersonalDetails'; import * as Session from './Session'; -type FirstAndLastName = { - firstName: string; - lastName: string; -}; - let currentUserEmail = ''; let currentUserAccountID = -1; Onyx.connect({ @@ -42,68 +35,6 @@ Onyx.connect({ callback: (val) => (privatePersonalDetails = val), }); -/** - * Creates a new displayName for a user based on passed personal details or login. - */ -function createDisplayName(login: string, personalDetails: Pick | OnyxEntry): string { - // If we have a number like +15857527441@expensify.sms then let's remove @expensify.sms and format it - // so that the option looks cleaner in our UI. - const userLogin = LocalePhoneNumber.formatPhoneNumber(login); - - if (!personalDetails) { - return userLogin; - } - - const firstName = personalDetails.firstName ?? ''; - const lastName = personalDetails.lastName ?? ''; - const fullName = `${firstName} ${lastName}`.trim(); - - // It's possible for fullName to be empty string, so we must use "||" to fallback to userLogin. - return fullName || userLogin; -} - -/** - * Gets the first and last name from the user's personal details. - * If the login is the same as the displayName, then they don't exist, - * so we return empty strings instead. - */ -function extractFirstAndLastNameFromAvailableDetails({login, displayName, firstName, lastName}: PersonalDetails): FirstAndLastName { - // It's possible for firstName to be empty string, so we must use "||" to consider lastName instead. - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - if (firstName || lastName) { - return {firstName: firstName ?? '', lastName: lastName ?? ''}; - } - if (login && Str.removeSMSDomain(login) === displayName) { - return {firstName: '', lastName: ''}; - } - - if (displayName) { - const firstSpaceIndex = displayName.indexOf(' '); - const lastSpaceIndex = displayName.lastIndexOf(' '); - if (firstSpaceIndex === -1) { - return {firstName: displayName, lastName: ''}; - } - - return { - firstName: displayName.substring(0, firstSpaceIndex).trim(), - lastName: displayName.substring(lastSpaceIndex).trim(), - }; - } - - return {firstName: '', lastName: ''}; -} - -/** - * Convert country names obtained from the backend to their respective ISO codes - * This is for backward compatibility of stored data before E/App#15507 - */ -function getCountryISO(countryName: string): string { - if (!countryName || countryName.length === 2) { - return countryName; - } - - return Object.entries(CONST.ALL_COUNTRIES).find(([, value]) => value === countryName)?.[0] ?? ''; -} function updatePronouns(pronouns: string) { if (currentUserAccountID) { @@ -149,7 +80,7 @@ function updateDisplayName(firstName: string, lastName: string) { [currentUserAccountID]: { firstName, lastName, - displayName: createDisplayName(currentUserEmail ?? '', { + displayName: PersonalDetailsUtils.createDisplayName(currentUserEmail ?? '', { firstName, lastName, }), @@ -563,9 +494,6 @@ function getPrivatePersonalDetails(): OnyxEntry { export { clearAvatarErrors, deleteAvatar, - extractFirstAndLastNameFromAvailableDetails, - getCountryISO, - createDisplayName, getPrivatePersonalDetails, openPersonalDetailsPage, openPublicProfilePage, diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index df5709ac68e2..1766b1a29677 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -10,6 +10,7 @@ import * as SequentialQueue from '@libs/Network/SequentialQueue'; import * as Pusher from '@libs/Pusher/pusher'; import PusherUtils from '@libs/PusherUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; +import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; @@ -23,7 +24,6 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type {EmptyObject} from '@src/types/utils/EmptyObject'; import * as Link from './Link'; import * as OnyxUpdates from './OnyxUpdates'; -import * as PersonalDetails from './PersonalDetails'; import * as Report from './Report'; import * as Session from './Session'; @@ -716,7 +716,7 @@ function setContactMethodAsDefault(newDefaultContactMethod: string) { value: { [currentUserAccountID]: { login: newDefaultContactMethod, - displayName: PersonalDetails.createDisplayName(newDefaultContactMethod, myPersonalDetails), + displayName: PersonalDetailsUtils.createDisplayName(newDefaultContactMethod, myPersonalDetails), }, }, }, diff --git a/src/pages/EnablePayments/AdditionalDetailsStep.js b/src/pages/EnablePayments/AdditionalDetailsStep.js index 5ca51e208b49..0371a70ede4b 100644 --- a/src/pages/EnablePayments/AdditionalDetailsStep.js +++ b/src/pages/EnablePayments/AdditionalDetailsStep.js @@ -18,8 +18,8 @@ import useThemeStyles from '@hooks/useThemeStyles'; import compose from '@libs/compose'; import {parsePhoneNumber} from '@libs/PhoneNumber'; import * as ValidationUtils from '@libs/ValidationUtils'; +import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import AddressForm from '@pages/ReimbursementAccount/AddressForm'; -import * as PersonalDetails from '@userActions/PersonalDetails'; import * as Wallet from '@userActions/Wallet'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; @@ -194,7 +194,7 @@ function AdditionalDetailsStep({walletAdditionalDetails, translate, currentUserP label={translate(fieldNameTranslationKeys.legalFirstName)} accessibilityLabel={translate(fieldNameTranslationKeys.legalFirstName)} role={CONST.ROLE.PRESENTATION} - defaultValue={PersonalDetails.extractFirstAndLastNameFromAvailableDetails(currentUserPersonalDetails).firstName} + defaultValue={PersonalDetailsUtils.extractFirstAndLastNameFromAvailableDetails(currentUserPersonalDetails).firstName} shouldSaveDraft /> Date: Fri, 19 Jan 2024 10:04:55 +0000 Subject: [PATCH 2/3] fix: run prettier --- src/libs/PersonalDetailsUtils.ts | 5 ++--- src/libs/actions/PersonalDetails.ts | 1 - src/libs/actions/User.ts | 2 +- src/pages/EnablePayments/AdditionalDetailsStep.js | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/libs/PersonalDetailsUtils.ts b/src/libs/PersonalDetailsUtils.ts index 85c439c11736..945c0b581b16 100644 --- a/src/libs/PersonalDetailsUtils.ts +++ b/src/libs/PersonalDetailsUtils.ts @@ -1,6 +1,6 @@ +import Str from 'expensify-common/lib/str'; import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; -import Str from 'expensify-common/lib/str'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {PersonalDetails, PersonalDetailsList, PrivatePersonalDetails} from '@src/types/onyx'; @@ -9,7 +9,6 @@ import * as LocalePhoneNumber from './LocalePhoneNumber'; import * as Localize from './Localize'; import * as UserUtils from './UserUtils'; - type FirstAndLastName = { firstName: string; lastName: string; @@ -273,5 +272,5 @@ export { getStreetLines, getEffectiveDisplayName, createDisplayName, - extractFirstAndLastNameFromAvailableDetails + extractFirstAndLastNameFromAvailableDetails, }; diff --git a/src/libs/actions/PersonalDetails.ts b/src/libs/actions/PersonalDetails.ts index b09b664ff745..afd0dc6037e8 100644 --- a/src/libs/actions/PersonalDetails.ts +++ b/src/libs/actions/PersonalDetails.ts @@ -35,7 +35,6 @@ Onyx.connect({ callback: (val) => (privatePersonalDetails = val), }); - function updatePronouns(pronouns: string) { if (currentUserAccountID) { type UpdatePronounsParams = { diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 1766b1a29677..b304be62b407 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -7,10 +7,10 @@ import * as API from '@libs/API'; import * as ErrorUtils from '@libs/ErrorUtils'; import Navigation from '@libs/Navigation/Navigation'; import * as SequentialQueue from '@libs/Network/SequentialQueue'; +import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import * as Pusher from '@libs/Pusher/pusher'; import PusherUtils from '@libs/PusherUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; -import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; diff --git a/src/pages/EnablePayments/AdditionalDetailsStep.js b/src/pages/EnablePayments/AdditionalDetailsStep.js index 0371a70ede4b..cf769bf58fd3 100644 --- a/src/pages/EnablePayments/AdditionalDetailsStep.js +++ b/src/pages/EnablePayments/AdditionalDetailsStep.js @@ -16,9 +16,9 @@ import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsDefaultPro import withLocalize, {withLocalizePropTypes} from '@components/withLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import compose from '@libs/compose'; +import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import {parsePhoneNumber} from '@libs/PhoneNumber'; import * as ValidationUtils from '@libs/ValidationUtils'; -import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import AddressForm from '@pages/ReimbursementAccount/AddressForm'; import * as Wallet from '@userActions/Wallet'; import CONST from '@src/CONST'; From b0970b6357bd36ac23899ff00cc7e010130c86a5 Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Tue, 30 Jan 2024 09:15:14 +0000 Subject: [PATCH 3/3] fix: remove params --- src/libs/PersonalDetailsUtils.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/libs/PersonalDetailsUtils.ts b/src/libs/PersonalDetailsUtils.ts index 945c0b581b16..2391874c83f3 100644 --- a/src/libs/PersonalDetailsUtils.ts +++ b/src/libs/PersonalDetailsUtils.ts @@ -203,10 +203,6 @@ function getEffectiveDisplayName(personalDetail?: PersonalDetails): string | und /** * Creates a new displayName for a user based on passed personal details or login. - * - * @param login - user's login - * @param passedPersonalDetails - details object with firstName and lastName - * @returns - The effective display name */ function createDisplayName(login: string, passedPersonalDetails: Pick | OnyxEntry): string { // If we have a number like +15857527441@expensify.sms then let's remove @expensify.sms and format it @@ -229,11 +225,6 @@ function createDisplayName(login: string, passedPersonalDetails: Pick