diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 7b3b1abd04ef..3019e3dfbb6c 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -765,7 +765,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, policies: OnyxCollection>) { const oldDefaultContactMethod = currentEmail; const optimisticData: OnyxUpdate[] = [ { @@ -859,23 +859,52 @@ function setContactMethodAsDefault(newDefaultContactMethod: string, policies: On ]; Object.values(policies ?? {}).forEach((policy) => { - if (policy?.ownerAccountID !== currentUserAccountID) { + if (!policy) { return; } - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, - value: { + + let optimisticPolicyDataValue; + let failurePolicyDataValue; + + if (policy.employeeList) { + const currentEmployee = policy.employeeList[oldDefaultContactMethod]; + optimisticPolicyDataValue = { + employeeList: { + [oldDefaultContactMethod]: null, + [newDefaultContactMethod]: currentEmployee, + }, + }; + failurePolicyDataValue = { + employeeList: { + [oldDefaultContactMethod]: currentEmployee, + [newDefaultContactMethod]: null, + }, + }; + } + + if (policy.ownerAccountID === currentUserAccountID) { + optimisticPolicyDataValue = { + ...optimisticPolicyDataValue, owner: newDefaultContactMethod, - }, - }); - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, - value: { + }; + failurePolicyDataValue = { + ...failurePolicyDataValue, owner: policy.owner, - }, - }); + }; + } + + if (optimisticPolicyDataValue && failurePolicyDataValue) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, + value: optimisticPolicyDataValue, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, + value: failurePolicyDataValue, + }); + } }); const parameters: SetContactMethodAsDefaultParams = { partnerUserID: newDefaultContactMethod, diff --git a/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx b/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx index 393b377869df..9a02c0fef67e 100644 --- a/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx +++ b/src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx @@ -34,10 +34,11 @@ import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; import ValidateCodeForm from './ValidateCodeForm'; import type {ValidateCodeFormHandle} from './ValidateCodeForm/BaseValidateCodeForm'; -const policiesSelector = (policy: OnyxEntry): Pick => ({ +const policiesSelector = (policy: OnyxEntry): Pick => ({ id: policy?.id ?? '-1', ownerAccountID: policy?.ownerAccountID, owner: policy?.owner ?? '', + employeeList: policy?.employeeList, }); type ContactMethodDetailsPageProps = StackScreenProps;