2018-06-26 20:34:52 +00:00
|
|
|
import React, { Fragment } from 'react';
|
|
|
|
import Card from '@material-ui/core/Card';
|
|
|
|
import CardActions from '@material-ui/core/CardActions';
|
|
|
|
import CardContent from '@material-ui/core/CardContent';
|
|
|
|
import PollManager from 'Embark/contracts/PollManager';
|
|
|
|
import TextField from '@material-ui/core/TextField';
|
|
|
|
import Button from '@material-ui/core/Button';
|
2018-06-28 14:53:33 +00:00
|
|
|
import CircularProgress from '@material-ui/core/CircularProgress';
|
2018-06-26 20:34:52 +00:00
|
|
|
import { withStyles } from '@material-ui/core/styles';
|
|
|
|
import { withFormik } from 'formik';
|
|
|
|
|
2018-06-26 21:05:58 +00:00
|
|
|
const oneDayinBlocks = 5760;
|
|
|
|
|
2018-06-26 20:34:52 +00:00
|
|
|
const styles = theme => ({
|
|
|
|
button: {
|
|
|
|
margin: theme.spacing.unit,
|
|
|
|
},
|
|
|
|
extendedIcon: {
|
|
|
|
marginRight: theme.spacing.unit,
|
|
|
|
},
|
|
|
|
textField: {
|
|
|
|
marginLeft: theme.spacing.unit,
|
|
|
|
marginRight: theme.spacing.unit
|
|
|
|
},
|
|
|
|
inputLabel: {
|
|
|
|
fontSize: '16px'
|
|
|
|
},
|
|
|
|
form: {
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column'
|
2018-06-27 10:46:55 +00:00
|
|
|
},
|
|
|
|
textFieldInput: {
|
|
|
|
fontSize: '16px'
|
|
|
|
},
|
|
|
|
textFieldFormLabel: {
|
|
|
|
fontSize: 18,
|
2018-06-26 20:34:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const InnerForm = ({
|
|
|
|
values,
|
|
|
|
errors,
|
|
|
|
touched,
|
|
|
|
handleChange,
|
|
|
|
handleBlur,
|
|
|
|
handleSubmit,
|
|
|
|
isSubmitting,
|
|
|
|
classes
|
|
|
|
}) => (
|
|
|
|
<Card>
|
|
|
|
<CardContent>
|
|
|
|
<form onSubmit={handleSubmit} className={classes.form}>
|
|
|
|
<TextField
|
|
|
|
id="description"
|
2018-06-27 10:46:55 +00:00
|
|
|
label="Enter your proposal description"
|
2018-06-26 20:34:52 +00:00
|
|
|
className={classes.textField}
|
|
|
|
value={values.description}
|
|
|
|
onChange={handleChange}
|
|
|
|
margin="normal"
|
|
|
|
fullWidth
|
2018-06-28 14:53:33 +00:00
|
|
|
error={!!errors.description}
|
2018-06-27 10:46:55 +00:00
|
|
|
InputProps={{
|
|
|
|
classes: {
|
|
|
|
input: classes.textFieldInput
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
InputLabelProps={{
|
|
|
|
className: classes.textFieldFormLabel
|
|
|
|
}}
|
2018-06-28 14:53:33 +00:00
|
|
|
helperText={errors.description}
|
2018-06-26 20:34:52 +00:00
|
|
|
/>
|
2018-06-27 19:34:06 +00:00
|
|
|
{!isSubmitting ?
|
|
|
|
<Button type="submit" variant="extendedFab" aria-label="add" className={classes.button}>Submit</Button> :
|
2018-06-28 14:53:33 +00:00
|
|
|
<CircularProgress style={{ margin: '10px 10px 10px 50%' }} />
|
2018-06-27 19:34:06 +00:00
|
|
|
}
|
2018-06-26 20:34:52 +00:00
|
|
|
</form>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
)
|
|
|
|
|
|
|
|
const StyledForm = withStyles(styles)(InnerForm);
|
|
|
|
const AddPoll = withFormik({
|
|
|
|
mapPropsToValues: props => ({ description: ''}),
|
2018-06-27 19:00:50 +00:00
|
|
|
validate(values, props){
|
2018-06-26 20:34:52 +00:00
|
|
|
const errors = {};
|
2018-06-27 19:00:50 +00:00
|
|
|
const { description } = values;
|
|
|
|
if(description.toString().trim() === "") errors.description = true;
|
2018-06-26 20:34:52 +00:00
|
|
|
return errors;
|
|
|
|
},
|
2018-06-28 14:53:33 +00:00
|
|
|
async handleSubmit(values, { setSubmitting, setErrors, props }) {
|
2018-06-26 21:05:58 +00:00
|
|
|
const { description } = values;
|
2018-06-26 21:15:20 +00:00
|
|
|
const { eth: { getBlockNumber }, utils: { asciiToHex } } = window.web3;
|
2018-06-26 21:05:58 +00:00
|
|
|
const { addPoll } = PollManager.methods;
|
|
|
|
const currentBlock = await getBlockNumber();
|
2018-06-26 21:19:26 +00:00
|
|
|
const endTime = currentBlock + (oneDayinBlocks * 90);
|
2018-06-27 21:23:27 +00:00
|
|
|
const toSend = addPoll(endTime, description);
|
2018-06-27 19:10:16 +00:00
|
|
|
|
|
|
|
setSubmitting(true);
|
|
|
|
|
2018-06-27 18:44:37 +00:00
|
|
|
toSend.estimateGas()
|
2018-06-28 14:53:33 +00:00
|
|
|
.then(gasEstimated => {
|
|
|
|
console.log("addPoll gas estimated: "+gasEstimated);
|
|
|
|
return toSend.send({gas: gasEstimated + 100000});
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
console.log('sucess:', res);
|
|
|
|
setSubmitting(false);
|
|
|
|
props.togglePoll();
|
|
|
|
})
|
|
|
|
.catch(res => {
|
|
|
|
console.log('fail:', res);
|
|
|
|
setErrors({ 'description': res.message.split('Error:').pop().trim() });
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setSubmitting(false);
|
|
|
|
});
|
2018-06-26 20:34:52 +00:00
|
|
|
}
|
|
|
|
})(StyledForm)
|
|
|
|
|
|
|
|
export default AddPoll;
|