From d82c5f93fc8408c8d82e6aa2fde9b71b69661aa6 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 14 Aug 2018 17:17:47 +0100 Subject: [PATCH] Display error + lint --- embark-ui/src/actions/index.js | 2 +- embark-ui/src/components/EnsRegister.js | 10 +++---- embark-ui/src/containers/EnsContainer.js | 8 +++--- embark-ui/src/reducers/index.js | 19 +++++++++++-- embark-ui/src/reducers/selectors.js | 4 +++ lib/modules/ens/ENSFunctions.js | 1 - lib/modules/ens/embarkjs.js | 2 +- lib/modules/ens/index.js | 34 +++++++++++++++++++----- 8 files changed, 61 insertions(+), 19 deletions(-) diff --git a/embark-ui/src/actions/index.js b/embark-ui/src/actions/index.js index 5414ef60..0442554e 100644 --- a/embark-ui/src/actions/index.js +++ b/embark-ui/src/actions/index.js @@ -143,7 +143,7 @@ export const ensRecord = { export const ENS_RECORDS = createRequestTypes('ENS_RECORDS'); export const ensRecords = { post: (subdomain, address) => action(ENS_RECORDS[REQUEST], {subdomain, address}), - success: (_record, payload) => action(ENS_RECORDS[SUCCESS], {ensRecords: [{subdomain: payload.subdomain, address: payload.address}]}), + success: (record) => action(ENS_RECORDS[SUCCESS], {ensRecords: [record]}), failure: (error) => action(ENS_RECORDS[FAILURE], {error}) }; diff --git a/embark-ui/src/components/EnsRegister.js b/embark-ui/src/components/EnsRegister.js index 63a40ee7..5024dc0a 100644 --- a/embark-ui/src/components/EnsRegister.js +++ b/embark-ui/src/components/EnsRegister.js @@ -30,11 +30,10 @@ class EnsRegister extends Component { } showResult() { - let ensRecord = this.props.ensRecords.find((record) => record.address === this.state.address && record.subdomain === this.state.subdomain); - if (ensRecord) { - return Successfully registered; + if (this.props.ensErrors) { + return An error happened: {this.props.ensErrors}; } else { - return An error happened; + return Successfully registered; } } @@ -65,7 +64,8 @@ class EnsRegister extends Component { EnsRegister.propTypes = { register: PropTypes.func, - ensRecords: PropTypes.arrayOf(PropTypes.object) + ensRecords: PropTypes.arrayOf(PropTypes.object), + ensErrors: PropTypes.string }; export default EnsRegister; diff --git a/embark-ui/src/containers/EnsContainer.js b/embark-ui/src/containers/EnsContainer.js index 68a15af8..630991f8 100644 --- a/embark-ui/src/containers/EnsContainer.js +++ b/embark-ui/src/containers/EnsContainer.js @@ -6,7 +6,7 @@ import {ensRecord, ensRecords} from "../actions"; import EnsRegister from "../components/EnsRegister"; import EnsLookup from "../components/EnsLookup"; import EnsResolve from "../components/EnsResolve"; -import {getEnsRecords, isEnsEnabled} from "../reducers/selectors"; +import {getEnsRecords, isEnsEnabled, getEnsErrors} from "../reducers/selectors"; class EnsContainer extends Component { @@ -15,7 +15,7 @@ class EnsContainer extends Component { - + ); } @@ -38,12 +38,14 @@ EnsContainer.propTypes = { resolve: PropTypes.func, lookup: PropTypes.func, register: PropTypes.func, - isEnsEnabled: PropTypes.bool + isEnsEnabled: PropTypes.bool, + ensErrors: PropTypes.string }; function mapStateToProps(state) { return { ensRecords: getEnsRecords(state), + ensErrors: getEnsErrors(state), isEnsEnabled: isEnsEnabled(state) }; } diff --git a/embark-ui/src/reducers/index.js b/embark-ui/src/reducers/index.js index 9a070114..64c9b6ef 100644 --- a/embark-ui/src/reducers/index.js +++ b/embark-ui/src/reducers/index.js @@ -1,5 +1,5 @@ import {combineReducers} from 'redux'; -import {REQUEST} from "../actions"; +import {REQUEST, SUCCESS} from "../actions"; const BN_FACTOR = 10000; const voidAddress = '0x0000000000000000000000000000000000000000'; @@ -95,6 +95,20 @@ function errorMessage(state = null, action) { return action.error || state; } +/* eslint multiline-ternary: "off" */ +function errorEntities(state = {}, action) { + if (!action.type.endsWith(SUCCESS)) { + return state; + } + let newState = {}; + for (let name of Object.keys(entitiesDefaultState)) { + if (action[name] && action[name].length > 0) { + newState[name] = action[name][0].error; + } + } + return {...state, ...newState}; +} + function loading(_state = false, action) { return action.type.endsWith(REQUEST); } @@ -102,7 +116,8 @@ function loading(_state = false, action) { const rootReducer = combineReducers({ entities, loading, - errorMessage + errorMessage, + errorEntities }); export default rootReducer; diff --git a/embark-ui/src/reducers/selectors.js b/embark-ui/src/reducers/selectors.js index aa0e8e43..5b51102c 100644 --- a/embark-ui/src/reducers/selectors.js +++ b/embark-ui/src/reducers/selectors.js @@ -98,6 +98,10 @@ export function getEnsRecords(state) { return state.entities.ensRecords; } +export function getEnsErrors(state) { + return state.errorEntities.ensRecords; +} + export function isEnsEnabled(state) { return Boolean(state.entities.plugins.find((plugin) => plugin.name === 'ens')); } diff --git a/lib/modules/ens/ENSFunctions.js b/lib/modules/ens/ENSFunctions.js index a73de580..bde805d7 100644 --- a/lib/modules/ens/ENSFunctions.js +++ b/lib/modules/ens/ENSFunctions.js @@ -104,4 +104,3 @@ if (typeof module !== 'undefined' && module.exports) { lookupAddress }; } -; diff --git a/lib/modules/ens/embarkjs.js b/lib/modules/ens/embarkjs.js index 227c35b9..e785255a 100644 --- a/lib/modules/ens/embarkjs.js +++ b/lib/modules/ens/embarkjs.js @@ -1,4 +1,4 @@ -/*global EmbarkJS, web3, lookupAddress, resolveName, registerSubDomain, namehash, reverseAddrSuffix*/ +/*global EmbarkJS, web3, lookupAddress, resolveName, registerSubDomain, reverseAddrSuffix*/ let __embarkENS = {}; diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index 5e39b53e..6434611e 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -201,7 +201,7 @@ class ENS { let self = this; const createInternalResolverContract = function(resolverAddress, callback) { - this.createResolverContract({resolverAbi: config.resolverAbi, resolverAddress}, callback) + this.createResolverContract({resolverAbi: config.resolverAbi, resolverAddress}, callback); }; self.embark.registerAPICall( @@ -233,7 +233,7 @@ class ENS { } ], function(error, name) { if (error) { - return res.send({error: error.message}); + return res.send({error: error || error.message}); } res.send({name}); }); @@ -244,18 +244,40 @@ class ENS { 'post', '/embark-api/ens/register', (req, res) => { - self.registerSubdomains({[req.body.subdomain]: req.body.address}, config, (error, transaction) => { + self.registerSubdomain(req.body.subdomain, req.body.address, config, (error) => { if (error) { - return res.send({error: error.message}); + return res.send({error: error || error.message}); } - res.send({transaction}); + res.send({name: `${req.body.subdomain}.${self.registration.rootDomain}`, address: req.body.address}); }); } ); } registerConfigSubdomains(config, callback) { - this.registerSubdomains(this.registration.subdomains, config, callback); + async.each(Object.keys(this.registration.subdomains), (subdomain, eachCb) => { + this.registerSubdomain(subdomain, this.registration.subdomains[subdomain], config, eachCb); + }, callback); + } + + registerSubdomain(subdomain, address, config, callback) { + const self = this; + self.events.request("blockchain:defaultAccount:get", (defaultAccount) => { + async.parallel({ + ens: self.createRegistryContract.bind(this, config), + registrar: self.createRegistrarContract.bind(this, config), + resolver: self.createResolverContract.bind(this, config) + }, function (err, contracts) { + if (err) { + return callback(err); + } + const {ens, registrar, resolver} = contracts; + + const reverseNode = utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix); + ENSFunctions.registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, + self.registration.rootDomain, reverseNode, address, self.logger, callback); + }); + }); } registerSubdomains(subdomains, config, callback) {