add support automatic detection of existing and setting of price

This commit is contained in:
Barry Gitarts 2018-05-24 14:30:01 -04:00
parent 576b7e35e3
commit 8ad6dc648c
1 changed files with 25 additions and 12 deletions

View File

@ -3,6 +3,13 @@ import React, { Fragment } from 'react';
import { Form, FormGroup, FormControl, HelpBlock, Button, ControlLabel } from 'react-bootstrap';
import { withFormik } from 'formik';
import { hash } from 'eth-ens-namehash'
import { debounce } from 'lodash/fp'
const delay = debounce(1000);
const getDomain = (hashedDomain, domains) => domains(hashedDomain).call();
const fetchDomain = delay(getDomain);
const setPrice = (domainFn, hashedDomain, price) => domainFn(hashedDomain, price || 0).send()
const FieldGroup = ({ id, label, error, ...props }) => (
<FormGroup controlId={id} validationState={error ? 'error' : null}>
@ -50,22 +57,28 @@ const AddDomain = withFormik({
mapPropsToValues: props => ({ domainName: '', domainPrice: '' }),
validate(values, props) {
const errors = {};
if (!values.domainName) errors.domainName = 'Required';
if (!domainName) errors.domainName = 'Required';
return errors;
},
handleSubmit(values, { setSubmitting }) {
const { domainName, domainPrice } = values
const { methods: { addDomain } } = ENSSubdomainRegistry
addDomain(hash(domainName), domainPrice || 0)
.send()
.then(res => {
setSubmitting(false);
console.log(res);
})
.catch(err => {
setSubmitting(false);
console.log(err);
})
const { methods: { domains, addDomain, setDomainPrice } } = ENSSubdomainRegistry
const hashedDomain = hash(domainName);
getDomain(hashedDomain, domains).then(({ state }) => {
setPrice(
!!state ? setDomainPrice : addDomain,
hashedDomain,
domainPrice
)
.then(res => {
setSubmitting(false);
console.log(res);
})
.catch(err => {
setSubmitting(false);
console.log(err);
})
})
}
})(InnerForm)