Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6700,6 +6700,12 @@ const CONST = {
ERROR_PERMISSION_DENIED: 'permissionDenied',
},
},
LAST_PAYMENT_METHOD: {
LAST_USED: 'lastUsed',
IOU: 'Iou',
EXPENSE: 'Expense',
INVOICE: 'Invoice',
},
SKIPPABLE_COLLECTION_MEMBER_IDS: [String(DEFAULT_NUMBER_ID), '-1', 'undefined', 'null', 'NaN'] as string[],
SETUP_SPECIALIST_LOGIN: 'Setup Specialist',
} as const;
Expand Down
18 changes: 13 additions & 5 deletions src/components/Search/SearchPageHeader/SearchPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
approveMoneyRequestOnSearch,
deleteMoneyRequestOnSearch,
exportSearchItemsToCSV,
getLastPolicyPaymentMethod,
payMoneyRequestOnSearch,
unholdMoneyRequestOnSearch,
updateAdvancedFilters,
Expand Down Expand Up @@ -149,9 +150,12 @@ function SearchPageHeader({queryJSON, searchName, searchRouterListVisible, hideS
!isOffline &&
!isAnyTransactionOnHold &&
(selectedReports.length
? selectedReports.every((report) => report.action === CONST.SEARCH.ACTION_TYPES.PAY && report.policyID && lastPaymentMethods[report.policyID])
? selectedReports.every((report) => report.action === CONST.SEARCH.ACTION_TYPES.PAY && report.policyID && getLastPolicyPaymentMethod(report.policyID, lastPaymentMethods))
: selectedTransactionsKeys.every(
(id) => selectedTransactions[id].action === CONST.SEARCH.ACTION_TYPES.PAY && selectedTransactions[id].policyID && lastPaymentMethods[selectedTransactions[id].policyID],
(id) =>
selectedTransactions[id].action === CONST.SEARCH.ACTION_TYPES.PAY &&
selectedTransactions[id].policyID &&
getLastPolicyPaymentMethod(selectedTransactions[id].policyID, lastPaymentMethods),
));

if (shouldShowPayOption) {
Expand All @@ -172,7 +176,7 @@ function SearchPageHeader({queryJSON, searchName, searchRouterListVisible, hideS

for (const item of items) {
const policyID = item.policyID;
const lastPolicyPaymentMethod = policyID ? lastPaymentMethods?.[policyID] : null;
const lastPolicyPaymentMethod = getLastPolicyPaymentMethod(policyID, lastPaymentMethods);

if (!lastPolicyPaymentMethod) {
Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID: item.reportID, backTo: activeRoute}));
Expand All @@ -189,11 +193,15 @@ function SearchPageHeader({queryJSON, searchName, searchRouterListVisible, hideS

const paymentData = (
selectedReports.length
? selectedReports.map((report) => ({reportID: report.reportID, amount: report.total, paymentType: lastPaymentMethods[`${report.policyID}`]}))
? selectedReports.map((report) => ({
reportID: report.reportID,
amount: report.total,
paymentType: getLastPolicyPaymentMethod(report.policyID, lastPaymentMethods),
}))
: Object.values(selectedTransactions).map((transaction) => ({
reportID: transaction.reportID,
amount: transaction.amount,
paymentType: lastPaymentMethods[transaction.policyID],
paymentType: getLastPolicyPaymentMethod(transaction.policyID, lastPaymentMethods),
}))
) as PaymentData[];

Expand Down
12 changes: 10 additions & 2 deletions src/components/SettlementButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {approveMoneyRequest, savePreferredPaymentMethod as savePreferredPaymentM
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {LastPaymentMethodType} from '@src/types/onyx';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';
Expand Down Expand Up @@ -74,7 +75,14 @@ function SettlementButton({
const policyEmployeeAccountIDs = policyID ? getPolicyEmployeeAccountIDs(policyID) : [];
const reportBelongsToWorkspace = policyID ? doesReportBelongToWorkspace(chatReport, policyEmployeeAccountIDs, policyID) : false;
const policyIDKey = reportBelongsToWorkspace ? policyID : CONST.POLICY.ID_FAKE;
const [lastPaymentMethod = '-1', lastPaymentMethodResult] = useOnyx(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, {selector: (paymentMethod) => paymentMethod?.[policyIDKey]});
const [lastPaymentMethod, lastPaymentMethodResult] = useOnyx(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, {
selector: (paymentMethod) => {
if (typeof paymentMethod?.[policyIDKey] === 'string') {
return paymentMethod?.[policyIDKey];
}
return (paymentMethod?.[policyIDKey] as LastPaymentMethodType)?.lastUsed;
},
});

const isLoadingLastPaymentMethod = isLoadingOnyxValue(lastPaymentMethodResult);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`);
Expand Down Expand Up @@ -221,7 +229,7 @@ function SettlementButton({
};

const savePreferredPaymentMethod = (id: string, value: PaymentMethodType) => {
savePreferredPaymentMethodIOU(id, value);
savePreferredPaymentMethodIOU(id, value, undefined);
};

return (
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9544,8 +9544,8 @@ function navigateToStartStepIfScanFileCannotBeRead(
}

/** Save the preferred payment method for a policy */
function savePreferredPaymentMethod(policyID: string, paymentMethod: PaymentMethodType) {
Onyx.merge(`${ONYXKEYS.NVP_LAST_PAYMENT_METHOD}`, {[policyID]: paymentMethod});
function savePreferredPaymentMethod(policyID: string, paymentMethod: PaymentMethodType, type: ValueOf<typeof CONST.LAST_PAYMENT_METHOD> | undefined) {
Onyx.merge(`${ONYXKEYS.NVP_LAST_PAYMENT_METHOD}`, {[policyID]: type ? {[type]: paymentMethod, [CONST.LAST_PAYMENT_METHOD.LAST_USED]: paymentMethod} : paymentMethod});
}

/** Get report policy id of IOU request */
Expand Down
19 changes: 17 additions & 2 deletions src/libs/actions/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import playSound, {SOUNDS} from '@libs/Sound';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import FILTER_KEYS from '@src/types/form/SearchAdvancedFiltersForm';
import type {LastPaymentMethod, SearchResults} from '@src/types/onyx';
import type {LastPaymentMethod, LastPaymentMethodType, SearchResults} from '@src/types/onyx';
import type {SearchPolicy, SearchReport, SearchTransaction} from '@src/types/onyx/SearchResults';
import {openReport} from './Report';

Expand Down Expand Up @@ -77,8 +77,22 @@ function handleActionButtonPress(hash: number, item: TransactionListItemType | R
}
}

function getLastPolicyPaymentMethod(policyID: string | undefined, lastPaymentMethods: OnyxEntry<LastPaymentMethod>) {
if (!policyID) {
return null;
}
let lastPolicyPaymentMethod = null;
if (typeof lastPaymentMethods?.[policyID] === 'string') {
lastPolicyPaymentMethod = lastPaymentMethods?.[policyID] as ValueOf<typeof CONST.IOU.PAYMENT_TYPE>;
} else {
lastPolicyPaymentMethod = (lastPaymentMethods?.[policyID] as LastPaymentMethodType)?.lastUsed as ValueOf<typeof CONST.IOU.PAYMENT_TYPE>;
}

return lastPolicyPaymentMethod;
}

function getPayActionCallback(hash: number, item: TransactionListItemType | ReportListItemType, goToItem: () => void) {
const lastPolicyPaymentMethod = item.policyID ? (lastPaymentMethod?.[item.policyID] as ValueOf<typeof CONST.IOU.PAYMENT_TYPE>) : null;
const lastPolicyPaymentMethod = getLastPolicyPaymentMethod(item.policyID, lastPaymentMethod);

if (!lastPolicyPaymentMethod) {
goToItem();
Expand Down Expand Up @@ -421,4 +435,5 @@ export {
handleActionButtonPress,
submitMoneyRequestOnSearch,
openSearchFiltersCardPage,
getLastPolicyPaymentMethod,
};
18 changes: 16 additions & 2 deletions src/types/onyx/LastPaymentMethod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
/**
* The new lastPaymentMethod object
*/
type LastPaymentMethodType = {
/** The default last payment method */
lastUsed: string;
/** The lastPaymentMethod of an IOU */
Iou: string;
/** The lastPaymentMethod of an Expense */
Expense: string;
/** The lastPaymentMethod of an Invoice */
Invoice: string;
};

/** Record of last payment methods, indexed by policy id */
type LastPaymentMethod = Record<string, string>;
type LastPaymentMethod = Record<string, LastPaymentMethodType | string>;

export default LastPaymentMethod;
export type {LastPaymentMethodType, LastPaymentMethod};
3 changes: 2 additions & 1 deletion src/types/onyx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import type InvitedEmailsToAccountIDs from './InvitedEmailsToAccountIDs';
import type IOU from './IOU';
import type JoinablePolicies from './JoinablePolicies';
import type LastExportMethod from './LastExportMethod';
import type LastPaymentMethod from './LastPaymentMethod';
import type {LastPaymentMethod, LastPaymentMethodType} from './LastPaymentMethod';
import type LastSelectedDistanceRates from './LastSelectedDistanceRates';
import type Locale from './Locale';
import type {LoginList} from './Login';
Expand Down Expand Up @@ -250,4 +250,5 @@ export type {
JoinablePolicies,
DismissedProductTraining,
TravelProvisioning,
LastPaymentMethodType,
};