-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
WorkspaceReimburseView.js
177 lines (161 loc) · 7.93 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
import React, {useState, useEffect, useCallback} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import lodashGet from 'lodash/get';
import _ from 'underscore';
import MenuItemWithTopDescription from '../../../components/MenuItemWithTopDescription';
import Text from '../../../components/Text';
import styles from '../../../styles/styles';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import * as Expensicons from '../../../components/Icon/Expensicons';
import * as Illustrations from '../../../components/Icon/Illustrations';
import Section from '../../../components/Section';
import Navigation from '../../../libs/Navigation/Navigation';
import CopyTextToClipboard from '../../../components/CopyTextToClipboard';
import * as Link from '../../../libs/actions/Link';
import compose from '../../../libs/compose';
import * as Policy from '../../../libs/actions/Policy';
import * as PolicyUtils from '../../../libs/PolicyUtils';
import CONST from '../../../CONST';
import ROUTES from '../../../ROUTES';
import ONYXKEYS from '../../../ONYXKEYS';
import * as ReimbursementAccountProps from '../../ReimbursementAccount/reimbursementAccountPropTypes';
import {withNetwork} from '../../../components/OnyxProvider';
import networkPropTypes from '../../../components/networkPropTypes';
import WorkspaceReimburseSection from './WorkspaceReimburseSection';
import * as BankAccounts from '../../../libs/actions/BankAccounts';
import OfflineWithFeedback from '../../../components/OfflineWithFeedback';
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 [currentRatePerUnit, setCurrentRatePerUnit] = useState('');
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 (
<>
<Section
title={translate('workspace.reimburse.captureReceipts')}
icon={Illustrations.MoneyReceipts}
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}
>
<View style={[styles.mv3]}>
<Text>{translate('workspace.reimburse.trackDistanceCopy')}</Text>
</View>
<OfflineWithFeedback
pendingAction={lodashGet(distanceCustomUnit, 'pendingAction') || lodashGet(distanceCustomRate, 'pendingAction')}
shouldShowErrorMessages={false}
>
<MenuItemWithTopDescription
title={currentRatePerUnit}
description={translate('workspace.reimburse.trackDistanceRate')}
shouldShowRightIcon
onPress={() => Navigation.navigate(ROUTES.WORKSPACE_RATE_AND_UNIT.getRoute(props.policy.id))}
wrapperStyle={[styles.mhn5, styles.wAuto]}
brickRoadIndicator={(lodashGet(distanceCustomUnit, 'errors') || lodashGet(distanceCustomRate, 'errors')) && CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR}
/>
</OfflineWithFeedback>
</Section>
<WorkspaceReimburseSection
policy={props.policy}
reimbursementAccount={props.reimbursementAccount}
network={props.network}
translate={translate}
/>
</>
);
}
WorkspaceReimburseView.defaultProps = defaultProps;
WorkspaceReimburseView.propTypes = propTypes;
WorkspaceReimburseView.displayName = 'WorkspaceReimburseView';
export default compose(
withLocalize,
withNetwork(),
withOnyx({
reimbursementAccount: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
},
}),
)(WorkspaceReimburseView);