From f76b81aaf98e8d81e0ac4e8ad7446fc4f8f84e2d Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 11 Jul 2018 13:23:00 -0400 Subject: [PATCH] and ens demo --- embark_demo/app/components/ens.js | 121 ++++++++++++++++++++++++++++++ embark_demo/app/dapp.css | 6 +- embark_demo/app/dapp.js | 28 ++++--- 3 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 embark_demo/app/components/ens.js diff --git a/embark_demo/app/components/ens.js b/embark_demo/app/components/ens.js new file mode 100644 index 0000000..4423f6c --- /dev/null +++ b/embark_demo/app/components/ens.js @@ -0,0 +1,121 @@ +import EmbarkJS from 'Embark/EmbarkJS'; +import React from 'react'; +import { Alert, Form, FormGroup, FormControl, Button } from 'react-bootstrap'; + +window.EmbarkJS = EmbarkJS; + +class ENS extends React.Component { + + constructor(props) { + super(props); + + this.state = { + valueResolve: 'ethereumfoundation.eth', + responseResolve: null, + isResolveError: false, + valueLookup: '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359', + responseLookup: null, + isLookupError: false, + embarkLogs: [] + } + } + + handleChange(stateName, e) { + this.setState({ [stateName]: e.target.value }); + } + + 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({ + responseResolve: err, + 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({ + responseLookup: err, + isLookupError: true + }); + } + this.setState({ + responseLookup: result, + isLookupError: false + }); + }); + } + + render() { + return ( + { + !this.props.enabled ? + + ENS provider might not be set + : '' + } +

Resolve a name

+
+ + {this.state.responseResolve && + + Resolved address: {this.state.responseResolve} + } + this.handleChange('valueResolve', e)}/> + + +
+ +

Lookup an address

+
+ + {this.state.responseLookup && + + Looked up domain: {this.state.responseLookup} + } + this.handleChange('valueLookup', e)}/> + + +
+ +

Embark Calls

+

Javascript calls being made:

+
+ { + this.state.embarkLogs.map((item, i) =>

{item}

) + } +
+
+ ); + } +} + +export default ENS; diff --git a/embark_demo/app/dapp.css b/embark_demo/app/dapp.css index 3d35569..401124e 100644 --- a/embark_demo/app/dapp.css +++ b/embark_demo/app/dapp.css @@ -50,4 +50,8 @@ div { input.form-control { margin-right: 5px; -} \ No newline at end of file +} + +.alert-result { + margin-left: 0; +} diff --git a/embark_demo/app/dapp.js b/embark_demo/app/dapp.js index a598da2..a220866 100644 --- a/embark_demo/app/dapp.js +++ b/embark_demo/app/dapp.js @@ -6,6 +6,7 @@ import EmbarkJS from 'Embark/EmbarkJS'; import Blockchain from './components/blockchain'; import Whisper from './components/whisper'; import Storage from './components/storage'; +import ENS from './components/ens'; import './dapp.css'; @@ -16,24 +17,25 @@ class App extends React.Component { this.state = { whisperEnabled: false, - storageEnabled: false + storageEnabled: false, + ensEnabled: false } } - componentDidMount(){ + componentDidMount(){ EmbarkJS.onReady(() => { if (EmbarkJS.isNewWeb3()) { - EmbarkJS.Messages.Providers.whisper.getWhisperVersion((err, version) => { + EmbarkJS.Messages.Providers.whisper.getWhisperVersion((err, version) => { if(!err) - this.setState({whisperEnabled: true}) - else - console.log(err); + this.setState({whisperEnabled: true}); + else + console.log(err); }); } else { if (EmbarkJS.Messages.providerName === 'whisper') { EmbarkJS.Messages.getWhisperVersion((err, version) => { if(!err) - this.setState({whisperEnabled: true}) + this.setState({whisperEnabled: true}); else console.log(err); }); @@ -41,7 +43,8 @@ class App extends React.Component { } this.setState({ - storageEnabled: true + storageEnabled: true, + ensEnabled: EmbarkJS.Names.isAvailable() }); }); } @@ -50,12 +53,12 @@ class App extends React.Component { _renderStatus(title, available){ let className = available ? 'pull-right status-online' : 'pull-right status-offline'; return - {title} + {title} ; } - render(){ + render() { return (

Embark - Usage Example

@@ -64,9 +67,12 @@ class App extends React.Component { - + + + +
); }