embark/templates/demo/app/components/ens.js

181 lines
5.6 KiB
JavaScript
Raw Normal View History

2018-07-23 21:11:41 +00:00
/*global web3*/
2018-07-11 17:11:55 +00:00
import EmbarkJS from 'Embark/EmbarkJS';
import React from 'react';
2018-07-23 21:11:41 +00:00
import { Alert, Form, FormGroup, FormControl, Button } from 'react-bootstrap';
2018-07-11 17:11:55 +00:00
window.EmbarkJS = EmbarkJS;
class ENS extends React.Component {
constructor(props) {
super(props);
this.state = {
2018-08-23 09:23:52 +00:00
valueResolve: 'eth',
2018-07-11 17:11:55 +00:00
responseResolve: null,
isResolveError: false,
2018-07-23 21:11:41 +00:00
valueLookup: '',
2018-07-11 17:11:55 +00:00
responseLookup: null,
isLookupError: false,
2018-07-23 21:11:41 +00:00
valueRegister: '',
addressRegister: '',
responseRegister: null,
isRegisterError: false,
2018-07-11 17:11:55 +00:00
embarkLogs: []
};
}
componentDidMount() {
2018-07-23 21:11:41 +00:00
EmbarkJS.onReady(() => {
if (!web3.eth.defaultAccount) {
this.setState({
globalError: 'There is currently no default account. If Metamask is active, please sign in or deactivate it.'
});
}
2018-07-23 21:11:41 +00:00
this.setState({
addressRegister: web3.eth.defaultAccount,
valueLookup: web3.eth.defaultAccount
})
});
}
2018-07-11 17:11:55 +00:00
handleChange(stateName, e) {
2018-07-23 21:11:41 +00:00
this.setState({ [stateName]: e.target.value });
}
checkEnter(e, func) {
if (e.key !== 'Enter') {
return;
}
e.preventDefault();
func.apply(this, [e]);
}
2018-07-23 21:11:41 +00:00
registerSubDomain(e) {
e.preventDefault();
const self = this;
const embarkLogs = this.state.embarkLogs;
embarkLogs.push(`EmbarkJS.Names.registerSubDomain('${this.state.valueRegister}', '${this.state.addressRegister}', console.log)`);
this.setState({
embarkLogs: embarkLogs
});
EmbarkJS.Names.registerSubDomain(this.state.valueRegister, this.state.addressRegister, (err, transaction) => {
const message = err ? err : `Successfully registered "${this.state.valueRegister}" with ${transaction.gasUsed} gas`;
self.setState({
responseRegister: message,
isRegisterError: !!err
});
});
2018-07-11 17:11:55 +00:00
}
resolveName(e) {
e.preventDefault();
const embarkLogs = this.state.embarkLogs;
embarkLogs.push(`EmbarkJS.Names.resolve('${this.state.valueResolve}', console.log)`);
this.setState({
embarkLogs: embarkLogs
});
EmbarkJS.Names.resolve(this.state.valueResolve, (err, result) => {
if (err) {
return this.setState({
2018-07-23 21:11:41 +00:00
responseResolve: err.message || err,
2018-07-11 17:11:55 +00:00
isResolveError: true
});
}
this.setState({
responseResolve: result,
isResolveError: false
});
});
}
lookupAddress(e) {
e.preventDefault();
const embarkLogs = this.state.embarkLogs;
embarkLogs.push(`EmbarkJS.Names.resolve('${this.state.valueLookup}', console.log)`);
this.setState({
embarkLogs: embarkLogs
});
EmbarkJS.Names.lookup(this.state.valueLookup, (err, result) => {
if (err) {
return this.setState({
2018-07-23 21:11:41 +00:00
responseLookup: err.message || err,
2018-07-11 17:11:55 +00:00
isLookupError: true
});
}
this.setState({
responseLookup: result,
isLookupError: false
});
});
}
render() {
return (<React.Fragment>
{this.state.globalError && <Alert bsStyle="danger">{this.state.globalError}</Alert>}
2018-07-11 17:11:55 +00:00
<h3>Resolve a name</h3>
<Form inline onKeyDown={(e) => this.checkEnter(e, this.resolveName)}>
2018-07-11 17:11:55 +00:00
<FormGroup>
{this.state.responseResolve &&
<Alert className="alert-result" bsStyle={this.state.isResolveError ? 'danger' : 'success'}>
Resolved address: <span className="value">{this.state.responseResolve}</span>
</Alert>}
<FormControl
type="text"
defaultValue={this.state.valueResolve}
onChange={(e) => this.handleChange('valueResolve', e)}/>
<Button bsStyle="primary" onClick={(e) => this.resolveName(e)}>Resolve name</Button>
</FormGroup>
</Form>
<h3>Lookup an address</h3>
<Form inline onKeyDown={(e) => this.checkEnter(e, this.lookupAddress)}>
2018-07-11 17:11:55 +00:00
<FormGroup>
{this.state.responseLookup &&
<Alert className="alert-result" bsStyle={this.state.isLookupError ? 'danger' : 'success'}>
Looked up domain: <span className="value">{this.state.responseLookup}</span>
</Alert>}
<FormControl
type="text"
defaultValue={this.state.valueLookup}
onChange={(e) => this.handleChange('valueLookup', e)}/>
<Button bsStyle="primary" onClick={(e) => this.lookupAddress(e)}>Lookup address</Button>
</FormGroup>
</Form>
2018-08-29 09:22:43 +00:00
<h3>Register subdomain for eth</h3>
<Form inline onKeyDown={(e) => this.checkEnter(e, this.registerSubDomain)}>
2018-07-23 21:11:41 +00:00
<FormGroup>
{this.state.responseRegister &&
<Alert className="alert-result" bsStyle={this.state.isRegisterError ? 'danger' : 'success'}>
<span className="value">{this.state.responseRegister}</span>
</Alert>}
<FormControl
type="text"
defaultValue={this.state.valueRegister}
onChange={(e) => this.handleChange('valueRegister', e)}/>
<FormControl
type="text"
defaultValue={this.state.addressRegister}
onChange={(e) => this.handleChange('addressRegister', e)}/>
<Button bsStyle="primary" onClick={(e) => this.registerSubDomain(e)}>Register subdomain</Button>
</FormGroup>
</Form>
2018-07-11 17:11:55 +00:00
<h3>Embark Calls </h3>
<p>Javascript calls being made: </p>
<div className="logs">
{
this.state.embarkLogs.map((item, i) => <p key={i}>{item}</p>)
}
</div>
</React.Fragment>
);
}
}
export default ENS;