Skip to content
Merged
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
26 changes: 10 additions & 16 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

let currentUserAccountID = -1;
let currentEmail = '';
Onyx.connect({

Check warning on line 66 in src/libs/actions/User.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
currentUserAccountID = value?.accountID ?? CONST.DEFAULT_NUMBER_ID;
Expand All @@ -71,20 +71,8 @@
},
});

let myPersonalDetails: OnyxEntry<OnyxPersonalDetails>;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
if (!value || currentUserAccountID === -1) {
return;
}

myPersonalDetails = value[currentUserAccountID] ?? undefined;
},
});

let allPolicies: OnyxCollection<Policy>;
Onyx.connect({

Check warning on line 75 in src/libs/actions/User.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
Expand Down Expand Up @@ -481,6 +469,7 @@
* Validates a secondary login / contact method
*/
function validateSecondaryLogin(
currentUserPersonalDetails: OnyxEntry<OnyxPersonalDetails>,
loginList: OnyxEntry<LoginList>,
contactMethod: string,
validateCode: string,
Expand Down Expand Up @@ -561,7 +550,7 @@
value: {
[currentUserAccountID]: {
login: contactMethod,
displayName: PersonalDetailsUtils.createDisplayName(contactMethod, myPersonalDetails, formatPhoneNumber),
displayName: PersonalDetailsUtils.createDisplayName(contactMethod, currentUserPersonalDetails, formatPhoneNumber),
},
},
},
Expand Down Expand Up @@ -1103,7 +1092,12 @@
/**
* 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<OnyxPersonalDetails>,
newDefaultContactMethod: string,
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
backTo?: string,
) {
const oldDefaultContactMethod = currentEmail;
const optimisticData: Array<
OnyxUpdate<typeof ONYXKEYS.ACCOUNT | typeof ONYXKEYS.SESSION | typeof ONYXKEYS.LOGIN_LIST | typeof ONYXKEYS.PERSONAL_DETAILS_LIST | typeof ONYXKEYS.COLLECTION.POLICY>
Expand Down Expand Up @@ -1142,7 +1136,7 @@
value: {
[currentUserAccountID]: {
login: newDefaultContactMethod,
displayName: PersonalDetailsUtils.createDisplayName(newDefaultContactMethod, myPersonalDetails, formatPhoneNumber),
displayName: PersonalDetailsUtils.createDisplayName(newDefaultContactMethod, currentUserPersonalDetails, formatPhoneNumber),
},
},
},
Expand Down Expand Up @@ -1195,7 +1189,7 @@
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[currentUserAccountID]: {...myPersonalDetails},
[currentUserAccountID]: {...currentUserPersonalDetails},
},
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -331,7 +333,7 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) {
{isValidateCodeFormVisible && !!loginData && !loginData.validatedDate && (
<ValidateCodeActionForm
hasMagicCodeBeenSent={hasMagicCodeBeenSent}
handleSubmitForm={(validateCode) => 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.
Expand Down
9 changes: 5 additions & 4 deletions src/pages/settings/VerifyAccountPageBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
Expand All @@ -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(() => {
Expand Down
Loading