diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 50db3e31f1ac..e1594a56eed4 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -117,7 +117,6 @@ import type { PolicyCategories, PolicyCategory, PolicyEmployee, - ReimbursementAccount, Report, ReportAction, ReportActions, @@ -269,12 +268,6 @@ Onyx.connect({ callback: (val) => (allPersonalDetails = val), }); -let reimbursementAccount: OnyxEntry; -Onyx.connect({ - key: ONYXKEYS.REIMBURSEMENT_ACCOUNT, - callback: (val) => (reimbursementAccount = val), -}); - let allRecentlyUsedCurrencies: string[]; Onyx.connect({ key: ONYXKEYS.RECENTLY_USED_CURRENCIES, @@ -384,6 +377,7 @@ function deleteWorkspace( policyCardFeeds: CardFeeds | undefined, reportsToArchive: Report[], transactionViolations: OnyxCollection | undefined, + reimbursementAccountError: Errors | undefined, lastUsedPaymentMethods?: LastPaymentMethod, ) { if (!allPolicies) { @@ -429,7 +423,7 @@ function deleteWorkspace( onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.REIMBURSEMENT_ACCOUNT, value: { - errors: reimbursementAccount?.errors ?? null, + errors: reimbursementAccountError ?? null, }, }, { diff --git a/src/pages/workspace/WorkspaceOverviewPage.tsx b/src/pages/workspace/WorkspaceOverviewPage.tsx index bae6dddd0dd8..2f0ce3ec300e 100644 --- a/src/pages/workspace/WorkspaceOverviewPage.tsx +++ b/src/pages/workspace/WorkspaceOverviewPage.tsx @@ -53,6 +53,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; +import {reimbursementAccountErrorSelector} from '@src/selectors/ReimbursementAccount'; import type {CurrencyList} from '@src/types/onyx'; import {getEmptyObject, isEmptyObject} from '@src/types/utils/EmptyObject'; import WorkspaceReceiptPartnersPromotionBanner from './receiptPartners/WorkspaceReceiptPartnersPromotionBanner'; @@ -77,6 +78,7 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa const [fundList] = useOnyx(ONYXKEYS.FUND_LIST, {canBeMissing: true}); const [isComingFromGlobalReimbursementsFlow] = useOnyx(ONYXKEYS.IS_COMING_FROM_GLOBAL_REIMBURSEMENTS_FLOW, {canBeMissing: true}); const [lastAccessedWorkspacePolicyID] = useOnyx(ONYXKEYS.LAST_ACCESSED_WORKSPACE_POLICY_ID, {canBeMissing: true}); + const [reimbursementAccountError] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {canBeMissing: true, selector: reimbursementAccountErrorSelector}); // When we create a new workspace, the policy prop will be empty on the first render. Therefore, we have to use policyDraft until policy has been set in Onyx. const policy = policyDraft?.id ? policyDraft : policyProp; @@ -201,10 +203,10 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa return; } - deleteWorkspace(policy.id, policyName, lastAccessedWorkspacePolicyID, defaultCardFeeds, reportsToArchive, transactionViolations, lastPaymentMethod); + deleteWorkspace(policy.id, policyName, lastAccessedWorkspacePolicyID, defaultCardFeeds, reportsToArchive, transactionViolations, reimbursementAccountError, lastPaymentMethod); setIsDeleteModalOpen(false); goBackFromInvalidPolicy(); - }, [policy?.id, policyName, lastAccessedWorkspacePolicyID, defaultCardFeeds, lastPaymentMethod, reportsToArchive, transactionViolations]); + }, [policy?.id, policyName, lastAccessedWorkspacePolicyID, defaultCardFeeds, reportsToArchive, transactionViolations, reimbursementAccountError, lastPaymentMethod]); useEffect(() => { if (isLoadingBill) { diff --git a/src/pages/workspace/WorkspacesListPage.tsx b/src/pages/workspace/WorkspacesListPage.tsx index becc43b645a6..ee2c12844922 100755 --- a/src/pages/workspace/WorkspacesListPage.tsx +++ b/src/pages/workspace/WorkspacesListPage.tsx @@ -58,6 +58,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; +import {reimbursementAccountErrorSelector} from '@src/selectors/ReimbursementAccount'; import type {Policy as PolicyType} from '@src/types/onyx'; import type * as OnyxCommon from '@src/types/onyx/OnyxCommon'; import type {PolicyDetailsForNonMembers} from '@src/types/onyx/Policy'; @@ -117,6 +118,7 @@ function WorkspacesListPage() { const [fundList] = useOnyx(ONYXKEYS.FUND_LIST, {canBeMissing: true}); const [duplicateWorkspace] = useOnyx(ONYXKEYS.DUPLICATE_WORKSPACE, {canBeMissing: true}); const {isRestrictedToPreferredPolicy} = usePreferredPolicy(); + const [reimbursementAccountError] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {canBeMissing: true, selector: reimbursementAccountErrorSelector}); // This hook preloads the screens of adjacent tabs to make changing tabs faster. usePreloadFullScreenNavigators(); @@ -157,7 +159,16 @@ function WorkspacesListPage() { return; } - deleteWorkspace(policyIDToDelete, policyNameToDelete, lastAccessedWorkspacePolicyID, defaultCardFeeds, reportsToArchive, transactionViolations, lastPaymentMethod); + deleteWorkspace( + policyIDToDelete, + policyNameToDelete, + lastAccessedWorkspacePolicyID, + defaultCardFeeds, + reportsToArchive, + transactionViolations, + reimbursementAccountError, + lastPaymentMethod, + ); setIsDeleteModalOpen(false); }; diff --git a/src/selectors/ReimbursementAccount.ts b/src/selectors/ReimbursementAccount.ts new file mode 100644 index 000000000000..17f6acec34da --- /dev/null +++ b/src/selectors/ReimbursementAccount.ts @@ -0,0 +1,7 @@ +import type {OnyxEntry} from 'react-native-onyx'; +import type {ReimbursementAccount} from '@src/types/onyx'; + +const reimbursementAccountErrorSelector = (reimbursementAccount: OnyxEntry) => reimbursementAccount?.errors; + +// eslint-disable-next-line import/prefer-default-export +export {reimbursementAccountErrorSelector}; diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 58fe7f150752..fad4db8a4510 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -811,14 +811,12 @@ describe('actions/Policy', () => { chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, policyName: fakePolicy.name, }; - const fakeReimbursementAccount = {errors: {}}; await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport); - await Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, fakeReimbursementAccount); // When deleting a workspace fails mockFetch?.fail?.(); - Policy.deleteWorkspace(fakePolicy.id, fakePolicy.name, undefined, undefined, [fakeReport], undefined, undefined); + Policy.deleteWorkspace(fakePolicy.id, fakePolicy.name, undefined, undefined, [fakeReport], undefined, {}); await waitForBatchedUpdates(); @@ -898,13 +896,21 @@ describe('actions/Policy', () => { {name: 'hold', type: CONST.VIOLATION_TYPES.WARNING}, ]); - Policy.deleteWorkspace(policyID, 'test', undefined, undefined, [expenseChatReport], { - // eslint-disable-next-line @typescript-eslint/naming-convention - transactionViolations_3: [ - {name: 'cashExpenseWithNoReceipt', type: CONST.VIOLATION_TYPES.VIOLATION}, - {name: 'hold', type: CONST.VIOLATION_TYPES.WARNING}, - ], - }); + Policy.deleteWorkspace( + policyID, + 'test', + undefined, + undefined, + [expenseChatReport], + { + // eslint-disable-next-line @typescript-eslint/naming-convention + transactionViolations_3: [ + {name: 'cashExpenseWithNoReceipt', type: CONST.VIOLATION_TYPES.VIOLATION}, + {name: 'hold', type: CONST.VIOLATION_TYPES.WARNING}, + ], + }, + undefined, + ); await waitForBatchedUpdates();