import React from 'react'; import translate from 'translations'; import { UnitDisplay, Spinner } from 'components/ui'; import Select from 'react-select'; import { TFetchCCRatesRequested, fetchCCRatesRequested } from 'actions/rates'; import { rateSymbols } from 'api/rates'; import { chain, flatMap } from 'lodash'; import { TokenBalance, getShownTokenBalances } from 'selectors/wallet'; import { Balance } from 'libs/wallet'; import './EquivalentValues.scss'; import { Wei } from 'libs/units'; import { AppState } from 'reducers'; import { getNetworkConfig, getOffline } from 'selectors/config'; import { connect } from 'react-redux'; import btcIco from 'assets/images/bitcoin.png'; import ethIco from 'assets/images/ether.png'; import repIco from 'assets/images/augur.png'; import { NetworkConfig } from 'types/network'; interface AllValue { symbol: string; balance: Balance['wei']; } interface DefaultOption { label: string; value: AllValue[]; } interface Option { label: string; value: Balance['wei'] | AllValue[]; } interface State { equivalentValues: Option; options: Option[]; } interface StateProps { balance: Balance; network: NetworkConfig; tokenBalances: TokenBalance[]; rates: AppState['rates']['rates']; ratesError: AppState['rates']['ratesError']; isOffline: AppState['config']['meta']['offline']; } interface DispatchProps { fetchCCRates: TFetchCCRatesRequested; } type Props = StateProps & DispatchProps; class EquivalentValues extends React.Component { private requestedCurrencies: string[] | null = null; public constructor(props: Props) { super(props); const { balance, tokenBalances, network } = this.props; this.state = { equivalentValues: this.defaultOption(balance, tokenBalances, network), options: [] }; if (props.balance && props.tokenBalances) { this.fetchRates(props); } } public defaultOption( balance: Balance, tokenBalances: TokenBalance[], network: StateProps['network'] ): DefaultOption { return { label: 'All', value: [{ symbol: network.unit, balance: balance.wei }, ...tokenBalances] }; } public componentWillReceiveProps(nextProps: Props) { const { balance, tokenBalances, isOffline, network } = this.props; if ( nextProps.balance !== balance || nextProps.tokenBalances !== tokenBalances || nextProps.isOffline !== isOffline || nextProps.network.unit !== network.unit ) { const defaultOption = this.defaultOption( nextProps.balance, nextProps.tokenBalances, nextProps.network ); const options: Option[] = [ defaultOption, { label: nextProps.network.unit, value: nextProps.balance.wei }, ...Object.values(nextProps.tokenBalances).map(token => { return { label: token.symbol, value: token.balance }; }) ]; const equivalentValues = options.find(opt => opt.label === this.state.equivalentValues.label) || defaultOption; this.setState({ equivalentValues, options }); this.fetchRates(nextProps); } } public selectOption = equivalentValues => { this.setState({ equivalentValues }); }; public render(): JSX.Element { const { balance, isOffline, tokenBalances, rates, network, ratesError } = this.props; const { equivalentValues, options } = this.state; const isFetching = !balance || balance.isPending || !tokenBalances || Object.keys(rates).length === 0; const pairRates = this.generateValues(equivalentValues.label, equivalentValues.value); const fiatSymbols = { USD: '$', EUR: '€', GBP: '£', CHF: ' ' }; const coinAndTokenSymbols = { BTC: btcIco, ETH: ethIco, REP: repIco }; const Value = ({ className = '', rate, value, symbol = '', icon = '' }) => (
{!!symbol && {symbol}} {rate}{' '}
); return (
{translate('sidebar_Equiv')}