Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Add new Componennt for Token Images (#4496)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngotchac committed Feb 9, 2017
1 parent 656c089 commit cf0fe4d
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 176 deletions.
109 changes: 4 additions & 105 deletions js/src/modals/Transfer/Details/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import BigNumber from 'bignumber.js';
import React, { Component, PropTypes } from 'react';
import { Checkbox, MenuItem } from 'material-ui';
import { isEqual } from 'lodash';
import { Checkbox } from 'material-ui';

import Form, { Input, InputAddressSelect, AddressSelect, Select } from '~/ui/Form';
import Form, { Input, InputAddressSelect, AddressSelect } from '~/ui/Form';
import { nullableProptype } from '~/util/proptypes';

import imageUnknown from '../../../../assets/images/contracts/unknown-64x64.png';
import TokenSelect from './tokenSelect';
import styles from '../transfer.css';

const CHECK_STYLE = {
Expand All @@ -31,110 +29,12 @@ const CHECK_STYLE = {
left: '1em'
};

class TokenSelect extends Component {
static contextTypes = {
api: PropTypes.object
}

static propTypes = {
onChange: PropTypes.func.isRequired,
balance: PropTypes.object.isRequired,
images: PropTypes.object.isRequired,
tag: PropTypes.string.isRequired
};

componentWillMount () {
this.computeTokens();
}

componentWillReceiveProps (nextProps) {
const prevTokens = this.props.balance.tokens.map((t) => `${t.token.tag}_${t.value.toNumber()}`);
const nextTokens = nextProps.balance.tokens.map((t) => `${t.token.tag}_${t.value.toNumber()}`);

if (!isEqual(prevTokens, nextTokens)) {
this.computeTokens(nextProps);
}
}

computeTokens (props = this.props) {
const { api } = this.context;
const { balance, images } = this.props;

const items = balance.tokens
.filter((token, index) => !index || token.value.gt(0))
.map((balance, index) => {
const token = balance.token;
const isEth = index === 0;
let imagesrc = token.image;

if (!imagesrc) {
imagesrc =
images[token.address]
? `${api.dappsUrl}${images[token.address]}`
: imageUnknown;
}
let value = 0;

if (isEth) {
value = api.util.fromWei(balance.value).toFormat(3);
} else {
const format = balance.token.format || 1;
const decimals = format === 1 ? 0 : Math.min(3, Math.floor(format / 10));

value = new BigNumber(balance.value).div(format).toFormat(decimals);
}

const label = (
<div className={ styles.token }>
<img src={ imagesrc } />
<div className={ styles.tokenname }>
{ token.name }
</div>
<div className={ styles.tokenbalance }>
{ value }<small> { token.tag }</small>
</div>
</div>
);

return (
<MenuItem
key={ `${index}_${token.tag}` }
value={ token.tag }
label={ label }
>
{ label }
</MenuItem>
);
});

this.setState({ items });
}

render () {
const { tag, onChange } = this.props;
const { items } = this.state;

return (
<Select
className={ styles.tokenSelect }
label='type of token transfer'
hint='type of token to transfer'
value={ tag }
onChange={ onChange }
>
{ items }
</Select>
);
}
}

export default class Details extends Component {
static propTypes = {
address: PropTypes.string,
balance: PropTypes.object,
all: PropTypes.bool,
extras: PropTypes.bool,
images: PropTypes.object.isRequired,
sender: PropTypes.string,
senderError: PropTypes.string,
sendersBalances: PropTypes.object,
Expand Down Expand Up @@ -249,12 +149,11 @@ export default class Details extends Component {
}

renderTokenSelect () {
const { balance, images, tag } = this.props;
const { balance, tag } = this.props;

return (
<TokenSelect
balance={ balance }
images={ images }
tag={ tag }
onChange={ this.onChangeToken }
/>
Expand Down
114 changes: 114 additions & 0 deletions js/src/modals/Transfer/Details/tokenSelect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import BigNumber from 'bignumber.js';
import React, { Component, PropTypes } from 'react';
import { MenuItem } from 'material-ui';
import { isEqual } from 'lodash';

import { Select } from '~/ui/Form';
import TokenImage from '~/ui/TokenImage';

import styles from '../transfer.css';

export default class TokenSelect extends Component {
static contextTypes = {
api: PropTypes.object
};

static propTypes = {
onChange: PropTypes.func.isRequired,
balance: PropTypes.object.isRequired,
tag: PropTypes.string.isRequired
};

componentWillMount () {
this.computeTokens();
}

componentWillReceiveProps (nextProps) {
const prevTokens = this.props.balance.tokens.map((t) => `${t.token.tag}_${t.value.toNumber()}`);
const nextTokens = nextProps.balance.tokens.map((t) => `${t.token.tag}_${t.value.toNumber()}`);

if (!isEqual(prevTokens, nextTokens)) {
this.computeTokens(nextProps);
}
}

computeTokens (props = this.props) {
const { api } = this.context;
const { balance } = this.props;

const items = balance.tokens
.filter((token, index) => !index || token.value.gt(0))
.map((balance, index) => {
const token = balance.token;
const isEth = index === 0;

let value = 0;

if (isEth) {
value = api.util.fromWei(balance.value).toFormat(3);
} else {
const format = balance.token.format || 1;
const decimals = format === 1 ? 0 : Math.min(3, Math.floor(format / 10));

value = new BigNumber(balance.value).div(format).toFormat(decimals);
}

const label = (
<div className={ styles.token }>
<TokenImage token={ token } />
<div className={ styles.tokenname }>
{ token.name }
</div>
<div className={ styles.tokenbalance }>
{ value }<small> { token.tag }</small>
</div>
</div>
);

return (
<MenuItem
key={ `${index}_${token.tag}` }
value={ token.tag }
label={ label }
>
{ label }
</MenuItem>
);
});

this.setState({ items });
}

render () {
const { tag, onChange } = this.props;
const { items } = this.state;

return (
<Select
className={ styles.tokenSelect }
label='type of token transfer'
hint='type of token to transfer'
value={ tag }
onChange={ onChange }
>
{ items }
</Select>
);
}
}
4 changes: 1 addition & 3 deletions js/src/modals/Transfer/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class Transfer extends Component {
static propTypes = {
newError: PropTypes.func.isRequired,
gasLimit: PropTypes.object.isRequired,
images: PropTypes.object.isRequired,

senders: nullableProptype(PropTypes.object),
sendersBalances: nullableProptype(PropTypes.object),
Expand Down Expand Up @@ -174,7 +173,7 @@ class Transfer extends Component {
}

renderDetailsPage () {
const { account, balance, images, senders } = this.props;
const { account, balance, senders } = this.props;
const { recipient, recipientError, sender, senderError, sendersBalances } = this.store;
const { valueAll, extras, tag, total, totalError, value, valueError } = this.store;

Expand All @@ -184,7 +183,6 @@ class Transfer extends Component {
all={ valueAll }
balance={ balance }
extras={ extras }
images={ images }
onChange={ this.store.onUpdateDetails }
recipient={ recipient }
recipientError={ recipientError }
Expand Down
2 changes: 1 addition & 1 deletion js/src/ui/AccountCard/accountCard.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('ui/AccountCard', () => {
let balance;

beforeEach(() => {
balance = component.find('Connect(Balance)');
balance = component.find('Balance');
});

it('renders the balance', () => {
Expand Down
35 changes: 4 additions & 31 deletions js/src/ui/Balance/balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@
import BigNumber from 'bignumber.js';
import React, { Component, PropTypes } from 'react';
import { FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';

import unknownImage from '~/../assets/images/contracts/unknown-64x64.png';
import TokenImage from '~/ui/TokenImage';

import styles from './balance.css';

class Balance extends Component {
export default class Balance extends Component {
static contextTypes = {
api: PropTypes.object
};

static propTypes = {
balance: PropTypes.object,
className: PropTypes.string,
images: PropTypes.object.isRequired,
showOnlyEth: PropTypes.bool,
showZeroValues: PropTypes.bool
};
Expand All @@ -43,7 +41,7 @@ class Balance extends Component {

render () {
const { api } = this.context;
const { balance, className, images, showZeroValues, showOnlyEth } = this.props;
const { balance, className, showZeroValues, showOnlyEth } = this.props;

if (!balance || !balance.tokens) {
return null;
Expand Down Expand Up @@ -79,26 +77,12 @@ class Balance extends Component {
value = api.util.fromWei(balance.value).toFormat(3);
}

const imageurl = token.image || images[token.address];
let imagesrc = unknownImage;

if (imageurl) {
const host = /^(\/)?api/.test(imageurl)
? api.dappsUrl
: '';

imagesrc = `${host}${imageurl}`;
}

return (
<div
className={ styles.balance }
key={ `${index}_${token.tag}` }
>
<img
src={ imagesrc }
alt={ token.name }
/>
<TokenImage token={ token } />
<div className={ styles.balanceValue }>
<span title={ value }> { value } </span>
</div>
Expand All @@ -125,14 +109,3 @@ class Balance extends Component {
);
}
}

function mapStateToProps (state) {
const { images } = state;

return { images };
}

export default connect(
mapStateToProps,
null
)(Balance);
Loading

0 comments on commit cf0fe4d

Please sign in to comment.