Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Clean-Up #3314
Browse files Browse the repository at this point in the history
  • Loading branch information
ngotchac committed Nov 14, 2016
1 parent 97378da commit 2944267
Show file tree
Hide file tree
Showing 9 changed files with 440 additions and 351 deletions.
337 changes: 3 additions & 334 deletions js/src/modals/DeployContract/DetailsStep/detailsStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,223 +15,13 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import React, { Component, PropTypes } from 'react';
import { MenuItem } from 'material-ui';
import { range } from 'lodash';

import IconButton from 'material-ui/IconButton';
import AddIcon from 'material-ui/svg-icons/content/add';
import RemoveIcon from 'material-ui/svg-icons/content/remove';

import { AddressSelect, Form, Input, InputAddressSelect, Select } from '../../../ui';
import { AddressSelect, Form, Input, TypedInput } from '../../../ui';
import { validateAbi } from '../../../util/validation';
import { parseAbiType } from '../../../util/abi';

import styles from '../deployContract.css';

class TypedInput extends Component {

static propTypes = {
onChange: PropTypes.func.isRequired,
accounts: PropTypes.object.isRequired,
param: PropTypes.object.isRequired,

error: PropTypes.any,
value: PropTypes.any,
label: PropTypes.string
};

render () {
const { param } = this.props;
const { type } = param;

if (type === ARRAY_TYPE) {
const { accounts, label, value = param.default } = this.props;
const { subtype, length } = param;

const fixedLength = !!length;

const inputs = range(length || value.length).map((_, index) => {
const onChange = (inputValue) => {
const newValues = [].concat(this.props.value);
newValues[index] = inputValue;
this.props.onChange(newValues);
};

return (
<TypedInput
key={ `${subtype.type}_${index}` }
onChange={ onChange }
accounts={ accounts }
param={ subtype }
value={ value[index] }
/>
);
});

return (
<div className={ styles.inputs }>
<label>{ label }</label>
{ fixedLength ? null : this.renderLength() }
{ inputs }
</div>
);
}

return this.renderType(type);
}

renderLength () {
const style = {
width: 16,
height: 16
};

return (
<div>
<IconButton
iconStyle={ style }
style={ style }
onClick={ this.onAddField }
>
<AddIcon />
</IconButton>

<IconButton
iconStyle={ style }
style={ style }
onClick={ this.onRemoveField }
>
<RemoveIcon />
</IconButton>
</div>
);
}

renderType (type) {
if (type === ADDRESS_TYPE) {
return this.renderAddress();
}

if (type === BOOL_TYPE) {
return this.renderBoolean();
}

if (type === STRING_TYPE) {
return this.renderDefault();
}

if (type === BYTES_TYPE) {
return this.renderDefault();
}

if (type === INT_TYPE) {
return this.renderNumber();
}

if (type === FIXED_TYPE) {
return this.renderNumber();
}

return this.renderDefault();
}

renderNumber () {
const { label, value, error, param } = this.props;

return (
<Input
label={ label }
value={ value }
error={ error }
onSubmit={ this.onSubmit }
type='number'
min={ param.signed ? null : 0 }
/>
);
}

renderDefault () {
const { label, value, error } = this.props;

return (
<Input
label={ label }
value={ value }
error={ error }
onSubmit={ this.onSubmit }
/>
);
}

renderAddress () {
const { accounts, label, value, error } = this.props;

return (
<InputAddressSelect
accounts={ accounts }
label={ label }
value={ value }
error={ error }
onChange={ this.onChange }
editing
/>
);
}

renderBoolean () {
const { label, value, error } = this.props;

const boolitems = ['false', 'true'].map((bool) => {
return (
<MenuItem
key={ bool }
value={ bool }
label={ bool }
>
{ bool }
</MenuItem>
);
});

return (
<Select
label={ label }
value={ value ? 'true' : 'false' }
error={ error }
onChange={ this.onChangeBool }
>
{ boolitems }
</Select>
);
}

onChangeBool = (event, _index, value) => {
this.props.onChange(value === 'true');
}

onChange = (event, value) => {
this.props.onChange(value);
}

onSubmit = (value) => {
this.props.onChange(value);
}

onAddField = () => {
const { value, onChange, param } = this.props;
const newValues = [].concat(value, param.subtype.default);

onChange(newValues);
}

onRemoveField = () => {
const { value, onChange } = this.props;
const newValues = value.slice(0, -1);

onChange(newValues);
}

}

