11import Onyx from 'react-native-onyx' ;
2- import type { OnyxCollection , OnyxEntry , OnyxUpdate } from 'react-native-onyx' ;
2+ import type { OnyxEntry , OnyxUpdate } from 'react-native-onyx' ;
33import type { ValueOf } from 'type-fest' ;
44import type { FormOnyxValues } from '@components/Form/types' ;
55import type { PaymentData , SearchQueryJSON } from '@components/Search/types' ;
@@ -22,33 +22,19 @@ import CONST from '@src/CONST';
2222import ONYXKEYS from '@src/ONYXKEYS' ;
2323import { FILTER_KEYS } from '@src/types/form/SearchAdvancedFiltersForm' ;
2424import type { SearchAdvancedFiltersForm } from '@src/types/form/SearchAdvancedFiltersForm' ;
25- import type { LastPaymentMethod , LastPaymentMethodType , Policy , SearchResults } from '@src/types/onyx' ;
25+ import type { LastPaymentMethod , LastPaymentMethodType , Policy } from '@src/types/onyx' ;
2626import type { ConnectionName } from '@src/types/onyx/Policy' ;
2727import type { SearchPolicy , SearchReport , SearchTransaction } from '@src/types/onyx/SearchResults' ;
2828import type Nullable from '@src/types/utils/Nullable' ;
2929
30- let lastPaymentMethod : OnyxEntry < LastPaymentMethod > ;
31- Onyx . connect ( {
32- key : ONYXKEYS . NVP_LAST_PAYMENT_METHOD ,
33- callback : ( val ) => {
34- lastPaymentMethod = val ;
35- } ,
36- } ) ;
37-
38- let allSnapshots : OnyxCollection < SearchResults > ;
39- Onyx . connect ( {
40- key : ONYXKEYS . COLLECTION . SNAPSHOT ,
41- callback : ( val ) => {
42- allSnapshots = val ;
43- } ,
44- waitForCollectionCallback : true ,
45- } ) ;
46-
4730function handleActionButtonPress (
4831 hash : number ,
4932 item : TransactionListItemType | TransactionReportGroupListItemType ,
5033 goToItem : ( ) => void ,
5134 isInMobileSelectionMode : boolean ,
35+ snapshotReport : SearchReport ,
36+ snapshotPolicy : SearchPolicy ,
37+ lastPaymentMethod : OnyxEntry < LastPaymentMethod > ,
5238 currentSearchKey ?: SearchKey ,
5339) {
5440 // The transactionIDList is needed to handle actions taken on `status:""` where transactions on single expense reports can be approved/paid.
@@ -64,22 +50,21 @@ function handleActionButtonPress(
6450
6551 switch ( item . action ) {
6652 case CONST . SEARCH . ACTION_TYPES . PAY :
67- getPayActionCallback ( hash , item , goToItem , currentSearchKey ) ;
53+ getPayActionCallback ( hash , item , goToItem , snapshotReport , snapshotPolicy , lastPaymentMethod , currentSearchKey ) ;
6854 return ;
6955 case CONST . SEARCH . ACTION_TYPES . APPROVE :
7056 approveMoneyRequestOnSearch ( hash , [ item . reportID ] , transactionID , currentSearchKey ) ;
7157 return ;
7258 case CONST . SEARCH . ACTION_TYPES . SUBMIT : {
73- const policy = ( allSnapshots ?. [ `${ ONYXKEYS . COLLECTION . SNAPSHOT } ${ hash } ` ] ?. data ?. [ `${ ONYXKEYS . COLLECTION . POLICY } ${ item . policyID } ` ] ?? { } ) as SearchPolicy ;
74- submitMoneyRequestOnSearch ( hash , [ item ] , [ policy ] , transactionID , currentSearchKey ) ;
59+ submitMoneyRequestOnSearch ( hash , [ item ] , [ snapshotPolicy ] , transactionID , currentSearchKey ) ;
7560 return ;
7661 }
7762 case CONST . SEARCH . ACTION_TYPES . EXPORT_TO_ACCOUNTING : {
7863 if ( ! item ) {
7964 return ;
8065 }
8166
82- const policy = ( allSnapshots ?. [ ` ${ ONYXKEYS . COLLECTION . SNAPSHOT } ${ hash } ` ] ?. data ?. [ ` ${ ONYXKEYS . COLLECTION . POLICY } ${ item . policyID } ` ] ?? { } ) as Policy ;
67+ const policy = ( snapshotPolicy ?? { } ) as Policy ;
8368 const connectedIntegration = getValidConnectedIntegration ( policy ) ;
8469
8570 if ( ! connectedIntegration ) {
@@ -108,24 +93,31 @@ function getLastPolicyPaymentMethod(policyID: string | undefined, lastPaymentMet
10893 return lastPolicyPaymentMethod ;
10994}
11095
111- function getPayActionCallback ( hash : number , item : TransactionListItemType | TransactionReportGroupListItemType , goToItem : ( ) => void , currentSearchKey ?: SearchKey ) {
96+ function getPayActionCallback (
97+ hash : number ,
98+ item : TransactionListItemType | TransactionReportGroupListItemType ,
99+ goToItem : ( ) => void ,
100+ snapshotReport : SearchReport ,
101+ snapshotPolicy : SearchPolicy ,
102+ lastPaymentMethod : OnyxEntry < LastPaymentMethod > ,
103+ currentSearchKey ?: SearchKey ,
104+ ) {
112105 const lastPolicyPaymentMethod = getLastPolicyPaymentMethod ( item . policyID , lastPaymentMethod ) ;
113106
114107 if ( ! lastPolicyPaymentMethod ) {
115108 goToItem ( ) ;
116109 return ;
117110 }
118111
119- const report = ( allSnapshots ?. [ `${ ONYXKEYS . COLLECTION . SNAPSHOT } ${ hash } ` ] ?. data ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ item . reportID } ` ] ?? { } ) as SearchReport ;
120- const amount = Math . abs ( ( report ?. total ?? 0 ) - ( report ?. nonReimbursableTotal ?? 0 ) ) ;
112+ const amount = Math . abs ( ( snapshotReport ?. total ?? 0 ) - ( snapshotReport ?. nonReimbursableTotal ?? 0 ) ) ;
121113 const transactionID = isTransactionListItemType ( item ) ? [ item . transactionID ] : undefined ;
122114
123115 if ( lastPolicyPaymentMethod === CONST . IOU . PAYMENT_TYPE . ELSEWHERE ) {
124116 payMoneyRequestOnSearch ( hash , [ { reportID : item . reportID , amount, paymentType : lastPolicyPaymentMethod } ] , transactionID , currentSearchKey ) ;
125117 return ;
126118 }
127119
128- const hasVBBA = ! ! allSnapshots ?. [ ` ${ ONYXKEYS . COLLECTION . SNAPSHOT } ${ hash } ` ] ?. data ?. [ ` ${ ONYXKEYS . COLLECTION . POLICY } ${ item . policyID } ` ] ?. achAccount ?. bankAccountID ;
120+ const hasVBBA = ! ! snapshotPolicy ?. achAccount ?. bankAccountID ;
129121 if ( hasVBBA ) {
130122 payMoneyRequestOnSearch ( hash , [ { reportID : item . reportID , amount, paymentType : lastPolicyPaymentMethod } ] , transactionID , currentSearchKey ) ;
131123 return ;
0 commit comments