From c655ff0a193f46b95a12ed0a701c5fd4c4835127 Mon Sep 17 00:00:00 2001 From: Hans Date: Sun, 2 Feb 2025 00:54:53 +0700 Subject: [PATCH] migrate to use new private_lastPaymentMethod --- src/CONST.ts | 6 ++++++ src/components/SettlementButton/index.tsx | 10 +++++++++- src/libs/actions/IOU.ts | 4 ++-- src/libs/actions/Search.ts | 10 ++++++++-- src/types/onyx/LastPaymentMethod.ts | 18 ++++++++++++++++-- src/types/onyx/index.ts | 3 ++- 6 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index 1907af4c6109..f20d8ac89af7 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -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; diff --git a/src/components/SettlementButton/index.tsx b/src/components/SettlementButton/index.tsx index 9c4294419c5e..7b0208e1a2f4 100644 --- a/src/components/SettlementButton/index.tsx +++ b/src/components/SettlementButton/index.tsx @@ -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'; @@ -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}`); diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0cd321851b24..57222664a326 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -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 | undefined) { + Onyx.merge(`${ONYXKEYS.NVP_LAST_PAYMENT_METHOD}`, {[policyID]: type ? {[type]: paymentMethod} : paymentMethod}); } /** Get report policy id of IOU request */ diff --git a/src/libs/actions/Search.ts b/src/libs/actions/Search.ts index 39cb9bc63e34..ff29f70c7c82 100644 --- a/src/libs/actions/Search.ts +++ b/src/libs/actions/Search.ts @@ -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'; @@ -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) : 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(); diff --git a/src/types/onyx/LastPaymentMethod.ts b/src/types/onyx/LastPaymentMethod.ts index ea0c644fc730..9b2a8dc41448 100644 --- a/src/types/onyx/LastPaymentMethod.ts +++ b/src/types/onyx/LastPaymentMethod.ts @@ -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; +type LastPaymentMethod = Record; -export default LastPaymentMethod; +export type {LastPaymentMethodType, LastPaymentMethod}; diff --git a/src/types/onyx/index.ts b/src/types/onyx/index.ts index 569504437fb2..f5a9acb5f3ea 100644 --- a/src/types/onyx/index.ts +++ b/src/types/onyx/index.ts @@ -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'; @@ -250,4 +250,5 @@ export type { JoinablePolicies, DismissedProductTraining, TravelProvisioning, + LastPaymentMethodType, };