Add new Componennt for Token Images (#4496) (#4498)

This commit is contained in:
Nicolas Gotchac
2017-02-09 17:41:27 +01:00
committed by Jaco Greeff
parent 48eac51c8a
commit 71f84067d9
11 changed files with 225 additions and 176 deletions

View File

@@ -65,7 +65,7 @@ describe('ui/AccountCard', () => {
let balance;
beforeEach(() => {
balance = component.find('Connect(Balance)');
balance = component.find('Balance');
});
it('renders the balance', () => {

View File

@@ -17,13 +17,12 @@
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
};
@@ -31,7 +30,6 @@ class Balance extends Component {
static propTypes = {
balance: PropTypes.object,
className: PropTypes.string,
images: PropTypes.object.isRequired,
showOnlyEth: PropTypes.bool,
showZeroValues: PropTypes.bool
};
@@ -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;
@@ -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>
@@ -125,14 +109,3 @@ class Balance extends Component {
);
}
}
function mapStateToProps (state) {
const { images } = state;
return { images };
}
export default connect(
mapStateToProps,
null
)(Balance);

View File

@@ -16,7 +16,6 @@
import { shallow } from 'enzyme';
import React from 'react';
import sinon from 'sinon';
import apiutil from '~/api/util';
@@ -32,7 +31,6 @@ const BALANCE = {
let api;
let component;
let store;
function createApi () {
api = {
@@ -43,36 +41,22 @@ function createApi () {
return api;
}
function createStore () {
store = {
dispatch: sinon.stub(),
subscribe: sinon.stub(),
getState: () => {
return {
images: {}
};
}
};
return store;
}
function render (props = {}) {
if (!props.balance) {
props.balance = BALANCE;
}
const api = createApi();
component = shallow(
<Balance
className='testClass'
{ ...props }
/>,
{
context: {
store: createStore()
}
context: { api }
}
).find('Balance').shallow({ context: { api: createApi() } });
);
return component;
}
@@ -91,18 +75,18 @@ describe('ui/Balance', () => {
});
it('renders all the non-zero balances', () => {
expect(component.find('img')).to.have.length(2);
expect(component.find('Connect(TokenImage)')).to.have.length(2);
});
describe('render specifiers', () => {
it('renders only the single token with showOnlyEth', () => {
render({ showOnlyEth: true });
expect(component.find('img')).to.have.length(1);
expect(component.find('Connect(TokenImage)')).to.have.length(1);
});
it('renders all the tokens with showZeroValues', () => {
render({ showZeroValues: true });
expect(component.find('img')).to.have.length(3);
expect(component.find('Connect(TokenImage)')).to.have.length(3);
});
it('shows ETH with zero value with showOnlyEth & showZeroValues', () => {
@@ -116,7 +100,7 @@ describe('ui/Balance', () => {
]
}
});
expect(component.find('img')).to.have.length(1);
expect(component.find('Connect(TokenImage)')).to.have.length(1);
});
});
});

View File

@@ -0,0 +1,17 @@
// 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/>.
export default from './tokenImage';

View File

@@ -0,0 +1,72 @@
// 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 React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import unknownImage from '~/../assets/images/contracts/unknown-64x64.png';
class TokenImage extends Component {
static contextTypes = {
api: PropTypes.object
};
static propTypes = {
image: PropTypes.string,
token: PropTypes.shape({
image: PropTypes.string,
address: PropTypes.string
}).isRequired
};
render () {
const { api } = this.context;
const { image, token } = this.props;
const imageurl = token.image || image;
let imagesrc = unknownImage;
if (imageurl) {
const host = /^(\/)?api/.test(imageurl)
? api.dappsUrl
: '';
imagesrc = `${host}${imageurl}`;
}
return (
<img
src={ imagesrc }
alt={ token.name }
/>
);
}
}
function mapStateToProps (iniState) {
const { images } = iniState;
return (_, props) => {
const { token } = props;
return { image: images[token.address] };
};
}
export default connect(
mapStateToProps,
null
)(TokenImage);