-
Notifications
You must be signed in to change notification settings - Fork 3k
/
IOURequestStepDistanceRate.tsx
129 lines (112 loc) · 5.75 KB
/
IOURequestStepDistanceRate.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React from 'react';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/RadioListItem';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as IOU from '@libs/actions/IOU';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import type {MileageRate} from '@libs/DistanceRequestUtils';
import DistanceRequestUtils from '@libs/DistanceRequestUtils';
import Navigation from '@libs/Navigation/Navigation';
import {getCustomUnit, isTaxTrackingEnabled} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
import type {Policy, Transaction} from '@src/types/onyx';
import type {Unit} from '@src/types/onyx/Policy';
import StepScreenWrapper from './StepScreenWrapper';
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound';
import withWritableReportOrNotFound from './withWritableReportOrNotFound';
type IOURequestStepDistanceRateOnyxProps = {
/** Policy details */
policy: OnyxEntry<Policy>;
/** Mileage rates */
rates: Record<string, MileageRate>;
};
type IOURequestStepDistanceRateProps = IOURequestStepDistanceRateOnyxProps &
WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_DISTANCE_RATE> & {
/** Holds data related to Money Request view state, rather than the underlying Money Request data. */
transaction: OnyxEntry<Transaction>;
};
function IOURequestStepDistanceRate({
policy,
report,
route: {
params: {backTo, transactionID},
},
transaction,
rates,
}: IOURequestStepDistanceRateProps) {
const styles = useThemeStyles();
const {translate, toLocaleDigit} = useLocalize();
const isDistanceRequest = TransactionUtils.isDistanceRequest(transaction);
const distanceUnit = getCustomUnit(policy);
const customUnitID = distanceUnit?.customUnitID;
const isPolicyExpenseChat = ReportUtils.isReportInGroupPolicy(report);
const shouldShowTax = isTaxTrackingEnabled(isPolicyExpenseChat, policy, isDistanceRequest);
const lastSelectedRateID = TransactionUtils.getRateID(transaction) ?? '';
const navigateBack = () => {
Navigation.goBack(backTo);
};
const sections = Object.values(rates).map((rate) => {
const rateForDisplay = DistanceRequestUtils.getRateForDisplay(rate.unit, rate.rate, rate.currency, translate, toLocaleDigit);
return {
text: rate.name ?? rateForDisplay,
alternateText: rate.name ? rateForDisplay : '',
keyForList: rate.customUnitRateID,
value: rate.customUnitRateID,
isSelected: lastSelectedRateID ? lastSelectedRateID === rate.customUnitRateID : Boolean(rate.name === CONST.CUSTOM_UNITS.DEFAULT_RATE),
};
});
const unit = (Object.values(rates)[0]?.unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.mile') : translate('common.kilometer')) as Unit;
const initiallyFocusedOption = sections.find((item) => item.isSelected)?.keyForList;
function selectDistanceRate(customUnitRateID: string) {
if (policy?.customUnits && customUnitID && shouldShowTax) {
const policyCustomUnitRate = policy?.customUnits[customUnitID].rates[customUnitRateID];
const taxRateExternalID = policyCustomUnitRate.attributes?.taxRateExternalID ?? '';
const taxAmount = CurrencyUtils.convertToBackendAmount(DistanceRequestUtils.calculateTaxAmount(policy, transaction, customUnitRateID));
IOU.setMoneyRequestTaxAmount(transactionID, taxAmount);
IOU.setMoneyRequestTaxRate(transactionID, taxRateExternalID);
}
IOU.updateDistanceRequestRate(transactionID, customUnitRateID, policy?.id ?? '');
navigateBack();
}
return (
<StepScreenWrapper
headerTitle={translate('common.rate')}
onBackButtonPress={navigateBack}
shouldShowWrapper={Boolean(backTo)}
testID="rate"
>
<Text style={[styles.mh5, styles.mv4]}>{translate('iou.chooseARate', {unit})}</Text>
<SelectionList
sections={[{data: sections}]}
ListItem={RadioListItem}
onSelectRow={({value}) => selectDistanceRate(value ?? '')}
shouldDebounceRowSelect
initiallyFocusedOptionKey={initiallyFocusedOption}
/>
</StepScreenWrapper>
);
}
IOURequestStepDistanceRate.displayName = 'IOURequestStepDistanceRate';
const IOURequestStepDistanceRateWithOnyx = withOnyx<IOURequestStepDistanceRateProps, IOURequestStepDistanceRateOnyxProps>({
policy: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report ? report.policyID : '0'}`,
},
rates: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report?.policyID ?? '0'}`,
selector: (policy: OnyxEntry<Policy>) => DistanceRequestUtils.getMileageRates(policy),
},
})(IOURequestStepDistanceRate);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepDistanceRateWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepDistanceRateWithOnyx);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepDistanceRateWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepDistanceRateWithWritableReportOrNotFound);
export default IOURequestStepDistanceRateWithFullTransactionOrNotFound;