Skip to content

Commit

Permalink
migrate to use new private_lastPaymentMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
hungvu193 committed Feb 1, 2025
1 parent 75bf54e commit c655ff0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6549,6 +6549,12 @@ const CONST = {
ERROR_PERMISSION_DENIED: 'permissionDenied',
},
},
LAST_PAYMENT_METHOD: {
DEFAULT: 'DEFAULT',
IOU: 'IOU',
EXPENSE: 'EXPENSE',
INVOICE: 'INVOICE',
},
} as const;

type Country = keyof typeof CONST.ALL_COUNTRIES;
Expand Down
10 changes: 9 additions & 1 deletion src/components/SettlementButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as IOU from '@userActions/IOU';
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 @@ -70,7 +71,14 @@ function SettlementButton({
const policyEmployeeAccountIDs = policyID ? getPolicyEmployeeAccountIDs(policyID) : [];
const reportBelongsToWorkspace = policyID ? ReportUtils.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)?.DEFAULT;
},
});

const isLoadingLastPaymentMethod = isLoadingOnyxValue(lastPaymentMethodResult);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`);
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 @@ -9230,8 +9230,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} : paymentMethod});
}

/** Get report policy id of IOU request */
Expand Down
10 changes: 8 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 @@ -78,7 +78,13 @@ function handleActionButtonPress(hash: number, item: TransactionListItemType | R
}

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

if (!lastPolicyPaymentMethod) {
goToItem();
Expand Down
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, this one hold the existing data of the old lastPaymentMethod value which is string */
DEFAULT: 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,
};

0 comments on commit c655ff0

Please sign in to comment.