-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddTokenController.js
68 lines (62 loc) · 2.62 KB
/
AddTokenController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from 'react';
import { FormattedMessage, injectIntl } from 'react-intl';
import { PopupAPI } from '@tronlink/lib/api';
import Toast, { T } from 'react-toast-mobile';
import TronWeb from 'tronweb';
class AddTokenController extends React.Component {
constructor(props) {
super(props);
this.state = {
address: {
value: '',
valid: false
}
};
}
async addToken(address) {
if(!this.state.address.valid)
return;
const { smart } = this.props.tokens;
const { formatMessage } = this.props.intl;
const token = await PopupAPI.getSmartToken(address);
if(!token) {
T.notify(formatMessage({ id: 'ERRORS.INVALID_TOKEN' }));
return;
}
if(Object.keys(smart).some((key) => key === address)) {
T.notify(formatMessage({ id: 'ERRORS.TOKEN_ADDED' }));
return;
}
await PopupAPI.addSmartToken(address, token.name, token.symbol, token.decimals);
T.notify(formatMessage({ id: 'TOAST.ADDED' }));
}
render() {
const { formatMessage } = this.props.intl;
const { onCancel } = this.props;
return (
<div className='insetContainer send' onClick={ () => { this.setState({ isOpen: {account: false, token: false } }); } }>
<div className='pageHeader'>
<div className='back' onClick={onCancel}> </div>
<FormattedMessage id='MENU.ADD_TRC20_TOKEN'/>
</div>
<div className='greyModal'>
<Toast />
<div className='input-group'>
<div className='input'>
<input type='text' onChange={ (e) => {
const value = e.target.value;
this.state.address.value = value;
this.state.address.valid = TronWeb.isAddress(value);
this.setState({ address: this.state.address });
}} placeholder={formatMessage({ id: 'MENU.ADD_TRC20_TOKEN.INPUT_PLACE_HOLDER' })} />
</div>
</div>
<button onClick={ () => this.addToken(this.state.address.value) } className={('customButton primary addToken') + ( this.state.address.valid ? ' is-valid' : ' is-invalid') }>
<FormattedMessage id='BUTTON.ADD_TOKEN' />
</button>
</div>
</div>
);
}
}
export default injectIntl(AddTokenController);