visual-identity/app/components/proposal-manager/proposal-manager.js

147 lines
4.1 KiB
JavaScript
Raw Normal View History

2018-05-21 21:43:31 +00:00
import EmbarkJS from 'Embark/EmbarkJS';
import ERC20Token from 'Embark/contracts/ERC20Token';
import ProposalCuration from 'Embark/contracts/ProposalCuration';
import SNT from 'Embark/contracts/SNT';
2018-05-31 19:29:50 +00:00
import React, { PureComponent, Fragment } from 'react';
import { Form, FormGroup, FormControl, HelpBlock, Button, Alert } from 'react-bootstrap';
import web3 from "Embark/web3";
import { withFormik } from 'formik';
import FieldGroup from '../standard/FieldGroup';
import TokenPermissions from '../standard/TokenPermission'
2018-05-21 21:43:31 +00:00
2018-05-31 19:29:50 +00:00
const { setSubmitPrice } = ProposalCuration.methods;
class InnerForm extends PureComponent {
2018-05-24 14:27:28 +00:00
constructor(props) {
super(props);
this.state = {
submitPrice: "Loading...",
canSubmit: true
};
}
2018-05-24 14:27:28 +00:00
componentDidMount(){
this._loadPrice();
}
2018-05-24 14:27:28 +00:00
componentWillReceiveProps(){
this._loadPrice();
}
2018-05-24 14:27:28 +00:00
_loadPrice(){
__embarkContext.execWhenReady(async () => {
try {
let _b = await ProposalCuration.methods.getSubmitPrice(web3.eth.defaultAccount).call();
this.setState({
submitPrice: _b,
canSubmit: true
2018-05-24 14:27:28 +00:00
});
} catch(err){
this.setState({
canSubmit: false,
submitPrice: "-"
});
}
});
}
2018-05-24 14:27:28 +00:00
setPrice = (address = web3.eth.defaultAccount, allowed = true, stakeValue = 1) => {
setSubmitPrice(address, allowed, stakeValue)
.send()
.then(res => {
2018-06-25 13:43:08 +00:00
this.setState({ ...this.state, canSubmit: true });
console.log(res);
})
.catch(err => { console.log(err) })
}
2018-05-31 17:29:42 +00:00
render() {
const { values, errors, touched, handleChange, handleBlur, handleSubmit, isSubmitting, setFieldValue } = this.props;
const { canSubmit } = this.state;
return (
<Fragment>
2018-05-31 17:29:42 +00:00
{!canSubmit &&
<Alert bsStyle="warning">
Account not allowed to submit proposals <Button onClick={(e) => this.setPrice()}>Click to enable (Admin only)</Button>
2018-05-31 17:29:42 +00:00
</Alert>
}
<TokenPermissions methods={SNT.methods} spender={ProposalCuration._address} symbol='SNT' />
<hr/>
2018-05-31 17:29:42 +00:00
<h2>Add proposal</h2>
<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>
</Fragment>
)
}
2018-05-21 21:43:31 +00:00
}
const ProposalManager = withFormik({
2018-05-31 17:29:42 +00:00
mapPropsToValues: props => ({ title: '', description: '', url: '' }),
validate(values) {},
2018-05-31 17:29:42 +00:00
handleSubmit(values, { setSubmitting}){
let dataObj = {
title: values.title,
description: values.description,
url: values.url
};
2018-05-31 17:29:42 +00:00
const { toHex } = web3.utils;
const { submitProposal } = ProposalCuration.methods;
EmbarkJS.Storage.saveText(JSON.stringify(dataObj))
2018-05-31 17:29:42 +00:00
.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);
2018-05-31 19:29:50 +00:00
//TODO show error
2018-05-31 17:29:42 +00:00
console.log('Storage saveText Error: ', err.message)
});
})
}
})(InnerForm)
2018-05-21 21:43:31 +00:00
export default ProposalManager;