2018-05-21 17:43:31 -04:00
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
|
|
|
import ERC20Token from 'Embark/contracts/ERC20Token';
|
2018-05-29 16:51:59 -04:00
|
|
|
import ProposalCuration from 'Embark/contracts/ProposalCuration';
|
|
|
|
import SNT from 'Embark/contracts/SNT';
|
2018-05-29 20:29:40 -04:00
|
|
|
import React, { Component, Fragment } from 'react';
|
2018-05-29 16:51:59 -04:00
|
|
|
import { Form, FormGroup, FormControl, HelpBlock, Button, Alert } from 'react-bootstrap';
|
2018-05-31 09:53:26 -04:00
|
|
|
import web3 from "Embark/web3";
|
|
|
|
import { withFormik } from 'formik';
|
|
|
|
import FieldGroup from '../standard/FieldGroup';
|
2018-05-21 17:43:31 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
//TODO make innerform and wrap
|
|
|
|
class InnerForm extends Component {
|
2018-05-24 10:27:28 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
submitPrice: "Loading...",
|
|
|
|
url: "",
|
|
|
|
title: "",
|
|
|
|
description: "",
|
|
|
|
canSubmit: true
|
|
|
|
};
|
|
|
|
}
|
2018-05-24 10:27:28 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
componentDidMount(){
|
|
|
|
this.loadPrice();
|
|
|
|
}
|
2018-05-24 10:27:28 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
componentWillReceiveProps(){
|
|
|
|
this.loadPrice();
|
|
|
|
}
|
2018-05-24 10:27:28 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
async loadPrice(){
|
|
|
|
__embarkContext.execWhenReady(async () => {
|
|
|
|
try {
|
|
|
|
let _b = await ProposalCuration.methods.getSubmitPrice(web3.eth.defaultAccount).call();
|
|
|
|
this.setState({
|
|
|
|
submitPrice: _b,
|
|
|
|
canSubmit: true
|
2018-05-24 10:27:28 -04:00
|
|
|
});
|
2018-05-31 09:53:26 -04:00
|
|
|
} catch(err){
|
|
|
|
this.setState({
|
|
|
|
canSubmit: false,
|
|
|
|
submitPrice: "-"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-05-24 10:27:28 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
async handleClick(){
|
|
|
|
let description = {
|
|
|
|
"url": this.state.url,
|
|
|
|
"title": this.state.title,
|
|
|
|
"description": this.state.description
|
|
|
|
};
|
2018-05-29 16:51:59 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
EmbarkJS.Storage.saveText(JSON.stringify(description))
|
2018-05-29 20:29:40 -04:00
|
|
|
.then(async (hash) => {
|
2018-05-31 09:53:26 -04:00
|
|
|
let hexHash = web3.utils.toHex(hash);
|
2018-05-29 16:51:59 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
let receipt = await SNT.methods.approve(
|
|
|
|
ProposalCuration.options.address,
|
|
|
|
this.state.submitPrice)
|
|
|
|
.send({from: web3.eth.defaultAccount, gasLimit: 1000000});
|
2018-05-29 16:51:59 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
console.log(receipt);
|
2018-05-29 16:51:59 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
receipt = await ProposalCuration.methods.submitProposal(
|
|
|
|
"0x00",
|
|
|
|
"0x0000000000000000000000000000000000000000",
|
|
|
|
0,
|
2018-05-29 20:29:40 -04:00
|
|
|
"0x00",
|
|
|
|
hexHash
|
2018-05-31 09:53:26 -04:00
|
|
|
)
|
|
|
|
.send({from: web3.eth.defaultAccount, gasLimit: 1000000});
|
2018-05-29 20:29:40 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
console.log(receipt);
|
|
|
|
})
|
2018-05-29 20:29:40 -04:00
|
|
|
.catch((err) => {
|
2018-05-31 09:53:26 -04:00
|
|
|
if(err){
|
|
|
|
// TODO show error
|
|
|
|
console.log("Storage saveText Error => " + err.message);
|
|
|
|
}
|
2018-05-29 20:29:40 -04:00
|
|
|
});
|
2018-05-31 09:53:26 -04:00
|
|
|
}
|
2018-05-29 16:51:59 -04:00
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
|
2018-05-31 13:29:42 -04:00
|
|
|
render() {
|
2018-05-31 09:53:26 -04:00
|
|
|
const { values, errors, touched, handleChange, handleBlur, handleSubmit, isSubmitting, setFieldValue } = this.props;
|
|
|
|
const { canSubmit } = this.state;
|
|
|
|
return (
|
|
|
|
<Fragment>
|
2018-05-31 13:29:42 -04:00
|
|
|
{!canSubmit &&
|
|
|
|
<Alert bsStyle="warning">
|
|
|
|
Account not allowed to submit proposals <Button>Click to enable (Admin only)</Button>
|
|
|
|
</Alert>
|
2018-05-31 09:53:26 -04:00
|
|
|
}
|
2018-05-31 13:29:42 -04:00
|
|
|
<h2>Add proposal</h2>
|
|
|
|
<p>Execute this on the console if proposal submit is not allowed</p>
|
|
|
|
<code>await ProposalCuration.methods.setSubmitPrice(web3.eth.defaultAccount, true, 1).send();</code>
|
|
|
|
<h3>Price: {this.state.submitPrice}</h3>
|
|
|
|
<Form onSubmit={handleSubmit}>
|
|
|
|
<FieldGroup
|
|
|
|
id="title"
|
|
|
|
name="title"
|
|
|
|
type="text"
|
|
|
|
label="Title"
|
|
|
|
onChange={handleChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
value={values.title}
|
|
|
|
/>
|
|
|
|
<FieldGroup
|
|
|
|
id="description"
|
|
|
|
name="description"
|
|
|
|
type="text"
|
|
|
|
label="Description"
|
|
|
|
onChange={handleChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
value={values.description}
|
|
|
|
/>
|
|
|
|
<FieldGroup
|
|
|
|
id="url"
|
|
|
|
name="url"
|
|
|
|
type="text"
|
|
|
|
label="URL"
|
|
|
|
onChange={handleChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
value={values.url}
|
|
|
|
/>
|
|
|
|
<Button type="submit" disabled={!canSubmit || isSubmitting}>{isSubmitting ? 'Submission in progress' : 'Submit'}</Button>
|
|
|
|
</Form>
|
2018-05-31 09:53:26 -04:00
|
|
|
</Fragment>
|
|
|
|
)
|
|
|
|
}
|
2018-05-21 17:43:31 -04:00
|
|
|
}
|
|
|
|
|
2018-05-31 09:53:26 -04:00
|
|
|
const ProposalManager = withFormik({
|
2018-05-31 13:29:42 -04:00
|
|
|
mapPropsToValues: props => ({ title: '', description: '', url: '' }),
|
2018-05-31 09:53:26 -04:00
|
|
|
validate(values) {},
|
2018-05-31 13:29:42 -04:00
|
|
|
handleSubmit(values, { setSubmitting}){
|
|
|
|
const { title, description, url } = values;
|
|
|
|
const { saveText } = EmbarkJS.Storage;
|
|
|
|
const { toHex } = web3.utils;
|
|
|
|
const { submitProposal } = ProposalCuration.methods;
|
|
|
|
saveText(JSON.stringify(description))
|
|
|
|
.then(hash => {
|
|
|
|
const hexHash = toHex(hash);
|
|
|
|
//TODO create toggle for address approval
|
|
|
|
submitProposal(
|
|
|
|
"0x00",
|
|
|
|
"0x0000000000000000000000000000000000000000",
|
|
|
|
0,
|
|
|
|
"0x00",
|
|
|
|
hexHash
|
|
|
|
)
|
|
|
|
.send({from: web3.eth.defaultAccount, gasLimit: 1000000})
|
|
|
|
.then(res => {
|
|
|
|
setSubmitting(false);
|
|
|
|
console.log(res);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
setSubmitting(false);
|
|
|
|
console.log('Storage saveText Error: ', err.message)
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2018-05-31 09:53:26 -04:00
|
|
|
})(InnerForm)
|
|
|
|
|
2018-05-21 17:43:31 -04:00
|
|
|
export default ProposalManager;
|