|
| 1 | +import React from 'react'; |
| 2 | +import { Text, View, Image } from 'react-native'; |
| 3 | +import i18next from 'i18next'; |
| 4 | + |
| 5 | +import { useTheme } from 'contexts/ThemeContext'; |
| 6 | +import { useCurrentAccount } from 'modules/Accounts/hooks/useCurrentAccount'; |
| 7 | +import { useAccountTokenBalancesQuery } from 'modules/Accounts/api/useAccountTokenBalancesQuery'; |
| 8 | +import { useApplicationSupportedTokensQuery } from 'modules/BlockchainApplication/api/useApplicationSupportedTokensQuery'; |
| 9 | +import Picker from 'components/shared/Picker'; |
| 10 | +import InfiniteScrollList from 'components/shared/InfiniteScrollList'; |
| 11 | +import Skeleton from 'components/shared/Skeleton/Skeleton'; |
| 12 | +import DataRenderer from 'components/shared/DataRenderer'; |
| 13 | +import { fromBaseToDisplayDenom } from 'utilities/conversions.utils'; |
| 14 | +import { deviceWidth } from 'utilities/device'; |
| 15 | + |
| 16 | +import getStyles from './RequestTokenSelectField.styles'; |
| 17 | + |
| 18 | +export function RequestTokenSelectField({ |
| 19 | + value, |
| 20 | + onChange, |
| 21 | + recipientApplication, |
| 22 | + errorMessage, |
| 23 | + style, |
| 24 | +}) { |
| 25 | + const { |
| 26 | + data: supportedTokensData, |
| 27 | + isLoading: isLoadingSupportedTokens, |
| 28 | + isError: isSupportedTokensError, |
| 29 | + } = useApplicationSupportedTokensQuery(recipientApplication); |
| 30 | + |
| 31 | + const [currentAccount] = useCurrentAccount(); |
| 32 | + |
| 33 | + const { data: tokenBalanceData } = useAccountTokenBalancesQuery(currentAccount.metadata.address, { |
| 34 | + params: { tokenID: value }, |
| 35 | + }); |
| 36 | + |
| 37 | + const { styles } = useTheme({ styles: getStyles() }); |
| 38 | + |
| 39 | + const selectedToken = supportedTokensData?.find((token) => token.tokenID === value); |
| 40 | + |
| 41 | + const tokenBalance = |
| 42 | + selectedToken && tokenBalanceData |
| 43 | + ? fromBaseToDisplayDenom({ |
| 44 | + amount: tokenBalanceData?.data[0]?.availableBalance || 0, |
| 45 | + displayDenom: selectedToken.displayDenom, |
| 46 | + denomUnits: selectedToken.denomUnits, |
| 47 | + symbol: selectedToken.symbol, |
| 48 | + withSymbol: true, |
| 49 | + formatAmount: true, |
| 50 | + }) |
| 51 | + : 0; |
| 52 | + |
| 53 | + const renderOptions = (data = supportedTokensData) => ( |
| 54 | + <InfiniteScrollList |
| 55 | + data={data} |
| 56 | + keyExtractor={(item) => item.tokenID} |
| 57 | + renderItem={(item) => ( |
| 58 | + <Picker.Item |
| 59 | + key={item.tokenID} |
| 60 | + value={item.tokenID} |
| 61 | + onChange={onChange} |
| 62 | + testID={`token-select-${item.symbol}`} |
| 63 | + > |
| 64 | + <Text style={[styles.theme.text]}>{item.symbol}</Text> |
| 65 | + <Image source={{ uri: item.logo.png }} style={styles.logo} /> |
| 66 | + </Picker.Item> |
| 67 | + )} |
| 68 | + /> |
| 69 | + ); |
| 70 | + |
| 71 | + const { showOptions } = Picker.usePickerMenu(renderOptions()); |
| 72 | + |
| 73 | + return ( |
| 74 | + <Picker value={value} error={errorMessage}> |
| 75 | + <View style={{ ...styles.row, justifyContent: 'space-between' }}> |
| 76 | + <Picker.Label style={style?.label}> |
| 77 | + {i18next.t('sendToken.tokenSelect.tokenIDFieldLabel')} |
| 78 | + </Picker.Label> |
| 79 | + |
| 80 | + {selectedToken && ( |
| 81 | + <Picker.Label style={style?.label}> |
| 82 | + {i18next.t('sendToken.tokenSelect.tokenIDBalanceLabel')}:{' '} |
| 83 | + <Text style={[styles.primaryText]}>{tokenBalance}</Text> |
| 84 | + </Picker.Label> |
| 85 | + )} |
| 86 | + </View> |
| 87 | + |
| 88 | + <DataRenderer |
| 89 | + data={supportedTokensData} |
| 90 | + isLoading={isLoadingSupportedTokens} |
| 91 | + error={isSupportedTokensError} |
| 92 | + renderData={() => ( |
| 93 | + <Picker.Toggle style={style?.toggle} openMenu={showOptions}> |
| 94 | + {selectedToken && ( |
| 95 | + <View style={[styles.row]} testID="select-token-picker"> |
| 96 | + <Text style={[styles.text, styles.theme.text]}>{selectedToken.symbol}</Text> |
| 97 | + <Image source={{ uri: selectedToken.logo?.png }} style={styles.logo} /> |
| 98 | + </View> |
| 99 | + )} |
| 100 | + </Picker.Toggle> |
| 101 | + )} |
| 102 | + renderLoading={() => ( |
| 103 | + <Skeleton height={48} width={deviceWidth() - 44} style={{ container: styles.skeleton }} /> |
| 104 | + )} |
| 105 | + /> |
| 106 | + </Picker> |
| 107 | + ); |
| 108 | +} |
0 commit comments