ens-usernames/app/components/ens/addDomain.js

93 lines
3.0 KiB
JavaScript
Raw Normal View History

import UsernameRegistrar from 'Embark/contracts/UsernameRegistrar';
import web3 from 'web3';
import ENSRegistry from 'Embark/contracts/ENSRegistry';
import React 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';
import { debounce } from 'lodash/fp';
const { methods: { owner } } = ENSRegistry;
const delay = debounce(500);
const getRegistry = (hashedRegistry, registrys) => registrys(hashedRegistry).call();
const registryIsOwner = address => address == UsernameRegistrar._address;
const fetchOwner = registryName => owner(hash(registryName)).call();
const debounceFetchOwner = delay(fetchOwner);
const getAndIsOwner = async registryName => {
const address = await debounceFetchOwner(registryName);
return registryIsOwner(address);
}
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
id="registryName"
name="registryName"
2018-05-22 20:40:56 +00:00
type="text"
label="Registry Name"
2018-05-22 20:40:56 +00:00
onChange={handleChange}
onBlur={handleBlur}
value={values.registryName}
error={errors.registryName}
2018-05-22 20:40:56 +00:00
/>
<FieldGroup
id="registryPrice"
name="registryPrice"
2018-05-22 20:40:56 +00:00
type="number"
label="Registry Price"
placeholder="(Optional) Registry will be free if left blank"
2018-05-22 20:40:56 +00:00
onChange={handleChange}
onBlur={handleBlur}
value={values.registryPrice}
2018-05-22 20:40:56 +00:00
/>
<Button bsStyle="primary" type="submit" disabled={isSubmitting || !!Object.keys(errors).length}>{!isSubmitting ? 'Submit' : 'Submitting to the Blockchain - (this may take awhile)'}</Button>
2018-05-22 20:40:56 +00:00
</form>
)
const AddRegistry = withFormik({
mapPropsToValues: props => ({ registryName: '', registryPrice: '' }),
async validate(values) {
const { registryName } = values;
2018-05-22 20:40:56 +00:00
const errors = {};
if (!registryName) errors.registryName = 'Required';
if (registryName && !await getAndIsOwner(registryName)) errors.registryName = 'This registry 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 }) {
const { registryName, registryPrice } = values;
const { methods: { state, activate, updateRegistryPrice } } = UsernameRegistrar;
const { registryState } = await state();
console.log(
'Inputs for setPrice',
Number(registryState) ? 'updateRegistryPrice' : 'activate',
web3.utils.toWei(registryPrice.toString(), 'ether'),
);
2018-05-25 15:39:11 +00:00
setPrice(
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
}
})(InnerForm);
2018-05-22 20:40:56 +00:00
export default AddRegistry;