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 { 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'; import BN from 'bn.js'; 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; } interface FiatSymbols { [key: string]: string; } interface Rates { [rate: string]: number; } 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: Option) => { 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: FiatSymbols = { USD: '$', EUR: '€', GBP: '£', CHF: ' ' }; const coinAndTokenSymbols: any = { BTC: btcIco, ETH: ethIco, REP: repIco }; interface ValueProps { className: string; rate: string; value: BN | null; symbol?: string; icon?: string; key?: number | string; } const Value = (props: ValueProps) => (
{!!props.symbol && ( {props.symbol} )} {props.rate}{' '}
); return (
{translate('SIDEBAR_EQUIV')}