Add Email and Registry lookups to Address Selector (#3992)
* Move AccountSelect values to MobX store * Mail registry + small fixes * Add Registry to AddressSelect lookups + Nav fixes * Fix linting * PR Grumbles * Fix tests and propTypes
This commit is contained in:
parent
486f7ae684
commit
9814251a28
@ -22,7 +22,7 @@ import { ContextProvider, muiTheme } from '~/ui';
|
|||||||
|
|
||||||
import DetailsStep from './';
|
import DetailsStep from './';
|
||||||
|
|
||||||
import { STORE, CONTRACT } from '../executeContract.test.js';
|
import { createApi, STORE, CONTRACT } from '../executeContract.test.js';
|
||||||
|
|
||||||
let component;
|
let component;
|
||||||
let onAmountChange;
|
let onAmountChange;
|
||||||
@ -41,7 +41,7 @@ function render (props) {
|
|||||||
onValueChange = sinon.stub();
|
onValueChange = sinon.stub();
|
||||||
|
|
||||||
component = mount(
|
component = mount(
|
||||||
<ContextProvider api={ {} } muiTheme={ muiTheme } store={ STORE }>
|
<ContextProvider api={ createApi() } muiTheme={ muiTheme } store={ STORE }>
|
||||||
<DetailsStep
|
<DetailsStep
|
||||||
{ ...props }
|
{ ...props }
|
||||||
contract={ CONTRACT }
|
contract={ CONTRACT }
|
||||||
|
@ -20,7 +20,7 @@ import sinon from 'sinon';
|
|||||||
|
|
||||||
import ExecuteContract from './';
|
import ExecuteContract from './';
|
||||||
|
|
||||||
import { CONTRACT, STORE } from './executeContract.test.js';
|
import { createApi, CONTRACT, STORE } from './executeContract.test.js';
|
||||||
|
|
||||||
let component;
|
let component;
|
||||||
let onClose;
|
let onClose;
|
||||||
@ -36,7 +36,7 @@ function render (props) {
|
|||||||
contract={ CONTRACT }
|
contract={ CONTRACT }
|
||||||
onClose={ onClose }
|
onClose={ onClose }
|
||||||
onFromAddressChange={ onFromAddressChange } />,
|
onFromAddressChange={ onFromAddressChange } />,
|
||||||
{ context: { api: {}, store: STORE } }
|
{ context: { api: createApi(), store: STORE } }
|
||||||
).find('ExecuteContract').shallow();
|
).find('ExecuteContract').shallow();
|
||||||
|
|
||||||
return component;
|
return component;
|
||||||
|
@ -64,7 +64,19 @@ const STORE = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function createApi (result = true) {
|
||||||
|
return {
|
||||||
|
parity: {
|
||||||
|
registryAddress: sinon.stub().resolves('0x0000000000000000000000000000000000000000')
|
||||||
|
},
|
||||||
|
util: {
|
||||||
|
sha3: sinon.stub().resolves('0x0000000000000000000000000000000000000000')
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
createApi,
|
||||||
CONTRACT,
|
CONTRACT,
|
||||||
STORE
|
STORE
|
||||||
};
|
};
|
||||||
|
@ -52,6 +52,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.description {
|
||||||
|
font-size: 0.75em;
|
||||||
|
color: rgba(255, 255, 255, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
.accountInfo {
|
.accountInfo {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ export default class AccountCard extends Component {
|
|||||||
const { account } = this.props;
|
const { account } = this.props;
|
||||||
const { copied } = this.state;
|
const { copied } = this.state;
|
||||||
|
|
||||||
const { address, name, meta = {} } = account;
|
const { address, name, description, meta = {} } = account;
|
||||||
|
|
||||||
const displayName = (name && name.toUpperCase()) || address;
|
const displayName = (name && name.toUpperCase()) || address;
|
||||||
const { tags = [] } = meta;
|
const { tags = [] } = meta;
|
||||||
@ -70,6 +70,7 @@ export default class AccountCard extends Component {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{ this.renderTags(tags, address) }
|
{ this.renderTags(tags, address) }
|
||||||
|
{ this.renderDescription(description) }
|
||||||
{ this.renderAddress(displayName, address) }
|
{ this.renderAddress(displayName, address) }
|
||||||
{ this.renderBalance(address) }
|
{ this.renderBalance(address) }
|
||||||
</div>
|
</div>
|
||||||
@ -77,6 +78,18 @@ export default class AccountCard extends Component {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderDescription (description) {
|
||||||
|
if (!description) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={ styles.description }>
|
||||||
|
<span>{ description }</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
renderAddress (name, address) {
|
renderAddress (name, address) {
|
||||||
if (name === address) {
|
if (name === address) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -19,6 +19,7 @@ import ReactDOM from 'react-dom';
|
|||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
import keycode, { codes } from 'keycode';
|
import keycode, { codes } from 'keycode';
|
||||||
import { FormattedMessage } from 'react-intl';
|
import { FormattedMessage } from 'react-intl';
|
||||||
|
import { observer } from 'mobx-react';
|
||||||
|
|
||||||
import TextFieldUnderline from 'material-ui/TextField/TextFieldUnderline';
|
import TextFieldUnderline from 'material-ui/TextField/TextFieldUnderline';
|
||||||
|
|
||||||
@ -26,7 +27,9 @@ import AccountCard from '~/ui/AccountCard';
|
|||||||
import InputAddress from '~/ui/Form/InputAddress';
|
import InputAddress from '~/ui/Form/InputAddress';
|
||||||
import Portal from '~/ui/Portal';
|
import Portal from '~/ui/Portal';
|
||||||
import { validateAddress } from '~/util/validation';
|
import { validateAddress } from '~/util/validation';
|
||||||
|
import { nodeOrStringProptype } from '~/util/proptypes';
|
||||||
|
|
||||||
|
import AddressSelectStore from './addressSelectStore';
|
||||||
import styles from './addressSelect.css';
|
import styles from './addressSelect.css';
|
||||||
|
|
||||||
const BOTTOM_BORDER_STYLE = { borderBottom: 'solid 3px' };
|
const BOTTOM_BORDER_STYLE = { borderBottom: 'solid 3px' };
|
||||||
@ -34,8 +37,10 @@ const BOTTOM_BORDER_STYLE = { borderBottom: 'solid 3px' };
|
|||||||
// Current Form ID
|
// Current Form ID
|
||||||
let currentId = 1;
|
let currentId = 1;
|
||||||
|
|
||||||
|
@observer
|
||||||
class AddressSelect extends Component {
|
class AddressSelect extends Component {
|
||||||
static contextTypes = {
|
static contextTypes = {
|
||||||
|
api: PropTypes.object.isRequired,
|
||||||
muiTheme: PropTypes.object.isRequired
|
muiTheme: PropTypes.object.isRequired
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -55,24 +60,25 @@ class AddressSelect extends Component {
|
|||||||
// Optional props
|
// Optional props
|
||||||
allowInput: PropTypes.bool,
|
allowInput: PropTypes.bool,
|
||||||
disabled: PropTypes.bool,
|
disabled: PropTypes.bool,
|
||||||
error: PropTypes.string,
|
error: nodeOrStringProptype(),
|
||||||
hint: PropTypes.string,
|
hint: nodeOrStringProptype(),
|
||||||
label: PropTypes.string,
|
label: nodeOrStringProptype(),
|
||||||
value: PropTypes.string
|
value: nodeOrStringProptype()
|
||||||
};
|
};
|
||||||
|
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
value: ''
|
value: ''
|
||||||
};
|
};
|
||||||
|
|
||||||
|
store = new AddressSelectStore(this.context.api);
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
expanded: false,
|
expanded: false,
|
||||||
focused: false,
|
focused: false,
|
||||||
focusedCat: null,
|
focusedCat: null,
|
||||||
focusedItem: null,
|
focusedItem: null,
|
||||||
inputFocused: false,
|
inputFocused: false,
|
||||||
inputValue: '',
|
inputValue: ''
|
||||||
values: []
|
|
||||||
};
|
};
|
||||||
|
|
||||||
componentWillMount () {
|
componentWillMount () {
|
||||||
@ -80,7 +86,7 @@ class AddressSelect extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps (nextProps) {
|
componentWillReceiveProps (nextProps) {
|
||||||
if (this.values && this.values.length > 0) {
|
if (this.store.values && this.store.values.length > 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,36 +94,7 @@ class AddressSelect extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setValues (props = this.props) {
|
setValues (props = this.props) {
|
||||||
const { accounts = {}, contracts = {}, contacts = {}, wallets = {} } = props;
|
this.store.setValues(props);
|
||||||
|
|
||||||
const accountsN = Object.keys(accounts).length;
|
|
||||||
const contractsN = Object.keys(contracts).length;
|
|
||||||
const contactsN = Object.keys(contacts).length;
|
|
||||||
const walletsN = Object.keys(wallets).length;
|
|
||||||
|
|
||||||
if (accountsN + contractsN + contactsN + walletsN === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.values = [
|
|
||||||
{
|
|
||||||
label: 'accounts',
|
|
||||||
values: [].concat(
|
|
||||||
Object.values(wallets),
|
|
||||||
Object.values(accounts)
|
|
||||||
)
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'contacts',
|
|
||||||
values: Object.values(contacts)
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'contracts',
|
|
||||||
values: Object.values(contracts)
|
|
||||||
}
|
|
||||||
].filter((cat) => cat.values.length > 0);
|
|
||||||
|
|
||||||
this.handleChange();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
@ -216,6 +193,7 @@ class AddressSelect extends Component {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{ this.renderCurrentInput() }
|
{ this.renderCurrentInput() }
|
||||||
|
{ this.renderRegistryValues() }
|
||||||
{ this.renderAccounts() }
|
{ this.renderAccounts() }
|
||||||
</Portal>
|
</Portal>
|
||||||
);
|
);
|
||||||
@ -241,8 +219,28 @@ class AddressSelect extends Component {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderRegistryValues () {
|
||||||
|
const { registryValues } = this.store;
|
||||||
|
|
||||||
|
if (registryValues.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const accounts = registryValues
|
||||||
|
.map((registryValue, index) => {
|
||||||
|
const account = { ...registryValue, index: `${registryValue.address}_${index}` };
|
||||||
|
return this.renderAccountCard(account);
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{ accounts }
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
renderAccounts () {
|
renderAccounts () {
|
||||||
const { values } = this.state;
|
const { values } = this.store;
|
||||||
|
|
||||||
if (values.length === 0) {
|
if (values.length === 0) {
|
||||||
return (
|
return (
|
||||||
@ -257,8 +255,8 @@ class AddressSelect extends Component {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const categories = values.map((category) => {
|
const categories = values.map((category, index) => {
|
||||||
return this.renderCategory(category.label, category.values);
|
return this.renderCategory(category, index);
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -268,7 +266,8 @@ class AddressSelect extends Component {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
renderCategory (name, values = []) {
|
renderCategory (category, index) {
|
||||||
|
const { label, key, values = [] } = category;
|
||||||
let content;
|
let content;
|
||||||
|
|
||||||
if (values.length === 0) {
|
if (values.length === 0) {
|
||||||
@ -292,8 +291,8 @@ class AddressSelect extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={ styles.category } key={ name }>
|
<div className={ styles.category } key={ `${key}_${index}` }>
|
||||||
<div className={ styles.title }>{ name }</div>
|
<div className={ styles.title }>{ label }</div>
|
||||||
{ content }
|
{ content }
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@ -306,7 +305,7 @@ class AddressSelect extends Component {
|
|||||||
const balance = balances[address];
|
const balance = balances[address];
|
||||||
const account = {
|
const account = {
|
||||||
...accountsInfo[address],
|
...accountsInfo[address],
|
||||||
address, index
|
..._account
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -325,9 +324,10 @@ class AddressSelect extends Component {
|
|||||||
this.inputRef = refId;
|
this.inputRef = refId;
|
||||||
}
|
}
|
||||||
|
|
||||||
handleCustomInput = () => {
|
validateCustomInput = () => {
|
||||||
const { allowInput } = this.props;
|
const { allowInput } = this.props;
|
||||||
const { inputValue, values } = this.state;
|
const { inputValue } = this.store;
|
||||||
|
const { values } = this.store;
|
||||||
|
|
||||||
// If input is HEX and allowInput === true, send it
|
// If input is HEX and allowInput === true, send it
|
||||||
if (allowInput && inputValue && /^(0x)?([0-9a-f])+$/i.test(inputValue)) {
|
if (allowInput && inputValue && /^(0x)?([0-9a-f])+$/i.test(inputValue)) {
|
||||||
@ -335,8 +335,8 @@ class AddressSelect extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If only one value, select it
|
// If only one value, select it
|
||||||
if (values.length === 1 && values[0].values.length === 1) {
|
if (values.reduce((cur, cat) => cur + cat.values.length, 0) === 1) {
|
||||||
const value = values[0].values[0];
|
const value = values.find((cat) => cat.values.length > 0).values[0];
|
||||||
return this.handleClick(value.address);
|
return this.handleClick(value.address);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -361,7 +361,7 @@ class AddressSelect extends Component {
|
|||||||
case 'enter':
|
case 'enter':
|
||||||
const index = this.state.focusedItem;
|
const index = this.state.focusedItem;
|
||||||
if (!index) {
|
if (!index) {
|
||||||
return this.handleCustomInput();
|
return this.validateCustomInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.handleDOMAction(`account_${index}`, 'click');
|
return this.handleDOMAction(`account_${index}`, 'click');
|
||||||
@ -408,10 +408,11 @@ class AddressSelect extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleNavigation = (direction, event) => {
|
handleNavigation = (direction, event) => {
|
||||||
const { focusedItem, focusedCat, values } = this.state;
|
const { focusedItem, focusedCat } = this.state;
|
||||||
|
const { values } = this.store;
|
||||||
|
|
||||||
// Don't do anything if no values
|
// Don't do anything if no values
|
||||||
if (values.length === 0) {
|
if (values.reduce((cur, cat) => cur + cat.values.length, 0) === 0) {
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -423,7 +424,12 @@ class AddressSelect extends Component {
|
|||||||
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
const nextValues = values[focusedCat || 0];
|
const firstCat = values.findIndex((cat) => cat.values.length > 0);
|
||||||
|
const nextCat = focusedCat && values[focusedCat].values.length > 0
|
||||||
|
? focusedCat
|
||||||
|
: firstCat;
|
||||||
|
|
||||||
|
const nextValues = values[nextCat];
|
||||||
const nextFocus = nextValues ? nextValues.values[0] : null;
|
const nextFocus = nextValues ? nextValues.values[0] : null;
|
||||||
return this.focusItem(nextFocus && nextFocus.index || 1);
|
return this.focusItem(nextFocus && nextFocus.index || 1);
|
||||||
}
|
}
|
||||||
@ -457,12 +463,21 @@ class AddressSelect extends Component {
|
|||||||
|
|
||||||
// If right: next category
|
// If right: next category
|
||||||
if (direction === 'right') {
|
if (direction === 'right') {
|
||||||
nextCategory = Math.min(prevCategoryIndex + 1, values.length - 1);
|
const categoryShift = values
|
||||||
|
.slice(prevCategoryIndex + 1, values.length)
|
||||||
|
.findIndex((cat) => cat.values.length > 0) + 1;
|
||||||
|
|
||||||
|
nextCategory = Math.min(prevCategoryIndex + categoryShift, values.length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If right: previous category
|
// If right: previous category
|
||||||
if (direction === 'left') {
|
if (direction === 'left') {
|
||||||
nextCategory = Math.max(prevCategoryIndex - 1, 0);
|
const categoryShift = values
|
||||||
|
.slice(0, prevCategoryIndex)
|
||||||
|
.reverse()
|
||||||
|
.findIndex((cat) => cat.values.length > 0) + 1;
|
||||||
|
|
||||||
|
nextCategory = Math.max(prevCategoryIndex - categoryShift, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If left or right: try to keep the horizontal index
|
// If left or right: try to keep the horizontal index
|
||||||
@ -525,43 +540,6 @@ class AddressSelect extends Component {
|
|||||||
this.setState({ expanded: false });
|
this.setState({ expanded: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter the given values based on the given
|
|
||||||
* filter
|
|
||||||
*/
|
|
||||||
filterValues = (values = [], _filter = '') => {
|
|
||||||
const filter = _filter.toLowerCase();
|
|
||||||
|
|
||||||
return values
|
|
||||||
// Remove empty accounts
|
|
||||||
.filter((a) => a)
|
|
||||||
.filter((account) => {
|
|
||||||
const address = account.address.toLowerCase();
|
|
||||||
const inAddress = address.includes(filter);
|
|
||||||
|
|
||||||
if (!account.name || inAddress) {
|
|
||||||
return inAddress;
|
|
||||||
}
|
|
||||||
|
|
||||||
const name = account.name.toLowerCase();
|
|
||||||
const inName = name.includes(filter);
|
|
||||||
const { meta = {} } = account;
|
|
||||||
|
|
||||||
if (!meta.tags || inName) {
|
|
||||||
return inName;
|
|
||||||
}
|
|
||||||
|
|
||||||
const tags = (meta.tags || []).join('');
|
|
||||||
return tags.includes(filter);
|
|
||||||
})
|
|
||||||
.sort((accA, accB) => {
|
|
||||||
const nameA = accA.name || accA.address;
|
|
||||||
const nameB = accB.name || accB.address;
|
|
||||||
|
|
||||||
return nameA.localeCompare(nameB);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
handleInputBlur = () => {
|
handleInputBlur = () => {
|
||||||
this.setState({ inputFocused: false });
|
this.setState({ inputFocused: false });
|
||||||
}
|
}
|
||||||
@ -572,25 +550,10 @@ class AddressSelect extends Component {
|
|||||||
|
|
||||||
handleChange = (event = { target: {} }) => {
|
handleChange = (event = { target: {} }) => {
|
||||||
const { value = '' } = event.target;
|
const { value = '' } = event.target;
|
||||||
let index = 0;
|
|
||||||
|
|
||||||
const values = this.values
|
this.store.handleChange(value);
|
||||||
.map((category) => {
|
|
||||||
const filteredValues = this
|
|
||||||
.filterValues(category.values, value)
|
|
||||||
.map((value) => {
|
|
||||||
index++;
|
|
||||||
return { ...value, index: parseInt(index) };
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
label: category.label,
|
|
||||||
values: filteredValues
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
values,
|
|
||||||
focusedItem: null,
|
focusedItem: null,
|
||||||
inputValue: value
|
inputValue: value
|
||||||
});
|
});
|
||||||
|
222
js/src/ui/Form/AddressSelect/addressSelectStore.js
Normal file
222
js/src/ui/Form/AddressSelect/addressSelectStore.js
Normal file
@ -0,0 +1,222 @@
|
|||||||
|
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
||||||
|
// This file is part of Parity.
|
||||||
|
|
||||||
|
// Parity is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
|
||||||
|
// Parity is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import React from 'react';
|
||||||
|
import { observable, action } from 'mobx';
|
||||||
|
import { flatMap } from 'lodash';
|
||||||
|
import { FormattedMessage } from 'react-intl';
|
||||||
|
|
||||||
|
import Contracts from '~/contracts';
|
||||||
|
import { sha3 } from '~/api/util/sha3';
|
||||||
|
|
||||||
|
export default class AddressSelectStore {
|
||||||
|
|
||||||
|
@observable values = [];
|
||||||
|
@observable registryValues = [];
|
||||||
|
|
||||||
|
initValues = [];
|
||||||
|
regLookups = [];
|
||||||
|
|
||||||
|
constructor (api) {
|
||||||
|
this.api = api;
|
||||||
|
|
||||||
|
const { registry } = Contracts.create(api);
|
||||||
|
|
||||||
|
registry
|
||||||
|
.getContract('emailverification')
|
||||||
|
.then((emailVerification) => {
|
||||||
|
this.regLookups.push({
|
||||||
|
lookup: (value) => {
|
||||||
|
return emailVerification
|
||||||
|
.instance
|
||||||
|
.reverse.call({}, [ sha3(value) ]);
|
||||||
|
},
|
||||||
|
describe: (value) => (
|
||||||
|
<FormattedMessage
|
||||||
|
id='addressSelect.fromEmail'
|
||||||
|
defaultMessage='Verified using email {value}'
|
||||||
|
values={ {
|
||||||
|
value
|
||||||
|
} }
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
registry
|
||||||
|
.getInstance()
|
||||||
|
.then((registryInstance) => {
|
||||||
|
this.regLookups.push({
|
||||||
|
lookup: (value) => {
|
||||||
|
return registryInstance
|
||||||
|
.getAddress.call({}, [ sha3(value), 'A' ]);
|
||||||
|
},
|
||||||
|
describe: (value) => (
|
||||||
|
<FormattedMessage
|
||||||
|
id='addressSelect.fromRegistry'
|
||||||
|
defaultMessage='{value} (from registry)'
|
||||||
|
values={ {
|
||||||
|
value
|
||||||
|
} }
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@action setValues (props) {
|
||||||
|
const { accounts = {}, contracts = {}, contacts = {}, wallets = {} } = props;
|
||||||
|
|
||||||
|
const accountsN = Object.keys(accounts).length;
|
||||||
|
const contractsN = Object.keys(contracts).length;
|
||||||
|
const contactsN = Object.keys(contacts).length;
|
||||||
|
const walletsN = Object.keys(wallets).length;
|
||||||
|
|
||||||
|
if (accountsN + contractsN + contactsN + walletsN === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.initValues = [
|
||||||
|
{
|
||||||
|
key: 'accounts',
|
||||||
|
label: (
|
||||||
|
<FormattedMessage
|
||||||
|
id='addressSelect.labels.accounts'
|
||||||
|
defaultMessage='accounts'
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
values: [].concat(
|
||||||
|
Object.values(wallets),
|
||||||
|
Object.values(accounts)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'contacts',
|
||||||
|
label: (
|
||||||
|
<FormattedMessage
|
||||||
|
id='addressSelect.labels.contacts'
|
||||||
|
defaultMessage='contacts'
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
values: Object.values(contacts)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'contracts',
|
||||||
|
label: (
|
||||||
|
<FormattedMessage
|
||||||
|
id='addressSelect.labels.contracts'
|
||||||
|
defaultMessage='contracts'
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
values: Object.values(contracts)
|
||||||
|
}
|
||||||
|
].filter((cat) => cat.values.length > 0);
|
||||||
|
|
||||||
|
this.handleChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
@action handleChange = (value = '') => {
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
this.values = this.initValues
|
||||||
|
.map((category) => {
|
||||||
|
const filteredValues = this
|
||||||
|
.filterValues(category.values, value)
|
||||||
|
.map((value) => {
|
||||||
|
index++;
|
||||||
|
|
||||||
|
return {
|
||||||
|
index: parseInt(index),
|
||||||
|
...value
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
label: category.label,
|
||||||
|
values: filteredValues
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Registries Lookup
|
||||||
|
this.registryValues = [];
|
||||||
|
|
||||||
|
const lookups = this.regLookups.map((regLookup) => regLookup.lookup(value));
|
||||||
|
|
||||||
|
Promise
|
||||||
|
.all(lookups)
|
||||||
|
.then((results) => {
|
||||||
|
return results
|
||||||
|
.map((result, index) => {
|
||||||
|
if (/^(0x)?0*$/.test(result)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lowercaseResult = result.toLowerCase();
|
||||||
|
|
||||||
|
const account = flatMap(this.initValues, (cat) => cat.values)
|
||||||
|
.find((account) => account.address.toLowerCase() === lowercaseResult);
|
||||||
|
|
||||||
|
return {
|
||||||
|
description: this.regLookups[index].describe(value),
|
||||||
|
address: result,
|
||||||
|
name: account && account.name || value
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.filter((data) => data);
|
||||||
|
})
|
||||||
|
.then((registryValues) => {
|
||||||
|
this.registryValues = registryValues;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter the given values based on the given
|
||||||
|
* filter
|
||||||
|
*/
|
||||||
|
filterValues = (values = [], _filter = '') => {
|
||||||
|
const filter = _filter.toLowerCase();
|
||||||
|
|
||||||
|
return values
|
||||||
|
// Remove empty accounts
|
||||||
|
.filter((a) => a)
|
||||||
|
.filter((account) => {
|
||||||
|
const address = account.address.toLowerCase();
|
||||||
|
const inAddress = address.includes(filter);
|
||||||
|
|
||||||
|
if (!account.name || inAddress) {
|
||||||
|
return inAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
const name = account.name.toLowerCase();
|
||||||
|
const inName = name.includes(filter);
|
||||||
|
const { meta = {} } = account;
|
||||||
|
|
||||||
|
if (!meta.tags || inName) {
|
||||||
|
return inName;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tags = (meta.tags || []).join('');
|
||||||
|
return tags.includes(filter);
|
||||||
|
})
|
||||||
|
.sort((accA, accB) => {
|
||||||
|
const nameA = accA.name || accA.address;
|
||||||
|
const nameB = accB.name || accB.address;
|
||||||
|
|
||||||
|
return nameA.localeCompare(nameB);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user