Add store for AddAddress (#3819)

* WIP

* Updated tests

* Final round of fixes

* Header update
This commit is contained in:
Jaco Greeff 2016-12-12 00:38:38 +01:00 committed by GitHub
parent 2de64bb5e4
commit 22ac80d98f
9 changed files with 423 additions and 135 deletions

View File

@ -14,31 +14,29 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
import React, { Component, PropTypes } from 'react';
import ContentAdd from 'material-ui/svg-icons/content/add'; import ContentAdd from 'material-ui/svg-icons/content/add';
import ContentClear from 'material-ui/svg-icons/content/clear'; import ContentClear from 'material-ui/svg-icons/content/clear';
import { observer } from 'mobx-react';
import React, { Component, PropTypes } from 'react';
import { FormattedMessage } from 'react-intl';
import { Button, Modal, Form, Input, InputAddress } from '~/ui'; import { Button, Form, Input, InputAddress, Modal } from '~/ui';
import { ERRORS, validateAddress, validateName } from '~/util/validation';
import Store from './store';
@observer
export default class AddAddress extends Component { export default class AddAddress extends Component {
static contextTypes = { static contextTypes = {
api: PropTypes.object.isRequired api: PropTypes.object.isRequired
} }
static propTypes = { static propTypes = {
contacts: PropTypes.object.isRequired,
address: PropTypes.string, address: PropTypes.string,
onClose: PropTypes.func contacts: PropTypes.object.isRequired,
onClose: PropTypes.func.isRequired
}; };
state = { store = new Store(this.context.api, this.props.contacts);
address: '',
addressError: ERRORS.invalidAddress,
name: '',
nameError: ERRORS.invalidName,
description: ''
};
componentWillMount () { componentWillMount () {
if (this.props.address) { if (this.props.address) {
@ -49,109 +47,113 @@ export default class AddAddress extends Component {
render () { render () {
return ( return (
<Modal <Modal
visible
actions={ this.renderDialogActions() } actions={ this.renderDialogActions() }
title='add saved address'> title={
<FormattedMessage
id='addAddress.label'
defaultMessage='add saved address' />
}
visible>
{ this.renderFields() } { this.renderFields() }
</Modal> </Modal>
); );
} }
renderDialogActions () { renderDialogActions () {
const { addressError, nameError } = this.state; const { hasError } = this.store;
const hasError = !!(addressError || nameError);
return ([ return ([
<Button <Button
icon={ <ContentClear /> } icon={ <ContentClear /> }
label='Cancel' label={
onClick={ this.onClose } />, <FormattedMessage
id='addAddress.button.close'
defaultMessage='Cancel' />
}
onClick={ this.onClose }
ref='closeButton' />,
<Button <Button
icon={ <ContentAdd /> }
label='Save Address'
disabled={ hasError } disabled={ hasError }
onClick={ this.onAdd } /> icon={ <ContentAdd /> }
label={
<FormattedMessage
id='addAddress.button.add'
defaultMessage='Save Address' />
}
onClick={ this.onAdd }
ref='addButton' />
]); ]);
} }
renderFields () { renderFields () {
const { address, addressError, description, name, nameError } = this.state; const { address, addressError, description, name, nameError } = this.store;
return ( return (
<Form> <Form>
<InputAddress <InputAddress
label='network address'
hint='the network address for the entry'
error={ addressError }
value={ address }
disabled={ !!this.props.address }
allowCopy={ false } allowCopy={ false }
onChange={ this.onEditAddress } /> disabled={ !!this.props.address }
error={ addressError }
hint={
<FormattedMessage
id='addAddress.input.address.hint'
defaultMessage='the network address for the entry' />
}
label={
<FormattedMessage
id='addAddress.input.address.label'
defaultMessage='network address' />
}
onChange={ this.onEditAddress }
ref='inputAddress'
value={ address } />
<Input <Input
label='address name'
hint='a descriptive name for the entry'
error={ nameError } error={ nameError }
value={ name } hint={
onChange={ this.onEditName } /> <FormattedMessage
id='addAddress.input.name.hint'
defaultMessage='a descriptive name for the entry' />
}
label={
<FormattedMessage
id='addAddress.input.name.label'
defaultMessage='address name' />
}
onChange={ this.onEditName }
ref='inputName'
value={ name } />
<Input <Input
multiLine hint={
rows={ 1 } <FormattedMessage
label='(optional) address description' id='addAddress.input.description.hint'
hint='an expanded description for the entry' defaultMessage='an expanded description for the entry' />
value={ description } }
onChange={ this.onEditDescription } /> label={
<FormattedMessage
id='addAddress.input.description.label'
defaultMessage='(optional) address description' />
}
onChange={ this.onEditDescription }
ref='inputDescription'
value={ description } />
</Form> </Form>
); );
} }
onEditAddress = (event, _address) => { onEditAddress = (event, address) => {
const { contacts } = this.props; this.store.setAddress(address);
let { address, addressError } = validateAddress(_address);
if (!addressError) {
const contact = contacts[address];
if (contact) {
addressError = ERRORS.duplicateAddress;
}
}
this.setState({
address,
addressError
});
} }
onEditDescription = (event, description) => { onEditDescription = (event, description) => {
this.setState({ this.store.setDescription(description);
description
});
} }
onEditName = (event, _name) => { onEditName = (event, name) => {
const { name, nameError } = validateName(_name); this.store.setName(name);
this.setState({
name,
nameError
});
} }
onAdd = () => { onAdd = () => {
const { api } = this.context; this.store.add();
const { address, name, description } = this.state;
Promise.all([
api.parity.setAccountName(address, name),
api.parity.setAccountMeta(address, {
description,
timestamp: Date.now(),
deleted: false
})
]).catch((error) => {
console.error('onAdd', error);
});
this.props.onClose(); this.props.onClose();
} }

View File

@ -0,0 +1,32 @@
// 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 { shallow } from 'enzyme';
import React from 'react';
import AddAddress from './';
describe('modals/AddAddress', () => {
describe('rendering', () => {
it('renders defaults', () => {
expect(
shallow(
<AddAddress />
)
).to.be.ok;
});
});
});

View File

@ -0,0 +1,87 @@
// 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 { action, computed, transaction, observable } from 'mobx';
import { ERRORS, validateAddress, validateName } from '~/util/validation';
export default class Store {
@observable address = '';
@observable addressError = ERRORS.invalidAddress;
@observable createError = null;
@observable description = '';
@observable name = '';
@observable nameError = ERRORS.invalidName;
constructor (api, contacts) {
this._api = api;
this._contacts = contacts;
}
@computed get hasError () {
return !!(this.addressError || this.nameError);
}
@action setAddress = (_address) => {
let { address, addressError } = validateAddress(_address);
if (!addressError) {
const contact = this._contacts[address];
if (contact) {
addressError = ERRORS.duplicateAddress;
}
}
transaction(() => {
this.address = address;
this.addressError = addressError;
});
}
@action setCreateError = (error) => {
this.createError = error;
}
@action setDescription = (description) => {
this.description = description;
}
@action setName = (_name) => {
const { name, nameError } = validateName(_name);
transaction(() => {
this.name = name;
this.nameError = nameError;
});
}
add () {
return Promise
.all([
this._api.parity.setAccountName(this.address, this.name),
this._api.parity.setAccountMeta(this.address, {
description: this.description,
timestamp: Date.now(),
deleted: false
})
])
.catch((error) => {
console.warn('Store:add', error);
this.setCreateError(error);
});
}
}

View File

@ -0,0 +1,128 @@
// 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 sinon from 'sinon';
import Store from './store';
import { TEST_ADDR_A, TEST_ADDR_B, TEST_CONTACTS } from './store.test.js';
describe('modals/AddAddress/store', () => {
let store;
describe('@action', () => {
beforeEach(() => {
store = new Store(null, TEST_CONTACTS);
});
describe('setAddress', () => {
it('successfully sets non-existent addresses', () => {
store.setAddress(TEST_ADDR_B);
expect(store.addressError).to.be.null;
expect(store.address).to.equal(TEST_ADDR_B);
});
it('fails on invalid addresses', () => {
store.setAddress('0xinvalid');
expect(store.addressError).not.to.be.null;
});
it('fails when an address is already added', () => {
store.setAddress(TEST_ADDR_A);
expect(store.addressError).not.to.be.null;
});
});
describe('setName', () => {
it('sucessfully sets valid names', () => {
const name = 'Test Name';
store.setName(name);
expect(store.nameError).to.be.null;
expect(store.name).to.equal(name);
});
it('fails when name is invalid', () => {
store.setName(null);
expect(store.nameError).not.to.be.null;
});
});
});
describe('@computed', () => {
beforeEach(() => {
store = new Store(null, TEST_CONTACTS);
});
describe('hasError', () => {
beforeEach(() => {
store.setAddress(TEST_ADDR_B);
store.setName('Test Name');
});
it('returns false proper inputs', () => {
expect(store.hasError).to.be.false;
});
it('returns true with addressError', () => {
store.setAddress(TEST_ADDR_A);
expect(store.addressError).not.to.be.null;
expect(store.hasError).to.be.true;
});
it('returns true with nameError', () => {
store.setName(null);
expect(store.nameError).not.to.be.null;
expect(store.hasError).to.be.true;
});
});
});
describe('methods', () => {
let api;
beforeEach(() => {
api = {
parity: {
setAccountMeta: sinon.stub().resolves(true),
setAccountName: sinon.stub().resolves(true)
}
};
store = new Store(api, {});
});
describe('add', () => {
it('calls setAccountMeta', () => {
store.add();
expect(api.parity.setAccountMeta).to.have.been.called;
});
it('calls setAccountName', () => {
store.add();
expect(api.parity.setAccountName).to.have.been.called;
});
});
});
});

View File

@ -0,0 +1,28 @@
// 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/>.
const TEST_ADDR_A = '0x63Cf90D3f0410092FC0fca41846f596223979195';
const TEST_ADDR_B = '0x00A40dEfa9933e82244bE542Fa7F8748eCCdd457';
const TEST_CONTACTS = {
[TEST_ADDR_A]: { name: 'test', meta: {} }
};
export {
TEST_ADDR_A,
TEST_ADDR_B,
TEST_CONTACTS
};

View File

@ -18,6 +18,8 @@ import React, { Component, PropTypes } from 'react';
import { TextField } from 'material-ui'; import { TextField } from 'material-ui';
import { noop } from 'lodash'; import { noop } from 'lodash';
import { nodeOrStringProptype } from '~/util/proptypes';
import CopyToClipboard from '../../CopyToClipboard'; import CopyToClipboard from '../../CopyToClipboard';
import styles from './input.css'; import styles from './input.css';
@ -41,18 +43,21 @@ const NAME_ID = ' ';
export default class Input extends Component { export default class Input extends Component {
static propTypes = { static propTypes = {
children: PropTypes.node,
className: PropTypes.string,
disabled: PropTypes.bool,
readOnly: PropTypes.bool,
allowCopy: PropTypes.oneOfType([ allowCopy: PropTypes.oneOfType([
PropTypes.string, PropTypes.string,
PropTypes.bool PropTypes.bool
]), ]),
floatCopy: PropTypes.bool, children: PropTypes.node,
className: PropTypes.string,
disabled: PropTypes.bool,
error: PropTypes.string, error: PropTypes.string,
hint: PropTypes.string, readOnly: PropTypes.bool,
label: PropTypes.string, floatCopy: PropTypes.bool,
hint: nodeOrStringProptype(),
hideUnderline: PropTypes.bool,
label: nodeOrStringProptype(),
max: PropTypes.any,
min: PropTypes.any,
multiLine: PropTypes.bool, multiLine: PropTypes.bool,
onBlur: PropTypes.func, onBlur: PropTypes.func,
onChange: PropTypes.func, onChange: PropTypes.func,
@ -61,26 +66,26 @@ export default class Input extends Component {
rows: PropTypes.number, rows: PropTypes.number,
type: PropTypes.string, type: PropTypes.string,
submitOnBlur: PropTypes.bool, submitOnBlur: PropTypes.bool,
hideUnderline: PropTypes.bool, style: PropTypes.object,
value: PropTypes.oneOfType([ value: PropTypes.oneOfType([
PropTypes.number, PropTypes.string PropTypes.number,
]), PropTypes.string
min: PropTypes.any, ])
max: PropTypes.any,
style: PropTypes.object
}; };
static defaultProps = { static defaultProps = {
submitOnBlur: true,
readOnly: false,
allowCopy: false, allowCopy: false,
hideUnderline: false, hideUnderline: false,
floatCopy: false, floatCopy: false,
readOnly: false,
submitOnBlur: true,
style: {} style: {}
} }
state = { state = {
value: typeof this.props.value === 'undefined' ? '' : this.props.value value: typeof this.props.value === 'undefined'
? ''
: this.props.value
} }
componentWillReceiveProps (newProps) { componentWillReceiveProps (newProps) {
@ -114,38 +119,29 @@ export default class Input extends Component {
autoComplete='off' autoComplete='off'
className={ className } className={ className }
errorText={ error } errorText={ error }
floatingLabelFixed floatingLabelFixed
floatingLabelText={ label } floatingLabelText={ label }
fullWidth
hintText={ hint } hintText={ hint }
id={ NAME_ID } id={ NAME_ID }
inputStyle={ inputStyle } inputStyle={ inputStyle }
fullWidth
max={ max } max={ max }
min={ min } min={ min }
multiLine={ multiLine } multiLine={ multiLine }
name={ NAME_ID } name={ NAME_ID }
onBlur={ this.onBlur } onBlur={ this.onBlur }
onChange={ this.onChange } onChange={ this.onChange }
onKeyDown={ this.onKeyDown } onKeyDown={ this.onKeyDown }
onPaste={ this.onPaste } onPaste={ this.onPaste }
readOnly={ readOnly } readOnly={ readOnly }
rows={ rows } rows={ rows }
style={ textFieldStyle } style={ textFieldStyle }
type={ type || 'text' } type={ type || 'text' }
underlineDisabledStyle={ UNDERLINE_DISABLED } underlineDisabledStyle={ UNDERLINE_DISABLED }
underlineStyle={ readOnly ? UNDERLINE_READONLY : UNDERLINE_NORMAL } underlineStyle={ readOnly ? UNDERLINE_READONLY : UNDERLINE_NORMAL }
underlineFocusStyle={ readOnly ? { display: 'none' } : null } underlineFocusStyle={ readOnly ? { display: 'none' } : null }
underlineShow={ !hideUnderline } underlineShow={ !hideUnderline }
value={ value }>
value={ value }
>
{ children } { children }
</TextField> </TextField>
</div> </div>
@ -159,11 +155,14 @@ export default class Input extends Component {
if (!allowCopy) { if (!allowCopy) {
return null; return null;
} }
const text = typeof allowCopy === 'string' const text = typeof allowCopy === 'string'
? allowCopy ? allowCopy
: value; : value;
const style = hideUnderline ? {} : { position: 'relative', top: '2px' }; const style = hideUnderline
? {}
: { position: 'relative', top: '2px' };
return ( return (
<div className={ styles.copy } style={ style }> <div className={ styles.copy } style={ style }>

View File

@ -18,28 +18,30 @@ import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
import Input from '../Input';
import IdentityIcon from '../../IdentityIcon';
import util from '~/api/util'; import util from '~/api/util';
import { nodeOrStringProptype } from '~/util/proptypes';
import IdentityIcon from '../../IdentityIcon';
import Input from '../Input';
import styles from './inputAddress.css'; import styles from './inputAddress.css';
class InputAddress extends Component { class InputAddress extends Component {
static propTypes = { static propTypes = {
accountsInfo: PropTypes.object,
allowCopy: PropTypes.bool,
className: PropTypes.string, className: PropTypes.string,
disabled: PropTypes.bool, disabled: PropTypes.bool,
error: PropTypes.string, error: PropTypes.string,
label: PropTypes.string, hideUnderline: PropTypes.bool,
hint: PropTypes.string, hint: nodeOrStringProptype(),
value: PropTypes.string, label: nodeOrStringProptype(),
accountsInfo: PropTypes.object,
tokens: PropTypes.object,
text: PropTypes.bool,
onChange: PropTypes.func, onChange: PropTypes.func,
onSubmit: PropTypes.func, onSubmit: PropTypes.func,
hideUnderline: PropTypes.bool, small: PropTypes.bool,
allowCopy: PropTypes.bool, text: PropTypes.bool,
small: PropTypes.bool tokens: PropTypes.object,
value: PropTypes.string
}; };
static defaultProps = { static defaultProps = {
@ -49,8 +51,8 @@ class InputAddress extends Component {
}; };
render () { render () {
const { className, disabled, error, label, hint, value, text } = this.props; const { className, disabled, error, hint, label, text, value } = this.props;
const { small, allowCopy, hideUnderline, onSubmit, accountsInfo, tokens } = this.props; const { accountsInfo, allowCopy, hideUnderline, onSubmit, small, tokens } = this.props;
const account = accountsInfo[value] || tokens[value]; const account = accountsInfo[value] || tokens[value];
@ -68,17 +70,20 @@ class InputAddress extends Component {
return ( return (
<div className={ containerClasses.join(' ') }> <div className={ containerClasses.join(' ') }>
<Input <Input
allowCopy={ allowCopy && (disabled ? value : false) }
className={ classes.join(' ') } className={ classes.join(' ') }
disabled={ disabled } disabled={ disabled }
label={ label }
hint={ hint }
error={ error } error={ error }
value={ text && account ? account.name : value } hideUnderline={ hideUnderline }
hint={ hint }
label={ label }
onChange={ this.handleInputChange } onChange={ this.handleInputChange }
onSubmit={ onSubmit } onSubmit={ onSubmit }
allowCopy={ allowCopy && (disabled ? value : false) } value={
hideUnderline={ hideUnderline } text && account
/> ? account.name
: value
} />
{ icon } { icon }
</div> </div>
); );
@ -128,8 +133,8 @@ class InputAddress extends Component {
} }
function mapStateToProps (state) { function mapStateToProps (state) {
const { accountsInfo } = state.personal;
const { tokens } = state.balances; const { tokens } = state.balances;
const { accountsInfo } = state.personal;
return { return {
accountsInfo, accountsInfo,

View File

@ -42,11 +42,11 @@ class Modal extends Component {
className: PropTypes.string, className: PropTypes.string,
compact: PropTypes.bool, compact: PropTypes.bool,
current: PropTypes.number, current: PropTypes.number,
waiting: PropTypes.array, settings: PropTypes.object.isRequired,
steps: PropTypes.array, steps: PropTypes.array,
title: nodeOrStringProptype(), title: nodeOrStringProptype(),
visible: PropTypes.bool.isRequired, visible: PropTypes.bool.isRequired,
settings: PropTypes.object.isRequired waiting: PropTypes.array
} }
render () { render () {
@ -55,23 +55,23 @@ class Modal extends Component {
const contentStyle = muiTheme.parity.getBackgroundStyle(null, settings.backgroundSeed); const contentStyle = muiTheme.parity.getBackgroundStyle(null, settings.backgroundSeed);
const header = ( const header = (
<Title <Title
current={ current }
busy={ busy } busy={ busy }
waiting={ waiting } current={ current }
steps={ steps } steps={ steps }
title={ title } /> title={ title }
waiting={ waiting } />
); );
const classes = `${styles.dialog} ${className}`; const classes = `${styles.dialog} ${className}`;
return ( return (
<Dialog <Dialog
className={ classes }
actions={ actions } actions={ actions }
actionsContainerClassName={ styles.actions }
actionsContainerStyle={ ACTIONS_STYLE } actionsContainerStyle={ ACTIONS_STYLE }
autoDetectWindowHeight={ false } autoDetectWindowHeight={ false }
autoScrollBodyContent autoScrollBodyContent
actionsContainerClassName={ styles.actions }
bodyClassName={ styles.body } bodyClassName={ styles.body }
className={ classes }
contentClassName={ styles.content } contentClassName={ styles.content }
contentStyle={ contentStyle } contentStyle={ contentStyle }
modal modal
@ -82,7 +82,12 @@ class Modal extends Component {
style={ DIALOG_STYLE } style={ DIALOG_STYLE }
title={ header } title={ header }
titleStyle={ TITLE_STYLE }> titleStyle={ TITLE_STYLE }>
<Container light compact={ compact } style={ { transition: 'none' } }> <Container
compact={ compact }
light
style={
{ transition: 'none' }
}>
{ children } { children }
</Container> </Container>
</Dialog> </Dialog>

View File

@ -15,11 +15,13 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
import 'isomorphic-fetch'; import 'isomorphic-fetch';
import 'mock-local-storage';
import es6Promise from 'es6-promise'; import es6Promise from 'es6-promise';
es6Promise.polyfill(); es6Promise.polyfill();
import 'mock-local-storage'; import injectTapEventPlugin from 'react-tap-event-plugin';
injectTapEventPlugin();
import chai from 'chai'; import chai from 'chai';
import chaiAsPromised from 'chai-as-promised'; import chaiAsPromised from 'chai-as-promised';