show token placeholder if there was an error fetching the image

This commit is contained in:
Mikhail Mikheev 2019-03-22 15:49:11 +04:00
parent beff6cfdd6
commit 67871312a4
3 changed files with 38 additions and 27 deletions

View File

@ -1,6 +1,6 @@
// @flow
import React from 'react'
import classNames from 'classnames/bind'
import React, { PureComponent } from 'react'
import styles from './index.scss'
const cx = classNames.bind(styles)
@ -10,24 +10,15 @@ type Props = {
fullwidth?: boolean,
bordered?: boolean,
className?: string,
style?: React$Node
style?: React$Node,
}
class Img extends PureComponent<Props> {
render() {
const {
fullwidth, alt, bordered, className, style, ...props
} = this.props
const Img = ({
fullwidth, alt, bordered, className, style, ...props
}: Props) => {
const classes = cx(styles.img, { fullwidth, bordered }, className)
return (
<img
alt={alt}
style={style}
{...props}
className={cx(styles.img, { fullwidth, bordered }, className)}
/>
)
}
return <img alt={alt} style={style} className={classes} {...props} />
}
export default Img

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 B

View File

@ -24,6 +24,7 @@ import Spacer from '~/components/Spacer'
import Row from '~/components/layout/Row'
import { type Token } from '~/logic/tokens/store/model/token'
import actions, { type Actions } from './actions'
import TokenPlaceholder from './assets/token_placeholder.png'
import { styles } from './style'
type Props = Actions & {
@ -37,10 +38,17 @@ type State = {
filter: string,
}
const filterBy = (filter: string, tokens: List<Token>): List<Token> => tokens.filter((token: Token) => !filter
|| token.get('symbol').toLowerCase().includes(filter.toLowerCase())
|| token.get('name').toLowerCase().includes(filter.toLowerCase()))
const filterBy = (filter: string, tokens: List<Token>): List<Token> => tokens.filter(
(token: Token) => !filter
|| token
.get('symbol')
.toLowerCase()
.includes(filter.toLowerCase())
|| token
.get('name')
.toLowerCase()
.includes(filter.toLowerCase()),
)
class Tokens extends React.Component<Props, State> {
state = {
@ -68,6 +76,11 @@ class Tokens extends React.Component<Props, State> {
disableToken(safeAddress, token)
}
setImageToPlaceholder = (e) => {
e.target.onerror = null
e.target.src = TokenPlaceholder
}
render() {
const { onClose, classes, tokens } = this.props
const { filter } = this.state
@ -84,7 +97,9 @@ class Tokens extends React.Component<Props, State> {
<React.Fragment>
<Block className={classes.root}>
<Row align="center" grow className={classes.heading}>
<Paragraph className={classes.manage} noMargin>Manage Tokens</Paragraph>
<Paragraph className={classes.manage} noMargin>
Manage Tokens
</Paragraph>
<IconButton onClick={onClose} disableRipple>
<Close className={classes.close} />
</IconButton>
@ -112,14 +127,16 @@ class Tokens extends React.Component<Props, State> {
{filteredTokens.map((token: Token) => (
<ListItem key={token.get('address')} className={classes.token}>
<ListItemIcon>
<Img src={token.get('logoUri')} height={28} alt={token.get('name')} />
<Img
src={token.get('logoUri')}
height={28}
alt={token.get('name')}
onError={this.setImageToPlaceholder}
/>
</ListItemIcon>
<ListItemText primary={token.get('symbol')} secondary={token.get('name')} />
<ListItemSecondaryAction>
<Switch
onChange={this.onSwitch(token)}
checked={token.get('status')}
/>
<Switch onChange={this.onSwitch(token)} checked={token.get('status')} />
</ListItemSecondaryAction>
</ListItem>
))}
@ -131,4 +148,7 @@ class Tokens extends React.Component<Props, State> {
const TokenComponent = withStyles(styles)(Tokens)
export default connect(undefined, actions)(TokenComponent)
export default connect(
undefined,
actions,
)(TokenComponent)