import React from 'react'; import ContractContext from './contract-context'; import PropTypes from 'prop-types'; class InstanceSelector extends React.Component { constructor(props) { super(props); this.state = { showInstances: false, showCustomAddressField: false, selectedInstance: props.selectedInstance, customInstance: "", error: false, errorMessage: "" }; this.handleShowInstances = this.handleShowInstances.bind(this); this.handleChange = this.handleChange.bind(this); this.handleClick = this.handleClick.bind(this); this.handleTextChange = this.handleTextChange.bind(this); } handleTextChange(e) { this.setState({customInstance: e.target.value}); } handleShowInstances(e) { e.preventDefault(); this.setState({ showInstances: !this.state.showInstances }); } handleClick(e) { e.preventDefault(); let instance; if (this.state.selectedInstance === "custom") { instance = this.state.customInstance; } else { instance = this.state.selectedInstance; } if (!(/^0x[0-9a-f]{40}$/i).test(instance)) { this.setState({error: true, errorMessage: 'Not a valid Ethereum address.'}); console.log(this.state.errorMessage); return; } else { this.setState({error: false}); } this.props.instanceUpdate(instance); this.setState({ showInstances: false, showCustomAddressField: false, selectedInstance: instance, customInstance: this.state.selectedInstance === "custom" ? this.state.customInstance : "" }); } handleChange(e) { this.setState({ showCustomAddressField: e.target.value === "custom", selectedInstance: e.target.value }); } render() { let showInstance; if (!this.state.showInstances) { showInstance = Change; } else { showInstance = Cancel; } return {(context) => (

Instance Selected: {this.props.selectedInstance !== null ? this.props.selectedInstance : 'none'}

{showInstance}
{ this.state.showInstances && { this.state.error &&
{this.state.errorMessage}
}
{ this.state.showCustomAddressField && }
}
)}
; } } InstanceSelector.propTypes = { instanceUpdate: PropTypes.func, selectedInstance: PropTypes.string }; export default InstanceSelector;