diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index c584490aacf9..140fc942f576 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -71,18 +71,6 @@ Onyx.connect({ }, }); -let myPersonalDetails: OnyxEntry; -Onyx.connect({ - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - callback: (value) => { - if (!value || currentUserAccountID === -1) { - return; - } - - myPersonalDetails = value[currentUserAccountID] ?? undefined; - }, -}); - let allPolicies: OnyxCollection; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY, @@ -481,6 +469,7 @@ function requestValidateCodeAction() { * Validates a secondary login / contact method */ function validateSecondaryLogin( + currentUserPersonalDetails: OnyxEntry, loginList: OnyxEntry, contactMethod: string, validateCode: string, @@ -561,7 +550,7 @@ function validateSecondaryLogin( value: { [currentUserAccountID]: { login: contactMethod, - displayName: PersonalDetailsUtils.createDisplayName(contactMethod, myPersonalDetails, formatPhoneNumber), + displayName: PersonalDetailsUtils.createDisplayName(contactMethod, currentUserPersonalDetails, formatPhoneNumber), }, }, }, @@ -1103,7 +1092,12 @@ function generateStatementPDF(period: string) { /** * Sets a contact method / secondary login as the user's "Default" contact method. */ -function setContactMethodAsDefault(newDefaultContactMethod: string, formatPhoneNumber: LocaleContextProps['formatPhoneNumber'], backTo?: string) { +function setContactMethodAsDefault( + currentUserPersonalDetails: OnyxEntry, + newDefaultContactMethod: string, + formatPhoneNumber: LocaleContextProps['formatPhoneNumber'], + backTo?: string, +) { const oldDefaultContactMethod = currentEmail; const optimisticData: Array< OnyxUpdate @@ -1142,7 +1136,7 @@ function setContactMethodAsDefault(newDefaultContactMethod: string, formatPhoneN value: { [currentUserAccountID]: { login: newDefaultContactMethod, - displayName: PersonalDetailsUtils.createDisplayName(newDefaultContactMethod, myPersonalDetails, formatPhoneNumber), + displayName: PersonalDetailsUtils.createDisplayName(newDefaultContactMethod, currentUserPersonalDetails, formatPhoneNumber), }, }, }, @@ -1195,7 +1189,7 @@ function setContactMethodAsDefault(newDefaultContactMethod: string, formatPhoneN onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.PERSONAL_DETAILS_LIST, value: { - [currentUserAccountID]: {...myPersonalDetails}, + [currentUserAccountID]: {...currentUserPersonalDetails}, }, }, ]; diff --git a/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx b/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx index 499b4a571e3e..e294f48def91 100644 --- a/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx +++ b/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx @@ -15,6 +15,7 @@ import ScrollView from '@components/ScrollView'; import Text from '@components/Text'; import ValidateCodeActionForm from '@components/ValidateCodeActionForm'; import type {ValidateCodeFormHandle} from '@components/ValidateCodeActionModal/ValidateCodeForm/BaseValidateCodeForm'; +import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; import useLocalize from '@hooks/useLocalize'; import useOnyx from '@hooks/useOnyx'; @@ -59,6 +60,7 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) { const {isActingAsDelegate, showDelegateNoAccessModal} = useContext(DelegateNoAccessContext); const isLoadingOnyxValues = isLoadingOnyxValue(loginListResult, sessionResult, myDomainSecurityGroupsResult, securityGroupsResult, isLoadingReportDataResult); const {isAccountLocked, showLockedAccountModal} = useContext(LockedAccountContext); + const currentUserPersonalDetails = useCurrentUserPersonalDetails(); const {formatPhoneNumber, translate} = useLocalize(); const themeStyles = useThemeStyles(); @@ -88,8 +90,8 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) { * Attempt to set this contact method as user's "Default contact method" */ const setAsDefault = useCallback(() => { - setContactMethodAsDefault(contactMethod, formatPhoneNumber, backTo); - }, [contactMethod, backTo, formatPhoneNumber]); + setContactMethodAsDefault(currentUserPersonalDetails, contactMethod, formatPhoneNumber, backTo); + }, [currentUserPersonalDetails, contactMethod, formatPhoneNumber, backTo]); /** * Checks if the user is allowed to change their default contact method. This should only be allowed if: @@ -331,7 +333,7 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) { {isValidateCodeFormVisible && !!loginData && !loginData.validatedDate && ( validateSecondaryLogin(loginList, contactMethod, validateCode, formatPhoneNumber)} + handleSubmitForm={(validateCode) => validateSecondaryLogin(currentUserPersonalDetails, loginList, contactMethod, validateCode, formatPhoneNumber)} validateError={!isEmptyObject(validateLoginError) ? validateLoginError : getLatestErrorField(loginData, 'validateCodeSent')} clearError={() => { // When removing unverified contact methods, the ValidateCodeActionForm unmounts and triggers clearError. diff --git a/src/pages/settings/VerifyAccountPageBase.tsx b/src/pages/settings/VerifyAccountPageBase.tsx index 6e08e343c7e2..2b9a50d89478 100644 --- a/src/pages/settings/VerifyAccountPageBase.tsx +++ b/src/pages/settings/VerifyAccountPageBase.tsx @@ -3,6 +3,7 @@ import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; import ScreenWrapper from '@components/ScreenWrapper'; import ValidateCodeActionContent from '@components/ValidateCodeActionModal/ValidateCodeActionContent'; +import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; import useLocalize from '@hooks/useLocalize'; import useOnyx from '@hooks/useOnyx'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -22,10 +23,10 @@ function VerifyAccountPageBase({navigateBackTo, navigateForwardTo, handleClose}: const styles = useThemeStyles(); const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: true}); const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST, {canBeMissing: true}); - const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: true}); + const currentUserPersonalDetails = useCurrentUserPersonalDetails(); // sometimes primaryLogin can be empty string // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - const contactMethod = (account?.primaryLogin || session?.email) ?? ''; + const contactMethod = (account?.primaryLogin || currentUserPersonalDetails.email) ?? ''; const {translate, formatPhoneNumber} = useLocalize(); const loginData = loginList?.[contactMethod]; const validateLoginError = getEarliestErrorField(loginData, 'validateLogin'); @@ -35,9 +36,9 @@ function VerifyAccountPageBase({navigateBackTo, navigateForwardTo, handleClose}: const handleSubmitForm = useCallback( (validateCode: string) => { - validateSecondaryLogin(loginList, contactMethod, validateCode, formatPhoneNumber, true); + validateSecondaryLogin(currentUserPersonalDetails, loginList, contactMethod, validateCode, formatPhoneNumber, true); }, - [loginList, contactMethod, formatPhoneNumber], + [currentUserPersonalDetails, loginList, contactMethod, formatPhoneNumber], ); const handleCloseWithFallback = useCallback(() => {