-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
IOURequestStepTaxRatePage.tsx
99 lines (87 loc) · 3.99 KB
/
IOURequestStepTaxRatePage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import TaxPicker from '@components/TaxPicker';
import useLocalize from '@hooks/useLocalize';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import * as IOU from '@userActions/IOU';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
import type {Policy, TaxRatesWithDefault, Transaction} from '@src/types/onyx';
import StepScreenWrapper from './StepScreenWrapper';
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound';
import withWritableReportOrNotFound from './withWritableReportOrNotFound';
type IOURequestStepTaxRatePageOnyxProps = {
policy: OnyxEntry<Policy>;
};
type IOURequestStepTaxRatePageProps = IOURequestStepTaxRatePageOnyxProps &
WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_TAX_RATE> & {
transaction: OnyxEntry<Transaction>;
};
function getTaxAmount(taxRates: TaxRatesWithDefault, selectedTaxRate: string, amount: number): number | undefined {
const percentage = Object.values(OptionsListUtils.transformedTaxRates(taxRates)).find((taxRate) => taxRate.modifiedName?.includes(selectedTaxRate))?.value;
if (percentage) {
return TransactionUtils.calculateTaxAmount(percentage, amount);
}
}
function IOURequestStepTaxRatePage({
route: {
params: {backTo},
},
policy,
transaction,
report,
}: IOURequestStepTaxRatePageProps) {
const {translate} = useLocalize();
const navigateBack = () => {
Navigation.goBack(backTo);
};
const taxRates = policy?.taxRates;
const defaultTaxKey = taxRates?.defaultExternalID;
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const selectedTaxRate = transaction?.taxRate?.keyForList || defaultTaxKey;
const updateTaxRates = (taxes: OptionsListUtils.TaxRatesOption) => {
if (!transaction || !taxes.text || !taxRates) {
Navigation.goBack(backTo);
return;
}
const taxAmount = getTaxAmount(taxRates, taxes.text, transaction?.amount);
if (taxAmount === undefined) {
Navigation.goBack(backTo);
return;
}
const amountInSmallestCurrencyUnits = CurrencyUtils.convertToBackendAmount(taxAmount);
IOU.setMoneyRequestTaxRate(transaction?.transactionID, taxes);
IOU.setMoneyRequestTaxAmount(transaction.transactionID, amountInSmallestCurrencyUnits, true);
Navigation.goBack(backTo);
};
return (
<StepScreenWrapper
headerTitle={translate('iou.taxRate')}
onBackButtonPress={navigateBack}
shouldShowWrapper
testID={IOURequestStepTaxRatePage.displayName}
>
<TaxPicker
selectedTaxRate={selectedTaxRate}
policyID={report?.policyID}
onSubmit={updateTaxRates}
/>
</StepScreenWrapper>
);
}
IOURequestStepTaxRatePage.displayName = 'IOURequestStepTaxRatePage';
const IOURequestStepTaxRatePageWithOnyx = withOnyx<IOURequestStepTaxRatePageProps, IOURequestStepTaxRatePageOnyxProps>({
policy: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report ? report.policyID : '0'}`,
},
})(IOURequestStepTaxRatePage);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepTaxRatePageWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepTaxRatePageWithOnyx);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepTaxRatePageWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepTaxRatePageWithWritableReportOrNotFound);
export default IOURequestStepTaxRatePageWithFullTransactionOrNotFound;