export default class DetailsStep extends Component {
static contextTypes = {
api: PropTypes.object.isRequired
Expand Down Expand Up @@ -313,6 +103,7 @@ export default class DetailsStep extends Component {
value={ code }
onSubmit={ this.onCodeChange }
readOnly={ readOnly } />

{ this.renderConstructorInputs() }
</Form>
);
Expand Down Expand Up @@ -400,125 +191,3 @@ export default class DetailsStep extends Component {
onCodeChange(code);
}
}

const ARRAY_TYPE = 'ARRAY_TYPE';
const ADDRESS_TYPE = 'ADDRESS_TYPE';
const STRING_TYPE = 'STRING_TYPE';
const BOOL_TYPE = 'BOOL_TYPE';
const BYTES_TYPE = 'BYTES_TYPE';
const INT_TYPE = 'INT_TYPE';
const FIXED_TYPE = 'FIXED_TYPE';

function parseAbiType (type) {
const arrayRegex = /^(.+)\[(\d*)\]$/;

if (arrayRegex.test(type)) {
const matches = arrayRegex.exec(type);

const subtype = parseAbiType(matches[1]);
const M = parseInt(matches[2]) || null;
const defaultValue = !M
? []
: range(M).map(() => subtype.default);

return {
type: ARRAY_TYPE,
subtype: subtype,
length: M,
default: defaultValue
};
}

const lengthRegex = /^(u?int|bytes)(\d{1,3})$/;

if (lengthRegex.test(type)) {
const matches = lengthRegex.exec(type);

const subtype = parseAbiType(matches[1]);
const length = parseInt(matches[2]);

return {
...subtype,
length
};
}

const fixedLengthRegex = /^(u?fixed)(\d{1,3})x(\d{1,3})$/;

if (fixedLengthRegex.test(type)) {
const matches = fixedLengthRegex.exec(type);

const subtype = parseAbiType(matches[1]);
const M = parseInt(matches[2]);
const N = parseInt(matches[3]);

return {
...subtype,
M, N
};
}

if (type === 'string') {
return {
type: STRING_TYPE,
default: ''
};
}

if (type === 'bool') {
return {
type: BOOL_TYPE,
default: false
};
}

if (type === 'address') {
return {
type: ADDRESS_TYPE,
default: '0x'
};
}

if (type === 'bytes') {
return {
type: BYTES_TYPE,
default: '0x'
};
}

if (type === 'uint') {
return {
type: INT_TYPE,
default: 0,
length: 256,
signed: false
};
}

if (type === 'int') {
return {
type: INT_TYPE,
default: 0,
length: 256,
signed: true
};
}

if (type === 'ufixed') {
return {
type: FIXED_TYPE,
default: 0,
length: 256,
signed: false
};
}

if (type === 'fixed') {
return {
type: FIXED_TYPE,
default: 0,
length: 256,
signed: true
};
}
}
14 changes: 0 additions & 14 deletions js/src/modals/DeployContract/deployContract.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,3 @@
.funcparams {
padding-left: 3em;
}

.inputs {
padding-top: 2px;

label {
line-height: 22px;
pointer-events: none;
color: rgba(255, 255, 255, 0.498039);
-webkit-user-select: none;
font-size: 12px;
top: 11px;
position: relative;
}
}
2 changes: 0 additions & 2 deletions js/src/modals/DeployContract/deployContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,6 @@ export default class DeployContract extends Component {
return;
}

console.log('onDeploymentState', data);

switch (data.state) {
case 'estimateGas':
case 'postTransaction':
Expand Down
Loading

0 comments on commit 2944267

Please sign in to comment.