1 import React from 'react'
2 import styles from './XpubField.scss'
3 import { SelectField, FieldLabel, TextField } from '../'
4 import { connect } from 'react-redux'
5 import actions from 'features/mockhsm/actions'
6 import {withNamespaces} from 'react-i18next'
8 const methodOptions = ['mockhsm', 'provide']
10 class XpubField extends React.Component {
23 if (!this.props.autocompleteIsLoaded) {
24 this.props.fetchAll().then(() => {
25 this.props.didLoadAutocomplete()
29 this.props.typeProps.onChange(methodOptions[0])
40 const typeOnChange = event => {
41 const value = typeProps.onChange(event).value
42 valueProps.onChange(this.state[value] || '')
43 this.setState({ autofocusInput: true })
46 const valueOnChange = event => {
47 const value = valueProps.onChange(event).value
48 this.setState({ [typeProps.value]: value })
52 'mockhsm': <SelectField options={mockhsmKeys}
53 autoFocus={this.state.autofocusInput}
56 fieldProps={{...valueProps, onChange: valueOnChange}} />,
58 autoFocus={this.state.autofocusInput}
59 fieldProps={{...valueProps, onChange: valueOnChange}}
60 placeholder={ t('xpub.providePlaceholder') } />,
61 'generate': <TextField
62 autoFocus={this.state.autofocusInput}
63 fieldProps={{...valueProps, onChange: valueOnChange}}
64 placeholder='Alias for generated key (leave blank for automatic value)' />,
68 <div className={styles.main}>
69 <FieldLabel>{t('form.key')}{this.props.index + 1}</FieldLabel>
71 <table className={styles.options}>
73 {methodOptions.map((key) =>
74 <tr key={`key-${this.props.index}-option-${key}`}>
75 <td className={styles.label}>
78 className={styles.radio}
79 name={`keys-${this.props.index}`}
80 onChange={typeOnChange}
81 checked={key == typeProps.value}
84 { t('xpub.methodOptions', { returnObjects: true })[key]}
88 <td className={styles.field}>
89 {typeProps.value == key && fields[key]}
100 XpubField.propTypes = {
101 index: React.PropTypes.number,
102 typeProps: React.PropTypes.object,
103 valueProps: React.PropTypes.object,
104 mockhsmKeys: React.PropTypes.array,
105 autocompleteIsLoaded: React.PropTypes.bool,
106 fetchAll: React.PropTypes.func,
107 didLoadAutocomplete: React.PropTypes.func,
110 export default connect(
113 for (var key in state.key.items) {
114 const item = state.key.items[key]
117 label: item.alias ? item.alias : item.id.slice(0, 32) + '...'
122 autocompleteIsLoaded: state.key.autocompleteIsLoaded,
127 didLoadAutocomplete: () => dispatch(actions.didLoadAutocomplete),
128 fetchAll: (cb) => dispatch(actions.fetchAll(cb)),
130 )(withNamespaces('translations')(XpubField))