From 0e7a2ef2e8888d47328ea1146f735aec315eae97 Mon Sep 17 00:00:00 2001 From: HenryNguyen5 Date: Sat, 12 May 2018 23:58:58 -0400 Subject: [PATCH] Automatically track custom tokens (#1769) --- .../BalanceSidebar/TokenBalances/Balances.tsx | 24 ++++++++++++++++--- .../BalanceSidebar/TokenBalances/TokenRow.tsx | 13 ++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/common/components/BalanceSidebar/TokenBalances/Balances.tsx b/common/components/BalanceSidebar/TokenBalances/Balances.tsx index c951f2d8..c21f35d1 100644 --- a/common/components/BalanceSidebar/TokenBalances/Balances.tsx +++ b/common/components/BalanceSidebar/TokenBalances/Balances.tsx @@ -20,7 +20,7 @@ interface TrackedTokens { } interface State { - trackedTokens: { [symbol: string]: boolean }; + trackedTokens: TrackedTokens; showCustomTokenForm: boolean; } export default class TokenBalances extends React.PureComponent { @@ -32,7 +32,7 @@ export default class TokenBalances extends React.PureComponent { public UNSAFE_componentWillReceiveProps(nextProps: Props) { if (nextProps.tokenBalances !== this.props.tokenBalances) { const trackedTokens = nextProps.tokenBalances.reduce((prev, t) => { - prev[t.symbol] = !t.balance.isZero(); + prev[t.symbol] = !t.balance.isZero() || t.custom; return prev; }, {}); this.setState({ trackedTokens }); @@ -45,7 +45,7 @@ export default class TokenBalances extends React.PureComponent { let bottom; let help; - if (tokenBalances.length && !hasSavedWalletTokens) { + if (tokenBalances.length && !hasSavedWalletTokens && !this.onlyCustomTokens()) { help = 'Select which tokens you would like to keep track of'; bottom = (
@@ -134,6 +134,24 @@ export default class TokenBalances extends React.PureComponent { }); }; + /** + * + * @description Checks if all currently tracked tokens are custom + * @private + * @returns + * @memberof TokenBalances + */ + private onlyCustomTokens() { + const tokenMap = this.props.tokenBalances.reduce<{ [key: string]: TokenBalance }>( + (acc, cur) => ({ ...acc, [cur.symbol]: cur }), + {} + ); + + return Object.keys(this.state.trackedTokens).reduce( + (prev, tokenName) => tokenMap[tokenName].custom && prev, + true + ); + } private addCustomToken = (token: Token) => { this.props.onAddCustomToken(token); this.setState({ showCustomTokenForm: false }); diff --git a/common/components/BalanceSidebar/TokenBalances/TokenRow.tsx b/common/components/BalanceSidebar/TokenBalances/TokenRow.tsx index f93cdff0..8fccce3b 100644 --- a/common/components/BalanceSidebar/TokenBalances/TokenRow.tsx +++ b/common/components/BalanceSidebar/TokenBalances/TokenRow.tsx @@ -30,11 +30,14 @@ export default class TokenRow extends React.PureComponent { return ( - {this.props.toggleTracked && ( - - - - )} + {/* Only allow to toggle tracking on non custom tokens + because the user can just remove the custom token instead */} + {!this.props.custom && + this.props.toggleTracked && ( + + + + )}