diff --git a/app/components/simple-voting/AddPoll.js b/app/components/simple-voting/AddPoll.js index d62c220..25f398f 100644 --- a/app/components/simple-voting/AddPoll.js +++ b/app/components/simple-voting/AddPoll.js @@ -5,9 +5,27 @@ 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'; +import { map } from 'lodash'; +import rlp from 'rlp'; import { withStyles } from '@material-ui/core/styles'; import { withFormik } from 'formik'; +const oneDayinBlocks = 5760; + +const singleChoiceDef = (question, options) => { + const d = [ + new Buffer(question), + map(options, function(o) { + return new Buffer(o); + }) + ]; + + const b = rlp.encode(d); + const rlpDefinition = '0x' + b.toString('hex'); + + return rlpDefinition; +} + const styles = theme => ({ button: { margin: theme.spacing.unit, @@ -25,6 +43,12 @@ const styles = theme => ({ form: { display: 'flex', flexDirection: 'column' + }, + textFieldInput: { + fontSize: '16px' + }, + textFieldFormLabel: { + fontSize: 18, } }); @@ -43,13 +67,21 @@ const InnerForm = ({
@@ -64,8 +96,23 @@ const AddPoll = withFormik({ const errors = {}; return errors; }, - handleSubmit(values) { - + async handleSubmit(values, { setSubmitting }) { + const { description } = values; + const { eth: { getBlockNumber }, utils: { asciiToHex } } = window.web3; + const { addPoll } = PollManager.methods; + const currentBlock = await getBlockNumber(); + const endTime = currentBlock + (oneDayinBlocks * 90); + addPoll( + endTime, + singleChoiceDef(description, ['YES', 'NO']) + ) + .send() + .then(res => { + console.log('sucess:', res); + }) + .catch(res => { + console.log('fail:', res); + }) } })(StyledForm)