Automatically track custom tokens (#1769)

This commit is contained in:
HenryNguyen5 2018-05-12 23:58:58 -04:00 committed by Daniel Ternyak
parent 5baa356ee1
commit 0e7a2ef2e8
2 changed files with 29 additions and 8 deletions

View File

@ -20,7 +20,7 @@ interface TrackedTokens {
} }
interface State { interface State {
trackedTokens: { [symbol: string]: boolean }; trackedTokens: TrackedTokens;
showCustomTokenForm: boolean; showCustomTokenForm: boolean;
} }
export default class TokenBalances extends React.PureComponent<Props, State> { export default class TokenBalances extends React.PureComponent<Props, State> {
@ -32,7 +32,7 @@ export default class TokenBalances extends React.PureComponent<Props, State> {
public UNSAFE_componentWillReceiveProps(nextProps: Props) { public UNSAFE_componentWillReceiveProps(nextProps: Props) {
if (nextProps.tokenBalances !== this.props.tokenBalances) { if (nextProps.tokenBalances !== this.props.tokenBalances) {
const trackedTokens = nextProps.tokenBalances.reduce<TrackedTokens>((prev, t) => { const trackedTokens = nextProps.tokenBalances.reduce<TrackedTokens>((prev, t) => {
prev[t.symbol] = !t.balance.isZero(); prev[t.symbol] = !t.balance.isZero() || t.custom;
return prev; return prev;
}, {}); }, {});
this.setState({ trackedTokens }); this.setState({ trackedTokens });
@ -45,7 +45,7 @@ export default class TokenBalances extends React.PureComponent<Props, State> {
let bottom; let bottom;
let help; let help;
if (tokenBalances.length && !hasSavedWalletTokens) { if (tokenBalances.length && !hasSavedWalletTokens && !this.onlyCustomTokens()) {
help = 'Select which tokens you would like to keep track of'; help = 'Select which tokens you would like to keep track of';
bottom = ( bottom = (
<div className="TokenBalances-buttons"> <div className="TokenBalances-buttons">
@ -134,6 +134,24 @@ export default class TokenBalances extends React.PureComponent<Props, State> {
}); });
}; };
/**
*
* @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) => { private addCustomToken = (token: Token) => {
this.props.onAddCustomToken(token); this.props.onAddCustomToken(token);
this.setState({ showCustomTokenForm: false }); this.setState({ showCustomTokenForm: false });

View File

@ -30,7 +30,10 @@ export default class TokenRow extends React.PureComponent<Props, State> {
return ( return (
<tr className="TokenRow" onClick={this.handleToggleTracked}> <tr className="TokenRow" onClick={this.handleToggleTracked}>
{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 && (
<td className="TokenRow-toggled"> <td className="TokenRow-toggled">
<input type="checkbox" checked={tracked} /> <input type="checkbox" checked={tracked} />
</td> </td>