import React from 'react'; import translate from 'translations'; import { UnitDisplay, Spinner } from 'components/ui'; import Select from 'react-select'; import { TFetchCCRates, fetchCCRates, rateSymbols } from 'actions/rates'; import { chain, flatMap } from 'lodash'; import { TokenBalance, getShownTokenBalances } from 'selectors/wallet'; import { Balance } from 'libs/wallet'; import { NetworkConfig } from 'config'; import './EquivalentValues.scss'; import { Wei } from 'libs/units'; import { AppState } from 'reducers'; import { getNetworkConfig } from 'selectors/config'; import { connect } from 'react-redux'; 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']['offline']; } interface DispatchProps { fetchCCRates: TFetchCCRates; } 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: NetworkConfig ): DefaultOption { return { label: 'All', value: [{ symbol: network.unit, balance: balance.wei }, ...tokenBalances] }; } public componentWillReceiveProps(nextProps: Props) { const { balance, tokenBalances, isOffline } = this.props; if ( nextProps.balance !== balance || nextProps.tokenBalances !== tokenBalances || nextProps.isOffline !== isOffline ) { 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 Value = ({ rate, value }) => (
{rate}{' '}
); return (
{translate('sidebar_Equiv')}