From a2857363db0e48fbc154d99b0e108fb0d234b7c4 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Thu, 11 Jul 2024 12:43:51 +0800 Subject: [PATCH 1/2] optimistically update the policy current user employee info --- src/libs/actions/User.ts | 11 ++++++++++- .../Profile/Contacts/ContactMethodDetailsPage.tsx | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 7b3b1abd04ef..8b5937363bc5 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[] = [ { @@ -862,11 +862,16 @@ function setContactMethodAsDefault(newDefaultContactMethod: string, policies: On if (policy?.ownerAccountID !== currentUserAccountID) { return; } + const currentEmployee = policy.employeeList?.[oldDefaultContactMethod] ?? {role: 'admin'}; optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, value: { owner: newDefaultContactMethod, + employeeList: { + [oldDefaultContactMethod]: null, + [newDefaultContactMethod]: currentEmployee, + }, }, }); failureData.push({ @@ -874,6 +879,10 @@ function setContactMethodAsDefault(newDefaultContactMethod: string, policies: On key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, value: { owner: policy.owner, + employeeList: { + [oldDefaultContactMethod]: currentEmployee, + [newDefaultContactMethod]: null, + }, }, }); }); 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; From bd11b4f407f4605c2db983668c58c6eb59adb955 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Thu, 11 Jul 2024 19:46:24 +0800 Subject: [PATCH 2/2] update all policy employee list too --- src/libs/actions/User.ts | 52 +++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 8b5937363bc5..3019e3dfbb6c 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -859,32 +859,52 @@ function setContactMethodAsDefault(newDefaultContactMethod: string, policies: On ]; Object.values(policies ?? {}).forEach((policy) => { - if (policy?.ownerAccountID !== currentUserAccountID) { + if (!policy) { return; } - const currentEmployee = policy.employeeList?.[oldDefaultContactMethod] ?? {role: 'admin'}; - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, - value: { - owner: newDefaultContactMethod, + + let optimisticPolicyDataValue; + let failurePolicyDataValue; + + if (policy.employeeList) { + const currentEmployee = policy.employeeList[oldDefaultContactMethod]; + optimisticPolicyDataValue = { employeeList: { [oldDefaultContactMethod]: null, [newDefaultContactMethod]: currentEmployee, }, - }, - }); - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, - value: { - owner: policy.owner, + }; + failurePolicyDataValue = { employeeList: { [oldDefaultContactMethod]: currentEmployee, [newDefaultContactMethod]: null, }, - }, - }); + }; + } + + if (policy.ownerAccountID === currentUserAccountID) { + optimisticPolicyDataValue = { + ...optimisticPolicyDataValue, + owner: newDefaultContactMethod, + }; + 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,