-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathIOUCurrencySelection.js
198 lines (175 loc) · 7.74 KB
/
IOUCurrencySelection.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
import Str from 'expensify-common/lib/str';
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {Keyboard} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import {withNetwork} from '@components/OnyxProvider';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
import withLocalize, {withLocalizePropTypes} from '@components/withLocalize';
import compose from '@libs/compose';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {iouDefaultProps, iouPropTypes} from './propTypes';
/**
* IOU Currency selection for selecting currency
*/
const propTypes = {
/** Route from navigation */
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,
/** Currently selected currency */
currency: PropTypes.string,
/** Route to navigate back after selecting a currency */
backTo: PropTypes.string,
}),
}).isRequired,
// The currency list constant object from Onyx
currencyList: PropTypes.objectOf(
PropTypes.shape({
// Symbol for the currency
symbol: PropTypes.string,
// Name of the currency
name: PropTypes.string,
// ISO4217 Code for the currency
ISO4217: PropTypes.string,
}),
),
/** Holds data related to Money Request view state, rather than the underlying Money Request data. */
iou: iouPropTypes,
...withLocalizePropTypes,
};
const defaultProps = {
currencyList: {},
iou: iouDefaultProps,
};
function IOUCurrencySelection(props) {
const [searchValue, setSearchValue] = useState('');
const optionsSelectorRef = useRef();
const selectedCurrencyCode = (lodashGet(props.route, 'params.currency', props.iou.currency) || CONST.CURRENCY.USD).toUpperCase();
const iouType = lodashGet(props.route, 'params.iouType', CONST.IOU.TYPE.REQUEST);
const reportID = lodashGet(props.route, 'params.reportID', '');
const threadReportID = lodashGet(props.route, 'params.threadReportID', '');
// Decides whether to allow or disallow editing a money request
useEffect(() => {
// Do not dismiss the modal, when it is not the edit flow.
if (!threadReportID) {
return;
}
const report = ReportUtils.getReport(threadReportID);
const parentReportAction = ReportActionsUtils.getReportAction(report.parentReportID, report.parentReportActionID);
// Do not dismiss the modal, when a current user can edit this currency of this money request.
if (ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.CURRENCY)) {
return;
}
// Dismiss the modal when a current user cannot edit a money request.
Navigation.isNavigationReady().then(() => {
Navigation.dismissModal();
});
}, [threadReportID]);
const confirmCurrencySelection = useCallback(
(option) => {
const backTo = lodashGet(props.route, 'params.backTo', '');
Keyboard.dismiss();
// When we refresh the web, the money request route gets cleared from the navigation stack.
// Navigating to "backTo" will result in forward navigation instead, causing disruption to the currency selection.
// To prevent any negative experience, we have made the decision to simply close the currency selection page.
if (_.isEmpty(backTo) || props.navigation.getState().routes.length === 1) {
Navigation.goBack(ROUTES.HOME);
} else {
Navigation.navigate(`${props.route.params.backTo}?currency=${option.currencyCode}`);
}
},
[props.route, props.navigation],
);
const {translate, currencyList} = props;
const {sections, headerMessage, initiallyFocusedOptionKey} = useMemo(() => {
const currencyOptions = _.map(currencyList, (currencyInfo, currencyCode) => {
const isSelectedCurrency = currencyCode === selectedCurrencyCode;
return {
currencyName: currencyInfo.name,
text: `${currencyCode} - ${CurrencyUtils.getLocalizedCurrencySymbol(currencyCode)}`,
currencyCode,
keyForList: currencyCode,
isSelected: isSelectedCurrency,
};
});
const searchRegex = new RegExp(Str.escapeForRegExp(searchValue.trim().replace(CONST.REGEX.ANY_SPACE, ' ')), 'i');
const filteredCurrencies = _.filter(
currencyOptions,
(currencyOption) =>
searchRegex.test(currencyOption.text.replace(CONST.REGEX.ANY_SPACE, ' ')) || searchRegex.test(currencyOption.currencyName.replace(CONST.REGEX.ANY_SPACE, ' ')),
);
const isEmpty = searchValue.trim() && !filteredCurrencies.length;
return {
initiallyFocusedOptionKey: _.get(
_.find(filteredCurrencies, (currency) => currency.currencyCode === selectedCurrencyCode),
'keyForList',
),
sections: isEmpty
? []
: [
{
data: filteredCurrencies,
indexOffset: 0,
},
],
headerMessage: isEmpty ? translate('common.noResultsFound') : '',
};
}, [currencyList, searchValue, selectedCurrencyCode, translate]);
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
onEntryTransitionEnd={() => optionsSelectorRef.current && optionsSelectorRef.current.focus()}
testID={IOUCurrencySelection.displayName}
>
{({didScreenTransitionEnd}) => (
<>
<HeaderWithBackButton
title={translate('common.selectCurrency')}
onBackButtonPress={() => Navigation.goBack(ROUTES.MONEY_REQUEST.getRoute(iouType, reportID))}
/>
<SelectionList
sections={sections}
textInputLabel={translate('common.search')}
textInputValue={searchValue}
onChangeText={setSearchValue}
onSelectRow={(option) => {
if (!didScreenTransitionEnd) {
return;
}
confirmCurrencySelection(option);
}}
headerMessage={headerMessage}
initiallyFocusedOptionKey={initiallyFocusedOptionKey}
showScrollIndicator
/>
</>
)}
</ScreenWrapper>
);
}
IOUCurrencySelection.displayName = 'IOUCurrencySelection';
IOUCurrencySelection.propTypes = propTypes;
IOUCurrencySelection.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
currencyList: {key: ONYXKEYS.CURRENCY_LIST},
iou: {key: ONYXKEYS.IOU},
}),
withNetwork(),
)(IOUCurrencySelection);