-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathWorkspaceReimburseView.js
183 lines (167 loc) · 8.26 KB
/
WorkspaceReimburseView.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {useCallback, useEffect, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import CopyTextToClipboard from '@components/CopyTextToClipboard';
import * as Expensicons from '@components/Icon/Expensicons';
import * as Illustrations from '@components/Icon/Illustrations';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import networkPropTypes from '@components/networkPropTypes';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import {withNetwork} from '@components/OnyxProvider';
import Section from '@components/Section';
import Text from '@components/Text';
import withLocalize, {withLocalizePropTypes} from '@components/withLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import compose from '@libs/compose';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReimbursementAccountProps from '@pages/ReimbursementAccount/reimbursementAccountPropTypes';
import * as BankAccounts from '@userActions/BankAccounts';
import * as Link from '@userActions/Link';
import * as Policy from '@userActions/Policy';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import WorkspaceReimburseSection from './WorkspaceReimburseSection';
const propTypes = {
/** Policy values needed in the component */
policy: PropTypes.shape({
id: PropTypes.string,
customUnits: PropTypes.objectOf(
PropTypes.shape({
customUnitID: PropTypes.string,
name: PropTypes.string,
attributes: PropTypes.shape({
unit: PropTypes.string,
}),
rates: PropTypes.objectOf(
PropTypes.shape({
customUnitRateID: PropTypes.string,
name: PropTypes.string,
rate: PropTypes.number,
}),
),
}),
),
outputCurrency: PropTypes.string,
lastModified: PropTypes.number,
}).isRequired,
/** From Onyx */
/** Bank account attached to free plan */
reimbursementAccount: ReimbursementAccountProps.reimbursementAccountPropTypes,
/** Information about the network */
network: networkPropTypes.isRequired,
...withLocalizePropTypes,
};
const defaultProps = {
reimbursementAccount: ReimbursementAccountProps.reimbursementAccountDefaultProps,
};
function WorkspaceReimburseView(props) {
const styles = useThemeStyles();
const [currentRatePerUnit, setCurrentRatePerUnit] = useState('');
const {isSmallScreenWidth} = useWindowDimensions();
const viewAllReceiptsUrl = `expenses?policyIDList=${props.policy.id}&billableReimbursable=reimbursable&submitterEmail=%2B%2B`;
const distanceCustomUnit = _.find(lodashGet(props.policy, 'customUnits', {}), (unit) => unit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE);
const distanceCustomRate = _.find(lodashGet(distanceCustomUnit, 'rates', {}), (rate) => rate.name === CONST.CUSTOM_UNITS.DEFAULT_RATE);
const {translate, toLocaleDigit} = props;
const getUnitLabel = useCallback((value) => translate(`common.${value}`), [translate]);
const getCurrentRatePerUnitLabel = useCallback(() => {
const customUnitRate = _.find(lodashGet(distanceCustomUnit, 'rates', '{}'), (rate) => rate && rate.name === CONST.CUSTOM_UNITS.DEFAULT_RATE);
const currentUnit = getUnitLabel(lodashGet(distanceCustomUnit, 'attributes.unit', CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES));
const currentRate = PolicyUtils.getUnitRateValue(customUnitRate, toLocaleDigit);
const perWord = translate('common.per');
return `${currentRate} ${perWord} ${currentUnit}`;
}, [translate, distanceCustomUnit, toLocaleDigit, getUnitLabel]);
const fetchData = useCallback(() => {
// Instead of setting the reimbursement account loading within the optimistic data of the API command, use a separate action so that the Onyx value is updated right away.
// openWorkspaceReimburseView uses API.read which will not make the request until all WRITE requests in the sequential queue have finished responding, so there would be a delay in
// updating Onyx with the optimistic data.
BankAccounts.setReimbursementAccountLoading(true);
Policy.openWorkspaceReimburseView(props.policy.id);
}, [props.policy.id]);
useEffect(() => {
if (props.network.isOffline) {
return;
}
fetchData();
}, [props.network.isOffline, fetchData]);
useEffect(() => {
setCurrentRatePerUnit(getCurrentRatePerUnitLabel());
}, [props.policy.customUnits, getCurrentRatePerUnitLabel]);
return (
<View style={[styles.mt3, isSmallScreenWidth ? styles.workspaceSectionMobile : styles.workspaceSection]}>
<Section
title={translate('workspace.reimburse.captureReceipts')}
icon={Illustrations.MoneyReceipts}
isCentralPane
menuItems={[
{
title: translate('workspace.reimburse.viewAllReceipts'),
onPress: () => Link.openOldDotLink(viewAllReceiptsUrl),
icon: Expensicons.Receipt,
shouldShowRightIcon: true,
iconRight: Expensicons.NewWindow,
wrapperStyle: [styles.cardMenuItem],
link: () => Link.buildOldDotURL(viewAllReceiptsUrl),
},
]}
>
<View style={[styles.mv3, styles.flexRow, styles.flexWrap]}>
<Text>
{translate('workspace.reimburse.captureNoVBACopyBeforeEmail')}
<CopyTextToClipboard
text={CONST.EMAIL.RECEIPTS}
textStyles={[styles.textBlue]}
/>
<Text>{translate('workspace.reimburse.captureNoVBACopyAfterEmail')}</Text>
</Text>
</View>
</Section>
<Section
title={translate('workspace.reimburse.trackDistance')}
icon={Illustrations.TrackShoe}
isCentralPane
>
<View style={[styles.mv3, styles.flexRow, styles.flexWrap]}>
<Text>{translate('workspace.reimburse.trackDistanceCopy')}</Text>
<OfflineWithFeedback
pendingAction={lodashGet(distanceCustomUnit, 'pendingAction') || lodashGet(distanceCustomRate, 'pendingAction')}
shouldShowErrorMessages={false}
style={styles.w100}
>
<MenuItemWithTopDescription
title={currentRatePerUnit}
description={translate('workspace.reimburse.trackDistanceRate')}
shouldShowRightIcon
onPress={() => Navigation.navigate(ROUTES.WORKSPACE_RATE_AND_UNIT.getRoute(props.policy.id))}
wrapperStyle={[styles.mt3, styles.ph8, styles.mhn8, styles.wAuto]}
brickRoadIndicator={(lodashGet(distanceCustomUnit, 'errors') || lodashGet(distanceCustomRate, 'errors')) && CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR}
/>
</OfflineWithFeedback>
</View>
</Section>
<WorkspaceReimburseSection
policy={props.policy}
reimbursementAccount={props.reimbursementAccount}
network={props.network}
translate={translate}
/>
</View>
);
}
WorkspaceReimburseView.defaultProps = defaultProps;
WorkspaceReimburseView.propTypes = propTypes;
WorkspaceReimburseView.displayName = 'WorkspaceReimburseView';
export default compose(
withLocalize,
withNetwork(),
withOnyx({
reimbursementAccount: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
},
}),
)(WorkspaceReimburseView);