2018-09-04 21:49:39 +00:00
import UsernameRegistrar from 'Embark/contracts/UsernameRegistrar' ;
2018-06-25 18:47:47 +00:00
import web3 from 'web3' ;
2018-05-25 22:50:16 +00:00
import ENSRegistry from 'Embark/contracts/ENSRegistry' ;
2018-06-25 18:47:47 +00:00
import React from 'react' ;
2018-05-27 20:28:07 +00:00
import { Button } from 'react-bootstrap' ;
2018-06-25 18:47:47 +00:00
import FieldGroup from '../standard/FieldGroup' ;
2018-05-22 20:40:56 +00:00
import { withFormik } from 'formik' ;
2018-06-25 18:47:47 +00:00
import { hash } from 'eth-ens-namehash' ;
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-06-25 18:47:47 +00:00
const delay = debounce ( 500 ) ;
2018-09-04 21:49:39 +00:00
const getRegistry = ( hashedRegistry , registrys ) => registrys ( hashedRegistry ) . call ( ) ;
const registryIsOwner = address => address == UsernameRegistrar . _address ;
const fetchOwner = registryName => owner ( hash ( registryName ) ) . call ( ) ;
2018-05-25 23:06:25 +00:00
const debounceFetchOwner = delay ( fetchOwner ) ;
2018-09-04 21:49:39 +00:00
const getAndIsOwner = async registryName => {
const address = await debounceFetchOwner ( registryName ) ;
2018-05-25 22:50:16 +00:00
return registryIsOwner ( address ) ;
}
2018-09-04 21:49:39 +00:00
const fetchRegistry = delay ( getRegistry ) ;
const setPrice = ( registryFn , price ) => registryFn ( price || 0 ) . send ( ) ;
2018-05-22 20:40:56 +00:00
const InnerForm = ( {
values ,
errors ,
touched ,
handleChange ,
handleBlur ,
handleSubmit ,
isSubmitting ,
} ) => (
< form onSubmit = { handleSubmit } >
< FieldGroup
2018-09-04 21:49:39 +00:00
id = "registryName"
name = "registryName"
2018-05-22 20:40:56 +00:00
type = "text"
2018-09-04 21:49:39 +00:00
label = "Registry Name"
2018-05-22 20:40:56 +00:00
onChange = { handleChange }
onBlur = { handleBlur }
2018-09-04 21:49:39 +00:00
value = { values . registryName }
error = { errors . registryName }
2018-05-22 20:40:56 +00:00
/ >
< FieldGroup
2018-09-04 21:49:39 +00:00
id = "registryPrice"
name = "registryPrice"
2018-05-22 20:40:56 +00:00
type = "number"
2018-09-04 21:49:39 +00:00
label = "Registry Price"
placeholder = "(Optional) Registry will be free if left blank"
2018-05-22 20:40:56 +00:00
onChange = { handleChange }
onBlur = { handleBlur }
2018-09-04 21:49:39 +00:00
value = { values . registryPrice }
2018-05-22 20:40:56 +00:00
/ >
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 >
)
2018-09-04 21:49:39 +00:00
const AddRegistry = withFormik ( {
mapPropsToValues : props => ( { registryName : '' , registryPrice : '' } ) ,
2018-06-25 18:47:47 +00:00
async validate ( values ) {
2018-09-04 21:49:39 +00:00
const { registryName } = values ;
2018-05-22 20:40:56 +00:00
const errors = { } ;
2018-09-04 21:49:39 +00:00
if ( ! registryName ) errors . registryName = 'Required' ;
if ( registryName && ! await getAndIsOwner ( registryName ) ) errors . registryName = 'This registry is not owned by registry' ;
2018-05-25 22:50:16 +00:00
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-09-04 21:49:39 +00:00
const { registryName , registryPrice } = values ;
const { methods : { state , activate , updateRegistryPrice } } = UsernameRegistrar ;
const { registryState } = await state ( ) ;
2018-08-09 15:56:19 +00:00
console . log (
'Inputs for setPrice' ,
2018-09-04 21:49:39 +00:00
Number ( registryState ) ? 'updateRegistryPrice' : 'activate' ,
web3 . utils . toWei ( registryPrice . toString ( ) , 'ether' ) ,
2018-08-09 15:56:19 +00:00
) ;
2018-05-25 15:39:11 +00:00
setPrice (
2018-09-04 21:49:39 +00:00
Number ( registryState ) ? updateRegistryPrice : activate ,
web3 . utils . toWei ( registryPrice . toString ( ) , 'ether' ) ,
2018-05-25 15:39:11 +00:00
)
. then ( res => {
setSubmitting ( false ) ;
console . log ( res ) ;
} )
. catch ( err => {
setSubmitting ( false ) ;
console . log ( err ) ;
} )
2018-05-22 20:40:56 +00:00
}
2018-06-25 18:47:47 +00:00
} ) ( InnerForm ) ;
2018-05-22 20:40:56 +00:00
2018-09-04 21:49:39 +00:00
export default AddRegistry ;