1
1
import { useFocusEffect } from '@react-navigation/native' ;
2
- import lodashGet from 'lodash/get' ;
3
- import PropTypes from 'prop-types' ;
4
2
import React , { useCallback , useEffect , useRef } from 'react' ;
3
+ import type { OnyxEntry } from 'react-native-onyx' ;
5
4
import { withOnyx } from 'react-native-onyx' ;
6
- import taxPropTypes from '@components/taxPropTypes' ;
7
- import transactionPropTypes from '@components/transactionPropTypes' ;
5
+ import type { BaseTextInputRef } from '@components/TextInput/BaseTextInput/types' ;
8
6
import useLocalize from '@hooks/useLocalize' ;
9
- import compose from '@libs/compose' ;
10
7
import * as CurrencyUtils from '@libs/CurrencyUtils' ;
11
8
import Navigation from '@libs/Navigation/Navigation' ;
12
9
import * as TransactionUtils from '@libs/TransactionUtils' ;
10
+ import type { CurrentMoney } from '@pages/iou/steps/MoneyRequestAmountForm' ;
13
11
import MoneyRequestAmountForm from '@pages/iou/steps/MoneyRequestAmountForm' ;
14
- import reportPropTypes from '@pages/reportPropTypes' ;
15
12
import * as IOU from '@userActions/IOU' ;
16
13
import CONST from '@src/CONST' ;
17
14
import ONYXKEYS from '@src/ONYXKEYS' ;
18
15
import ROUTES from '@src/ROUTES' ;
19
- import IOURequestStepRoutePropTypes from './IOURequestStepRoutePropTypes' ;
16
+ import type SCREENS from '@src/SCREENS' ;
17
+ import type { Policy , Transaction } from '@src/types/onyx' ;
20
18
import StepScreenWrapper from './StepScreenWrapper' ;
21
19
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound' ;
20
+ import type { WithWritableReportOrNotFoundProps } from './withWritableReportOrNotFound' ;
22
21
import withWritableReportOrNotFound from './withWritableReportOrNotFound' ;
23
22
24
- const propTypes = {
25
- /** Navigation route context info provided by react navigation */
26
- route : IOURequestStepRoutePropTypes . isRequired ,
27
-
28
- /* Onyx Props */
29
- /** The report that the transaction belongs to */
30
- report : reportPropTypes ,
31
-
32
- /** The transaction object being modified in Onyx */
33
- transaction : transactionPropTypes ,
34
-
35
- /* Onyx Props */
36
- /** The policy of the report */
37
- policy : PropTypes . shape ( {
38
- /** Collection of tax rates attached to a policy */
39
- taxRates : taxPropTypes ,
40
- } ) ,
23
+ type IOURequestStepTaxAmountPageOnyxProps = {
24
+ policy : OnyxEntry < Policy > ;
41
25
} ;
42
26
43
- const defaultProps = {
44
- report : { } ,
45
- policy : { } ,
46
- transaction : { } ,
47
- } ;
27
+ type IOURequestStepTaxAmountPageProps = IOURequestStepTaxAmountPageOnyxProps &
28
+ WithWritableReportOrNotFoundProps < typeof SCREENS . MONEY_REQUEST . STEP_TAX_AMOUNT > & {
29
+ transaction : OnyxEntry < Transaction > ;
30
+ } ;
48
31
49
- const getTaxAmount = ( transaction , defaultTaxValue ) => {
50
- const percentage = ( transaction . taxRate ? transaction . taxRate . data . value : defaultTaxValue ) || '' ;
51
- return CurrencyUtils . convertToBackendAmount ( Number . parseFloat ( TransactionUtils . calculateTaxAmount ( percentage , transaction . amount ) ) ) ;
52
- } ;
32
+ function getTaxAmount ( transaction : OnyxEntry < Transaction > , defaultTaxValue : string | undefined ) : number | undefined {
33
+ if ( ! transaction ?. amount ) {
34
+ return ;
35
+ }
36
+ const percentage = ( transaction ?. taxRate ? transaction ?. taxRate ?. data ?. value : defaultTaxValue ) ?? '' ;
37
+ return CurrencyUtils . convertToBackendAmount ( TransactionUtils . calculateTaxAmount ( percentage , transaction ?. amount ) ) ;
38
+ }
53
39
54
40
function IOURequestStepTaxAmountPage ( {
55
41
route : {
56
42
params : { iouType, reportID, transactionID, backTo} ,
57
43
} ,
58
44
transaction,
59
- transaction : { currency} ,
60
45
report,
61
46
policy,
62
- } ) {
47
+ } : IOURequestStepTaxAmountPageProps ) {
63
48
const { translate} = useLocalize ( ) ;
64
- const textInput = useRef ( null ) ;
49
+ const textInput = useRef < BaseTextInputRef | null > ( ) ;
65
50
const isEditing = Navigation . getActiveRoute ( ) . includes ( 'taxAmount' ) ;
66
51
67
- const focusTimeoutRef = useRef ( null ) ;
52
+ const focusTimeoutRef = useRef < NodeJS . Timeout > ( ) ;
68
53
69
54
const isSaveButtonPressed = useRef ( false ) ;
70
- const originalCurrency = useRef ( null ) ;
71
- const taxRates = lodashGet ( policy , ' taxRates' , { } ) ;
55
+ const originalCurrency = useRef < string > ( ) ;
56
+ const taxRates = policy ?. taxRates ;
72
57
73
58
useEffect ( ( ) => {
74
- if ( transaction . originalCurrency ) {
59
+ if ( transaction ? .originalCurrency ) {
75
60
originalCurrency . current = transaction . originalCurrency ;
76
- } else {
77
- originalCurrency . current = currency ;
78
- IOU . setMoneyRequestOriginalCurrency_temporaryForRefactor ( transactionID , currency ) ;
61
+ } else if ( transaction ?. currency ) {
62
+ originalCurrency . current = transaction . currency ;
63
+ IOU . setMoneyRequestOriginalCurrency_temporaryForRefactor ( transactionID , transaction ?. currency ) ;
79
64
}
80
65
return ( ) => {
81
- if ( isSaveButtonPressed . current ) {
66
+ if ( isSaveButtonPressed . current || ! originalCurrency . current ) {
82
67
return ;
83
68
}
84
69
IOU . setMoneyRequestCurrency_temporaryForRefactor ( transactionID , originalCurrency . current , true ) ;
@@ -88,7 +73,7 @@ function IOURequestStepTaxAmountPage({
88
73
89
74
useFocusEffect (
90
75
useCallback ( ( ) => {
91
- focusTimeoutRef . current = setTimeout ( ( ) => textInput . current && textInput . current . focus ( ) , CONST . ANIMATED_TRANSITION ) ;
76
+ focusTimeoutRef . current = setTimeout ( ( ) => textInput . current ? .focus ( ) , CONST . ANIMATED_TRANSITION ) ;
92
77
return ( ) => {
93
78
if ( ! focusTimeoutRef . current ) {
94
79
return ;
@@ -111,12 +96,13 @@ function IOURequestStepTaxAmountPage({
111
96
) ;
112
97
} ;
113
98
114
- const updateTaxAmount = ( currentAmount ) => {
99
+ const updateTaxAmount = ( currentAmount : CurrentMoney ) => {
115
100
isSaveButtonPressed . current = true ;
116
101
const amountInSmallestCurrencyUnits = CurrencyUtils . convertToBackendAmount ( Number . parseFloat ( currentAmount . amount ) ) ;
117
102
IOU . setMoneyRequestTaxAmount ( transactionID , amountInSmallestCurrencyUnits , true ) ;
118
103
119
- IOU . setMoneyRequestCurrency_temporaryForRefactor ( transactionID , currency || CONST . CURRENCY . USD , true ) ;
104
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
105
+ IOU . setMoneyRequestCurrency_temporaryForRefactor ( transactionID , transaction ?. currency || CONST . CURRENCY . USD , true ) ;
120
106
121
107
if ( backTo ) {
122
108
Navigation . goBack ( backTo ) ;
@@ -126,7 +112,7 @@ function IOURequestStepTaxAmountPage({
126
112
// If a reportID exists in the report object, it's because the user started this flow from using the + button in the composer
127
113
// inside a report. In this case, the participants can be automatically assigned from the report and the user can skip the participants step and go straight
128
114
// to the confirm step.
129
- if ( report . reportID ) {
115
+ if ( report ? .reportID ) {
130
116
// TODO: Is this really needed at all?
131
117
IOU . setMoneyRequestParticipantsFromReport ( transactionID , report ) ;
132
118
Navigation . navigate ( ROUTES . MONEY_REQUEST_STEP_CONFIRMATION . getRoute ( CONST . IOU . ACTION . CREATE , iouType , transactionID , reportID ) ) ;
@@ -148,9 +134,9 @@ function IOURequestStepTaxAmountPage({
148
134
>
149
135
< MoneyRequestAmountForm
150
136
isEditing = { isEditing }
151
- currency = { currency }
152
- amount = { transaction . taxAmount }
153
- taxAmount = { getTaxAmount ( transaction , taxRates . defaultValue ) }
137
+ currency = { transaction ?. currency }
138
+ amount = { transaction ? .taxAmount }
139
+ taxAmount = { getTaxAmount ( transaction , taxRates ? .defaultValue ) }
154
140
ref = { ( e ) => ( textInput . current = e ) }
155
141
onCurrencyButtonPress = { navigateToCurrencySelectionPage }
156
142
onSubmitButtonPress = { updateTaxAmount }
@@ -159,16 +145,17 @@ function IOURequestStepTaxAmountPage({
159
145
) ;
160
146
}
161
147
162
- IOURequestStepTaxAmountPage . propTypes = propTypes ;
163
- IOURequestStepTaxAmountPage . defaultProps = defaultProps ;
164
148
IOURequestStepTaxAmountPage . displayName = 'IOURequestStepTaxAmountPage' ;
165
149
166
- export default compose (
167
- withWritableReportOrNotFound ,
168
- withFullTransactionOrNotFound ,
169
- withOnyx ( {
170
- policy : {
171
- key : ( { report} ) => `${ ONYXKEYS . COLLECTION . POLICY } ${ report ? report . policyID : '0' } ` ,
172
- } ,
173
- } ) ,
174
- ) ( IOURequestStepTaxAmountPage ) ;
150
+ const IOURequestStepTaxAmountPageWithOnyx = withOnyx < IOURequestStepTaxAmountPageProps , IOURequestStepTaxAmountPageOnyxProps > ( {
151
+ policy : {
152
+ key : ( { report} ) => `${ ONYXKEYS . COLLECTION . POLICY } ${ report ? report . policyID : '0' } ` ,
153
+ } ,
154
+ } ) ( IOURequestStepTaxAmountPage ) ;
155
+
156
+ // eslint-disable-next-line rulesdir/no-negated-variables
157
+ const IOURequestStepTaxAmountPageWithWritableReportOrNotFound = withWritableReportOrNotFound ( IOURequestStepTaxAmountPageWithOnyx ) ;
158
+ // eslint-disable-next-line rulesdir/no-negated-variables
159
+ const IOURequestStepTaxAmountPageWithFullTransactionOrNotFound = withFullTransactionOrNotFound ( IOURequestStepTaxAmountPageWithWritableReportOrNotFound ) ;
160
+
161
+ export default IOURequestStepTaxAmountPageWithFullTransactionOrNotFound ;
0 commit comments