diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 56affbab9de6..5e0b83f8521d 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -36,7 +36,7 @@ import Visibility from '@libs/Visibility'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; -import type {BlockedFromConcierge, CustomStatusDraft, Policy} from '@src/types/onyx'; +import type {BlockedFromConcierge, CustomStatusDraft, LoginList, Policy} from '@src/types/onyx'; import type Login from '@src/types/onyx/Login'; import type {OnyxServerUpdate} from '@src/types/onyx/OnyxUpdatesFromServer'; import type OnyxPersonalDetails from '@src/types/onyx/PersonalDetails'; @@ -70,6 +70,13 @@ Onyx.connect({ }, }); +let allPolicies: OnyxCollection; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY, + waitForCollectionCallback: true, + callback: (value) => (allPolicies = value), +}); + /** * Attempt to close the user's accountt */ @@ -365,7 +372,7 @@ function validateLogin(accountID: number, validateCode: string) { /** * Validates a secondary login / contact method */ -function validateSecondaryLogin(contactMethod: string, validateCode: string) { +function validateSecondaryLogin(loginList: OnyxEntry, contactMethod: string, validateCode: string) { const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -420,6 +427,70 @@ function validateSecondaryLogin(contactMethod: string, validateCode: string) { }, }, ]; + // If the primary login isn't validated yet, set the secondary login as the primary login + if (!loginList?.[currentEmail].validatedDate) { + successData.push( + ...[ + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.ACCOUNT, + value: { + primaryLogin: contactMethod, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.SESSION, + value: { + email: contactMethod, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: { + [currentUserAccountID]: { + login: contactMethod, + displayName: PersonalDetailsUtils.createDisplayName(contactMethod, myPersonalDetails), + }, + }, + }, + ], + ); + + Object.values(allPolicies ?? {}).forEach((policy) => { + if (!policy) { + return; + } + + let optimisticPolicyDataValue; + + if (policy.employeeList) { + const currentEmployee = policy.employeeList[currentEmail]; + optimisticPolicyDataValue = { + employeeList: { + [currentEmail]: null, + [contactMethod]: currentEmployee, + }, + }; + } + + if (policy.ownerAccountID === currentUserAccountID) { + optimisticPolicyDataValue = { + ...optimisticPolicyDataValue, + owner: contactMethod, + }; + } + + if (optimisticPolicyDataValue) { + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, + value: optimisticPolicyDataValue, + }); + } + }); + } const failureData: OnyxUpdate[] = [ { @@ -765,7 +836,7 @@ function generateStatementPDF(period: string) { /** * Sets a contact method / secondary login as the user's "Default" contact method. */ -function setContactMethodAsDefault(newDefaultContactMethod: string, policies: OnyxCollection>) { +function setContactMethodAsDefault(newDefaultContactMethod: string) { const oldDefaultContactMethod = currentEmail; const optimisticData: OnyxUpdate[] = [ { @@ -858,7 +929,7 @@ function setContactMethodAsDefault(newDefaultContactMethod: string, policies: On }, ]; - Object.values(policies ?? {}).forEach((policy) => { + Object.values(allPolicies ?? {}).forEach((policy) => { if (!policy) { return; } diff --git a/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx b/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx index 9a02c0fef67e..af0cd242e301 100644 --- a/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx +++ b/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx @@ -2,7 +2,6 @@ import type {StackScreenProps} from '@react-navigation/stack'; import {Str} from 'expensify-common'; import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react'; import {InteractionManager, Keyboard, View} from 'react-native'; -import type {OnyxEntry} from 'react-native-onyx'; import {useOnyx} from 'react-native-onyx'; import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView'; import ConfirmModal from '@components/ConfirmModal'; @@ -28,19 +27,11 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; -import type {Policy} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; import ValidateCodeForm from './ValidateCodeForm'; import type {ValidateCodeFormHandle} from './ValidateCodeForm/BaseValidateCodeForm'; -const policiesSelector = (policy: OnyxEntry): Pick => ({ - id: policy?.id ?? '-1', - ownerAccountID: policy?.ownerAccountID, - owner: policy?.owner ?? '', - employeeList: policy?.employeeList, -}); - type ContactMethodDetailsPageProps = StackScreenProps; function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) { @@ -49,9 +40,8 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) { const [myDomainSecurityGroups, myDomainSecurityGroupsResult] = useOnyx(ONYXKEYS.MY_DOMAIN_SECURITY_GROUPS); const [securityGroups, securityGroupsResult] = useOnyx(ONYXKEYS.COLLECTION.SECURITY_GROUP); const [isLoadingReportData, isLoadingReportDataResult] = useOnyx(ONYXKEYS.IS_LOADING_REPORT_DATA, {initialValue: true}); - const [policies, policiesResult] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: policiesSelector}); - const isLoadingOnyxValues = isLoadingOnyxValue(loginListResult, sessionResult, myDomainSecurityGroupsResult, securityGroupsResult, isLoadingReportDataResult, policiesResult); + const isLoadingOnyxValues = isLoadingOnyxValue(loginListResult, sessionResult, myDomainSecurityGroupsResult, securityGroupsResult, isLoadingReportDataResult); const {formatPhoneNumber, translate} = useLocalize(); const theme = useTheme(); @@ -88,8 +78,8 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) { * Attempt to set this contact method as user's "Default contact method" */ const setAsDefault = useCallback(() => { - User.setContactMethodAsDefault(contactMethod, policies); - }, [contactMethod, policies]); + User.setContactMethodAsDefault(contactMethod); + }, [contactMethod]); /** * Checks if the user is allowed to change their default contact method. This should only be allowed if: diff --git a/src/pages/settings/Profile/Contacts/ValidateCodeForm/BaseValidateCodeForm.tsx b/src/pages/settings/Profile/Contacts/ValidateCodeForm/BaseValidateCodeForm.tsx index 556d341d049f..e0b7a23d5df3 100644 --- a/src/pages/settings/Profile/Contacts/ValidateCodeForm/BaseValidateCodeForm.tsx +++ b/src/pages/settings/Profile/Contacts/ValidateCodeForm/BaseValidateCodeForm.tsx @@ -166,8 +166,8 @@ function BaseValidateCodeForm({account = {}, contactMethod, hasMagicCodeBeenSent } setFormError({}); - User.validateSecondaryLogin(contactMethod, validateCode); - }, [validateCode, contactMethod]); + User.validateSecondaryLogin(loginList, contactMethod, validateCode); + }, [loginList, validateCode, contactMethod]); return ( <>