-
Notifications
You must be signed in to change notification settings - Fork 3k
/
CountrySelection.tsx
91 lines (83 loc) · 3.93 KB
/
CountrySelection.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
import React, {useCallback, useMemo, useState} from 'react';
import {View} from 'react-native';
import SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/RadioListItem';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import searchOptions from '@libs/searchOptions';
import type {Option} from '@libs/searchOptions';
import StringUtils from '@libs/StringUtils';
import type {CustomSubStepProps} from '@pages/settings/Wallet/InternationalDepositAccount/types';
import * as BankAccounts from '@userActions/BankAccounts';
import Text from '@src/components/Text';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ROUTES from '@src/ROUTES';
function CountrySelection({isEditing, onNext, formValues, resetScreenIndex}: CustomSubStepProps) {
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const styles = useThemeStyles();
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');
const [currentCountry, setCurrentCountry] = useState(formValues.bankCountry);
const onCountrySelected = useCallback(() => {
if (currentCountry === CONST.COUNTRY.US) {
Navigation.navigate(ROUTES.SETTINGS_ADD_US_BANK_ACCOUNT);
return;
}
if (isEditing && formValues.bankCountry === currentCountry) {
onNext();
return;
}
BankAccounts.fetchCorpayFields(currentCountry, formValues.bankCurrency);
resetScreenIndex?.(CONST.CORPAY_FIELDS.INDEXES.MAPPING.BANK_ACCOUNT_DETAILS);
}, [currentCountry, formValues.bankCountry, formValues.bankCurrency, isEditing, onNext, resetScreenIndex]);
const onSelectionChange = useCallback((country: Option) => {
setCurrentCountry(country.value);
}, []);
const countries = useMemo(
() =>
Object.keys(CONST.ALL_COUNTRIES).map((countryISO) => {
const countryName = translate(`allCountries.${countryISO}` as TranslationPaths);
return {
value: countryISO,
keyForList: countryISO,
text: countryName,
isSelected: currentCountry === countryISO,
searchValue: StringUtils.sanitizeString(`${countryISO}${countryName}`),
};
}),
[translate, currentCountry],
);
const searchResults = searchOptions(debouncedSearchValue, countries);
const headerMessage = debouncedSearchValue.trim() && !searchResults.length ? translate('common.noResultsFound') : '';
return (
<>
<View style={styles.ph5}>
<Text style={[styles.textHeadlineLineHeightXXL, styles.mb6]}>{translate('addPersonalBankAccount.countrySelectionStepHeader')}</Text>
</View>
<SelectionList
headerMessage={headerMessage}
sections={[{data: searchResults}]}
textInputValue={searchValue}
textInputLabel={translate('common.search')}
onChangeText={setSearchValue}
onSelectRow={onSelectionChange}
onConfirm={onCountrySelected}
ListItem={RadioListItem}
initiallyFocusedOptionKey={currentCountry}
shouldSingleExecuteRowSelect
shouldStopPropagation
shouldUseDynamicMaxToRenderPerBatch
showConfirmButton
confirmButtonText={isEditing ? translate('common.confirm') : translate('common.next')}
isConfirmButtonDisabled={isOffline}
shouldUpdateFocusedIndex
/>
</>
);
}
CountrySelection.displayName = 'CountrySelection';
export default CountrySelection;