From c32c30fa8c03a18510a78a37a6509b488e8ce0a2 Mon Sep 17 00:00:00 2001 From: Vasek Mlejnsky Date: Thu, 30 Aug 2018 10:08:20 +0200 Subject: [PATCH] Refactorize Detail component - Rename it to "AccountBalance" - Convert component to class - CSS refactorization - Handle hidden toggle inside component instead of using actions/reducers --- .../Wallet/views/AccountSummary/Container.js | 3 - .../components/AccountBalance/index.js | 137 ++++++------------ .../components/Details/index.js | 75 ---------- .../Wallet/views/AccountSummary/index.js | 4 +- 4 files changed, 47 insertions(+), 172 deletions(-) delete mode 100644 src/views/Wallet/views/AccountSummary/components/Details/index.js diff --git a/src/views/Wallet/views/AccountSummary/Container.js b/src/views/Wallet/views/AccountSummary/Container.js index 7a7b7c83..9f568e9f 100644 --- a/src/views/Wallet/views/AccountSummary/Container.js +++ b/src/views/Wallet/views/AccountSummary/Container.js @@ -3,7 +3,6 @@ import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; import type { MapStateToProps, MapDispatchToProps } from 'react-redux'; -import * as SummaryActions from 'actions/SummaryActions'; import * as TokenActions from 'actions/TokenActions'; import type { State, Dispatch } from 'flowtype'; @@ -20,7 +19,6 @@ type StateProps = BaseStateProps & { }; type DispatchProps = BaseDispatchProps & { - onDetailsToggle: typeof SummaryActions.onDetailsToggle, addToken: typeof TokenActions.add, loadTokens: typeof TokenActions.load, removeToken: typeof TokenActions.remove, @@ -40,7 +38,6 @@ const mapStateToProps: MapStateToProps = (state: St }); const mapDispatchToProps: MapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({ - onDetailsToggle: bindActionCreators(SummaryActions.onDetailsToggle, dispatch), addToken: bindActionCreators(TokenActions.add, dispatch), loadTokens: bindActionCreators(TokenActions.load, dispatch), removeToken: bindActionCreators(TokenActions.remove, dispatch), diff --git a/src/views/Wallet/views/AccountSummary/components/AccountBalance/index.js b/src/views/Wallet/views/AccountSummary/components/AccountBalance/index.js index 24bdfb9d..684da2e9 100644 --- a/src/views/Wallet/views/AccountSummary/components/AccountBalance/index.js +++ b/src/views/Wallet/views/AccountSummary/components/AccountBalance/index.js @@ -1,26 +1,25 @@ /* @flow */ import React, { Component } from 'react'; -import styled, { css } from 'styled-components'; +import BigNumber from 'bignumber.js'; +import styled from 'styled-components'; import Icon from 'components/Icon'; import colors from 'config/colors'; import ICONS from 'config/icons'; import { FONT_SIZE, FONT_WEIGHT } from 'config/variables'; -import BigNumber from 'bignumber.js'; - import type { Coin } from 'reducers/LocalStorageReducer'; import type { Props as BaseProps } from '../../Container'; type Props = { - // coin: $PropertyType<$ElementType, 'coin'>, coin: Coin, - summary: $ElementType, balance: string, - network: string, fiat: $ElementType, - onToggle: $ElementType } +type State = { + isHidden: boolean, +}; + const Wrapper = styled.div` padding: 0 48px 25px; position: relative; @@ -72,106 +71,60 @@ const BalanceWrapper = styled.div` margin-right: 48px; `; -class AccountBalance extends Component { - constructor(props) { +class AccountBalance extends Component { + constructor(props: Props) { super(props); this.state = { isHidden: false, }; } - shouldHide(hide) { - this.setState({ - isHidden: hide, - }); + handleHideBallanceIconClick() { + this.setState(previousState => ({ isHidden: !previousState.isHidden })); } render() { - const selectedCoin = props.coin; - const fiatRate = props.fiat.find(f => f.network === selectedCoin.network); + const selectedCoin = this.props.coin; + const fiatRate: any = this.props.fiat.find(f => f.network === selectedCoin.network); - const accountBalance = new BigNumber(props.balance); + const accountBalance = new BigNumber(this.props.balance); const fiatRateValue = new BigNumber(fiatRate.value); const fiat = accountBalance.times(fiatRateValue).toFixed(2); return ( - + + this.handleHideBallanceIconClick()} + > + + + {!this.state.isHidden && ( + + + + {fiatRate && ( + ${fiat} + )} + {this.props.balance} {selectedCoin.symbol} + + {fiatRate && ( + + + ${fiatRateValue.toFixed(2)} + 1.00 {selectedCoin.symbol} + + )} + + )} + ); } } - -const AccountBalance = (props: Props): ?React$Element => { - if (!props.summary.details) { - return ( - - - - - - ); - } - - const selectedCoin = props.coin; - const fiatRate = props.fiat.find(f => f.network === selectedCoin.network); - - const accountBalance = new BigNumber(props.balance); - const fiatRateValue = new BigNumber(fiatRate.value); - const fiat = accountBalance.times(fiatRateValue).toFixed(2); - - return ( - - {props.summary.details && ( - - - - - - - - {fiatRate && ( - ${fiat} - )} - {props.balance} {selectedCoin.symbol} - - {fiatRate && ( - - - ${fiatRateValue.toFixed(2)} - 1.00 {selectedCoin.symbol} - - )} - - )} - - {!props.summary.details && ( - - - - )} - - ); -}; - -export default AccountBalance; \ No newline at end of file +export default AccountBalance; diff --git a/src/views/Wallet/views/AccountSummary/components/Details/index.js b/src/views/Wallet/views/AccountSummary/components/Details/index.js deleted file mode 100644 index 450b3141..00000000 --- a/src/views/Wallet/views/AccountSummary/components/Details/index.js +++ /dev/null @@ -1,75 +0,0 @@ -/* @flow */ - - -import React from 'react'; -import BigNumber from 'bignumber.js'; - -import type { Coin } from 'reducers/LocalStorageReducer'; -import type { Props as BaseProps } from '../../Container'; - -type Props = { - // coin: $PropertyType<$ElementType, 'coin'>, - coin: Coin, - summary: $ElementType, - balance: string, - network: string, - fiat: $ElementType, - onToggle: $ElementType -} - -const SummaryDetails = (props: Props): ?React$Element => { - if (!props.summary.details) { - return ( -
-
-
- ); - } - - const selectedCoin = props.coin; - const fiatRate = props.fiat.find(f => f.network === selectedCoin.network); - - let balanceColumn = null; - let rateColumn = null; - - if (fiatRate) { - const accountBalance = new BigNumber(props.balance); - const fiatValue = new BigNumber(fiatRate.value); - const fiat = accountBalance.times(fiatValue).toFixed(2); - - balanceColumn = ( -
-
Balance
-
${ fiat }
-
{ props.balance } { selectedCoin.symbol }
-
- ); - - rateColumn = ( -
-
Rate
-
${ fiatValue.toFixed(2) }
-
1.00 { selectedCoin.symbol }
-
- ); - } else { - balanceColumn = ( -
-
Balance
-
{ props.balance } { selectedCoin.symbol }
-
- ); - } - - return ( -
-
-
- { balanceColumn } - { rateColumn } -
-
- ); -}; - -export default SummaryDetails; \ No newline at end of file diff --git a/src/views/Wallet/views/AccountSummary/index.js b/src/views/Wallet/views/AccountSummary/index.js index 2f067fd5..293c36f3 100644 --- a/src/views/Wallet/views/AccountSummary/index.js +++ b/src/views/Wallet/views/AccountSummary/index.js @@ -14,7 +14,7 @@ import CoinLogo from 'components/CoinLogo'; import * as stateUtils from 'reducers/utils'; import SelectedAccount from 'views/Wallet/components/SelectedAccount'; import Link from 'components/Link'; -import SummaryDetails from './components/Details'; +import AccountBalance from './components/AccountBalance'; import SummaryTokens from './components/Tokens'; import type { Props } from './Container'; @@ -83,7 +83,7 @@ const Summary = (props: Props) => { -