2018-05-22 20:40:56 +00:00
import ENSSubdomainRegistry from 'Embark/contracts/ENSSubdomainRegistry' ;
2018-05-25 22:50:16 +00:00
import ENSRegistry from 'Embark/contracts/ENSRegistry' ;
2018-05-22 20:40:56 +00:00
import React , { Fragment } from 'react' ;
2018-05-27 20:28:07 +00:00
import { Button } from 'react-bootstrap' ;
import FieldGroup from '../standard/FieldGroup'
2018-05-22 20:40:56 +00:00
import { withFormik } from 'formik' ;
import { hash } from 'eth-ens-namehash'
2018-05-24 18:30:01 +00:00
import { debounce } from 'lodash/fp'
2018-05-25 22:50:16 +00:00
const { methods : { owner } } = ENSRegistry ;
2018-05-24 18:30:01 +00:00
2018-05-25 23:06:25 +00:00
const delay = debounce ( 250 ) ;
2018-05-24 18:30:01 +00:00
const getDomain = ( hashedDomain , domains ) => domains ( hashedDomain ) . call ( ) ;
2018-05-25 22:50:16 +00:00
const registryIsOwner = address => address == ENSSubdomainRegistry . _address ;
2018-05-25 23:06:25 +00:00
const fetchOwner = domainName => owner ( hash ( domainName ) ) . call ( ) ;
const debounceFetchOwner = delay ( fetchOwner ) ;
2018-05-25 22:50:16 +00:00
const getAndIsOwner = async domainName => {
2018-05-25 23:06:25 +00:00
const address = await debounceFetchOwner ( domainName ) ;
2018-05-25 22:50:16 +00:00
return registryIsOwner ( address ) ;
}
2018-05-24 18:30:01 +00:00
const fetchDomain = delay ( getDomain ) ;
2018-05-25 22:50:16 +00:00
const setPrice = ( domainFn , hashedDomain , price ) => domainFn ( hashedDomain , price || 0 ) . send ( ) ;
2018-05-22 20:40:56 +00:00
const InnerForm = ( {
values ,
errors ,
touched ,
handleChange ,
handleBlur ,
handleSubmit ,
isSubmitting ,
} ) => (
< form onSubmit = { handleSubmit } >
< FieldGroup
id = "domainName"
name = "domainName"
type = "text"
label = "Domain Name"
onChange = { handleChange }
onBlur = { handleBlur }
value = { values . domainName }
error = { errors . domainName }
/ >
< FieldGroup
id = "domainPrice"
name = "domainPrice"
type = "number"
label = "Domain Price"
placeholder = "(Optional) Domain will be free if left blank"
onChange = { handleChange }
onBlur = { handleBlur }
value = { values . domainPrice }
/ >
2018-05-25 22:50:16 +00:00
< Button bsStyle = "primary" type = "submit" disabled = { isSubmitting || ! ! Object . keys ( errors ) . length } > { ! isSubmitting ? 'Submit' : 'Submitting to the Blockchain - (this may take awhile)' } < / B u t t o n >
2018-05-22 20:40:56 +00:00
< / f o r m >
)
const AddDomain = withFormik ( {
mapPropsToValues : props => ( { domainName : '' , domainPrice : '' } ) ,
2018-05-25 22:50:16 +00:00
async validate ( values , props ) {
const { domainName } = values
2018-05-22 20:40:56 +00:00
const errors = { } ;
2018-05-24 18:30:01 +00:00
if ( ! domainName ) errors . domainName = 'Required' ;
2018-05-25 22:50:16 +00:00
if ( domainName && ! await getAndIsOwner ( domainName ) ) errors . domainName = 'This domain is not owned by registry'
if ( Object . keys ( errors ) . length ) throw errors ;
2018-05-22 20:40:56 +00:00
} ,
2018-05-25 15:39:11 +00:00
async handleSubmit ( values , { setSubmitting } ) {
2018-05-22 20:40:56 +00:00
const { domainName , domainPrice } = values
2018-05-30 15:19:39 +00:00
const { methods : { domains , setDomainPrice , updateDomainPrice } } = ENSSubdomainRegistry
2018-05-25 19:22:55 +00:00
const { sha3 } = window . web3 . utils
2018-05-25 22:50:16 +00:00
const hashedDomain = hash ( domainName )
2018-05-25 15:39:11 +00:00
const { state } = await getDomain ( hashedDomain , domains ) ;
setPrice (
2018-05-30 15:19:39 +00:00
! ! Number ( state ) ? updateDomainPrice : setDomainPrice ,
2018-05-25 15:39:11 +00:00
hashedDomain ,
domainPrice
)
. then ( res => {
setSubmitting ( false ) ;
console . log ( res ) ;
} )
. catch ( err => {
setSubmitting ( false ) ;
console . log ( err ) ;
} )
2018-05-22 20:40:56 +00:00
}
} ) ( InnerForm )
export default AddDomain ;