-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathNewRequestAmountPage.js
213 lines (183 loc) · 7.73 KB
/
NewRequestAmountPage.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import React, {useCallback, useEffect, useRef} from 'react';
import {InteractionManager, View} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import {useFocusEffect} from '@react-navigation/native';
import lodashGet from 'lodash/get';
import _ from 'underscore';
import ONYXKEYS from '../../../ONYXKEYS';
import Navigation from '../../../libs/Navigation/Navigation';
import ROUTES from '../../../ROUTES';
import * as ReportUtils from '../../../libs/ReportUtils';
import * as CurrencyUtils from '../../../libs/CurrencyUtils';
import CONST from '../../../CONST';
import reportPropTypes from '../../reportPropTypes';
import participantPropTypes from '../../../components/participantPropTypes';
import * as IOU from '../../../libs/actions/IOU';
import useLocalize from '../../../hooks/useLocalize';
import MoneyRequestAmountForm from './MoneyRequestAmountForm';
import * as IOUUtils from '../../../libs/IOUUtils';
import FullPageNotFoundView from '../../../components/BlockingViews/FullPageNotFoundView';
import styles from '../../../styles/styles';
import HeaderWithBackButton from '../../../components/HeaderWithBackButton';
import ScreenWrapper from '../../../components/ScreenWrapper';
const propTypes = {
/** React Navigation route */
route: PropTypes.shape({
/** Params from the route */
params: PropTypes.shape({
/** The type of IOU report, i.e. bill, request, send */
iouType: PropTypes.string,
/** The report ID of the IOU */
reportID: PropTypes.string,
/** Selected currency from IOUCurrencySelection */
currency: PropTypes.string,
}),
}).isRequired,
/** The report on which the request is initiated on */
report: reportPropTypes,
/** Holds data related to Money Request view state, rather than the underlying Money Request data. */
iou: PropTypes.shape({
/** ID (iouType + reportID) of the request */
id: PropTypes.string,
/** Amount of the request */
amount: PropTypes.number,
/** Currency of the request */
currency: PropTypes.string,
merchant: PropTypes.string,
created: PropTypes.string,
/** List of the participants */
participants: PropTypes.arrayOf(participantPropTypes),
}),
};
const defaultProps = {
report: {},
iou: {
id: '',
amount: 0,
created: '',
merchant: '',
currency: CONST.CURRENCY.USD,
participants: [],
},
};
function NewRequestAmountPage({route, iou, report}) {
const {translate} = useLocalize();
const prevMoneyRequestID = useRef(iou.id);
const textInput = useRef(null);
const iouType = lodashGet(route, 'params.iouType', '');
const reportID = lodashGet(route, 'params.reportID', '');
const isEditing = lodashGet(route, 'path', '').includes('amount');
const currentCurrency = lodashGet(route, 'params.currency', '');
const currency = currentCurrency || iou.currency;
const focusTextInput = () => {
// Component may not be initialized due to navigation transitions
// Wait until interactions are complete before trying to focus
InteractionManager.runAfterInteractions(() => {
// Focus text input
if (!textInput.current) {
return;
}
textInput.current.focus();
});
};
useFocusEffect(
useCallback(() => {
focusTextInput();
}, []),
);
// Check and dismiss modal
useEffect(() => {
if (!ReportUtils.shouldDisableWriteActions(report)) {
return;
}
Navigation.dismissModal(reportID);
}, [report, reportID]);
// Because we use Onyx to store IOU info, when we try to make two different money requests from different tabs,
// it can result in an IOU sent with improper values. In such cases we want to reset the flow and redirect the user to the first step of the IOU.
useEffect(() => {
if (isEditing) {
// ID in Onyx could change by initiating a new request in a separate browser tab or completing a request
if (prevMoneyRequestID.current !== iou.id) {
// The ID is cleared on completing a request. In that case, we will do nothing.
if (!iou.id) {
return;
}
Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType, reportID), true);
return;
}
const moneyRequestID = `${iouType}${reportID}`;
const shouldReset = iou.id !== moneyRequestID;
if (shouldReset) {
IOU.resetMoneyRequestInfo(moneyRequestID);
}
if (_.isEmpty(iou.participants) || iou.amount === 0 || shouldReset) {
Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType, reportID), true);
}
}
return () => {
prevMoneyRequestID.current = iou.id;
};
}, [iou.participants, iou.amount, iou.id, isEditing, iouType, reportID]);
const navigateBack = () => {
Navigation.goBack(isEditing ? ROUTES.getMoneyRequestConfirmationRoute(iouType, reportID) : null);
};
const navigateToCurrencySelectionPage = () => {
// Remove query from the route and encode it.
const activeRoute = encodeURIComponent(Navigation.getActiveRoute().replace(/\?.*/, ''));
Navigation.navigate(ROUTES.getMoneyRequestCurrencyRoute(iouType, reportID, currency, activeRoute));
};
const navigateToNextPage = (currentAmount) => {
const amountInSmallestCurrencyUnits = CurrencyUtils.convertToBackendAmount(Number.parseFloat(currentAmount));
IOU.setMoneyRequestAmount(amountInSmallestCurrencyUnits);
IOU.setMoneyRequestCurrency(currency);
if (isEditing) {
Navigation.goBack(ROUTES.getMoneyRequestConfirmationRoute(iouType, reportID));
return;
}
IOU.navigateToNextPage(iou, iouType, reportID, report);
};
const content = (
<MoneyRequestAmountForm
isEditing={isEditing}
currency={currency}
amount={iou.amount}
ref={(e) => (textInput.current = e)}
onCurrencyButtonPress={navigateToCurrencySelectionPage}
onSubmitButtonPress={navigateToNextPage}
/>
);
// ScreenWrapper is only needed in edit mode because we have a dedicated route for the edit amount page (MoneyRequestEditAmountPage).
// The rest of the cases this component is rendered through <MoneyRequestSelectorPage /> which has it's own ScreenWrapper
if (!isEditing) {
return content;
}
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableKeyboardAvoidingView={false}
onEntryTransitionEnd={focusTextInput}
>
{({safeAreaPaddingBottomStyle}) => (
<FullPageNotFoundView shouldShow={!IOUUtils.isValidMoneyRequestType(iouType)}>
<View style={[styles.flex1, safeAreaPaddingBottomStyle]}>
<HeaderWithBackButton
title={translate('iou.amount')}
onBackButonBackButtonPress={navigateBack}
/>
{content}
</View>
</FullPageNotFoundView>
)}
</ScreenWrapper>
);
}
NewRequestAmountPage.propTypes = propTypes;
NewRequestAmountPage.defaultProps = defaultProps;
NewRequestAmountPage.displayName = 'NewRequestAmountPage';
export default withOnyx({
iou: {key: ONYXKEYS.IOU},
report: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${lodashGet(route, 'params.reportID', '')}`,
},
})(NewRequestAmountPage);