Skip to content

Commit 53f0ae3

Browse files
authored
Merge pull request Expensify#66059 from getusha/fix-prevent-snapshot-use-in-settlement-button
fix: dont use snapshot data in search page in settlement button
2 parents c79fc56 + 2b1a605 commit 53f0ae3

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

src/components/SettlementButton/index.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {LockedAccountContext} from '@components/LockedAccountModalProvider';
1212
import useLocalize from '@hooks/useLocalize';
1313
import useNetwork from '@hooks/useNetwork';
1414
import useOnyx from '@hooks/useOnyx';
15+
import usePolicy from '@hooks/usePolicy';
1516
import useThemeStyles from '@hooks/useThemeStyles';
1617
import {isCurrencySupportedForDirectReimbursement} from '@libs/actions/Policy/Policy';
1718
import {getCurrentUserAccountID} from '@libs/actions/Report';
@@ -88,6 +89,7 @@ function SettlementButton({
8889
const styles = useThemeStyles();
8990
const {translate} = useLocalize();
9091
const {isOffline} = useNetwork();
92+
9193
// The app would crash due to subscribing to the entire report collection if chatReportID is an empty string. So we should have a fallback ID here.
9294
// eslint-disable-next-line rulesdir/no-default-id-values
9395
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID || CONST.DEFAULT_NUMBER_ID}`, {canBeMissing: true});
@@ -107,14 +109,18 @@ function SettlementButton({
107109
const [fundList = {}] = useOnyx(ONYXKEYS.FUND_LIST, {canBeMissing: true});
108110
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
109111
const currentUserAccountID = getCurrentUserAccountID().toString();
110-
const activeAdminPolicies = getActiveAdminWorkspaces(policies, currentUserAccountID).sort((a, b) => (a.name || '').localeCompare(b.name || ''));
112+
113+
// Passing `undefined` to ensure we use the Onyx connection from PolicyUtils,
114+
// since `useOnyx` only returns snapshot data on the SearchPage.
115+
// By passing `undefined`, we fall back to the function's default value.
116+
const activeAdminPolicies = getActiveAdminWorkspaces(undefined, currentUserAccountID).sort((a, b) => (a.name || '').localeCompare(b.name || ''));
111117
const reportID = iouReport?.reportID;
112118

113119
const hasPreferredPaymentMethod = !!lastPaymentMethod;
114120
const isLoadingLastPaymentMethod = isLoadingOnyxValue(lastPaymentMethodResult);
115121
const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
116-
const isLastPaymentPolicy = !Object.values({...CONST.PAYMENT_METHODS, ...CONST.IOU.PAYMENT_TYPE}).includes(lastPaymentMethod as PaymentMethod);
117-
const lastPaymentPolicy = isLastPaymentPolicy ? policies?.[`${ONYXKEYS.COLLECTION.POLICY}${lastPaymentMethod}`] : undefined;
122+
const lastPaymentPolicy = usePolicy(lastPaymentMethod);
123+
118124
const [bankAccountList = {}] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {canBeMissing: true});
119125
const bankAccount = bankAccountList[lastBankAccountID ?? CONST.DEFAULT_NUMBER_ID];
120126
const isExpenseReport = isExpenseReportUtil(iouReport);

src/libs/PolicyUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,8 @@ function getPolicy(policyID: string | undefined, policies: OnyxCollection<Policy
755755
}
756756

757757
/** Return active policies where current user is an admin */
758-
function getActiveAdminWorkspaces(policies: OnyxCollection<Policy> | null, currentUserLogin: string | undefined): Policy[] {
759-
const activePolicies = getActivePolicies(policies, currentUserLogin);
758+
function getActiveAdminWorkspaces(policies: OnyxCollection<Policy> | null | undefined, currentUserLogin: string | undefined): Policy[] {
759+
const activePolicies = getActivePolicies(policies === undefined ? allPolicies : policies, currentUserLogin);
760760
return activePolicies.filter((policy) => shouldShowPolicy(policy, isOfflineNetworkStore(), currentUserLogin) && isPolicyAdmin(policy, currentUserLogin));
761761
}
762762

0 commit comments

Comments
 (0)