2018-05-22 20:40:56 +00:00
|
|
|
import ENSSubdomainRegistry from 'Embark/contracts/ENSSubdomainRegistry';
|
|
|
|
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'
|
2018-05-24 18:30:01 +00:00
|
|
|
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()
|
2018-05-22 20:40:56 +00:00
|
|
|
|
|
|
|
const FieldGroup = ({ id, label, error, ...props }) => (
|
|
|
|
<FormGroup controlId={id} validationState={error ? 'error' : null}>
|
|
|
|
<ControlLabel>{label}</ControlLabel>
|
|
|
|
<FormControl {...props} />
|
|
|
|
{error && <HelpBlock>{error}</HelpBlock>}
|
|
|
|
</FormGroup>
|
|
|
|
)
|
|
|
|
|
|
|
|
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}
|
|
|
|
/>
|
|
|
|
<Button bsStyle="primary" type="submit" disabled={isSubmitting}>{!isSubmitting ? 'Submit' : 'Submitting to the Blockchain - (this may take awhile)'}</Button>
|
|
|
|
</form>
|
|
|
|
)
|
|
|
|
|
|
|
|
const AddDomain = withFormik({
|
|
|
|
mapPropsToValues: props => ({ domainName: '', domainPrice: '' }),
|
|
|
|
validate(values, props) {
|
|
|
|
const errors = {};
|
2018-05-24 18:30:01 +00:00
|
|
|
if (!domainName) errors.domainName = 'Required';
|
2018-05-22 20:40:56 +00:00
|
|
|
return errors;
|
|
|
|
},
|
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-24 18:30:01 +00:00
|
|
|
const { methods: { domains, addDomain, setDomainPrice } } = ENSSubdomainRegistry
|
2018-05-25 19:22:55 +00:00
|
|
|
const { sha3 } = window.web3.utils
|
|
|
|
const hashedDomain = sha3(domainName);
|
|
|
|
const debugTable = await ['eth', 'stateofus', 'stateofus.eth', 'freedomain', 'freedomain.eth', domainName]
|
|
|
|
.map(async str => {
|
|
|
|
const result = {};
|
|
|
|
result['domainName'] = str;
|
|
|
|
result['sha3Result'] = await getDomain(sha3(str), domains);
|
|
|
|
result['sha3State'] = result.sha3Result.state
|
|
|
|
result['sha3Price'] = result.sha3Result.price
|
|
|
|
result['nameHashResult'] = await getDomain(hash(str), domains);
|
|
|
|
result['nameHashState'] = result['nameHashResult'].state
|
|
|
|
result['nameHashPrice'] = result['nameHashResult'].price
|
|
|
|
result['sha3String'] = sha3(str);
|
|
|
|
result['nameHashString'] = hash(str);
|
|
|
|
return result;
|
|
|
|
})
|
|
|
|
Promise.all(debugTable).then(v => { console.table(v) });
|
2018-05-25 15:39:11 +00:00
|
|
|
const { state } = await getDomain(hashedDomain, domains);
|
|
|
|
setPrice(
|
|
|
|
!!state ? setDomainPrice : addDomain,
|
|
|
|
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;
|