Commit 7a55c380 authored by Miguel Braidot's avatar Miguel Braidot

El formulario de votación se llena con las opciones del SC

parent eb7dce85
......@@ -39,7 +39,7 @@
<i class="fas fa-plus" aria-hidden="true"></i>
</span>
</a>
<form action="">
<form action="smartVotar.html">
<div class="alert alert-danger" role="alert" id="errorForm">
<p>Comprobá los siguientes errores del formulario:</p>
<ul></ul>
......@@ -111,8 +111,8 @@
},
submitHandler: function (form) {
// do other things for a valid form
//form.submit();
ver_votacion('');
form.submit();
//ver_votacion('');
},
messages: {
input_address: {
......
......@@ -219,6 +219,17 @@ function setup_existing_contract(abi, addr) {
return;
}
function setup_existing_instance(abi, addr) {
if (api_version() == 0) {
var MyContract = window.web3.eth.contract(abi);
console.log(MyContract);
contract = MyContract.at(addr);
return contract;
}
window.alert('Do not know how to deal with contracts at this API version (' + window.web3.version.api + ').');
return;
}
function add_proposal(pos) {
var div = document.getElementById('newtable');
if (!div)
......@@ -386,53 +397,18 @@ async function nueva_votacion(arguments,proposals) {
if (mainaddr[netid] == undefined)
return;
//var f = document.forms['form_create_new_ballot'];
/*var arguments = [];
arguments.push(document.getElementById('_title').value);
arguments.push(document.getElementById('start_date').value);
arguments.push(document.getElementById('voteBefore').value);
arguments.push(document.getElementById('percentOfRegisteredVotersReqToBeValid').value);
arguments.push(document.getElementById('percentOfVotesCastToWin').value);
arguments.push(document.getElementById('countNonvotesAsBlanks').value);
arguments.push(document.getElementById('maxVotesPerVoter').value);
arguments.push(document.getElementById('maxVotesPerProposal').value);
var p;
var proposals = [];
for (var i = 1; p = document.getElementById('proposal' + i); i++){
proposals.push(web3.fromAscii(p.value));
}*/
for (var i = 0; i < proposals.length; i++){
proposals[i] = web3.fromAscii(proposals[i]);
}
//var newdiv = document.getElementById('newcontract');
//var localstatus = addchild(newdiv, 'p', 'Trying to access your account.');
alertarLoading('Trying to access your account.');
await access_accounts();
//localstatus.innerHTML = 'Creating new voting contract.';
alertarLoading('Creating new voting contract.');
var contract = setup_existing_contract(abiNew, mainaddr[netid]);
if (!contract)
return;
/*var rcpt;
if (api_version() == 0)
contract.newBallot.call(
arguments[0],
arguments[1],
arguments[2],
arguments[3],
arguments[4],
arguments[5],
arguments[6],
arguments[7],
proposals, {
gas: 2111000
},
contract_created
);*/
console.log(arguments,proposals);
try
{
......@@ -459,9 +435,6 @@ async function nueva_votacion(arguments,proposals) {
async function contract_created(err, rcpt) {
console.log(rcpt);
/*if (!rcpt)
return;*/
//ver_votacion( newaddr );
}
async function ver_votacion(addr) {
......@@ -481,6 +454,7 @@ async function ver_votacion(addr) {
ver_votacion2(addr, code);
});
}
async function ver_votacion2(addr, code) {
console.log('Contract code', code);
if (code == '0x') {
......@@ -491,33 +465,107 @@ async function ver_votacion2(addr, code) {
'encoding': 'hex'
});
console.log('keccak3: ', keccak3);
if (keccak3 != ballot_keccak3) {
/*if (keccak3 != ballot_keccak3) {
alertar('No hay una votación en esa dirección.');
return;
}
var contract = setup_existing_contract(abiBallot, addr);
}*/
var contract = setup_existing_instance(abiBallot, addr);
if (!contract)
return;
var detailpromises;
// make sure we have just a single verdiv
var verdiv;
while (verdiv = document.getElementById('verdiv'))
verdiv.parent.removeChild(verdiv);
verdiv = addchild(rootdiv, 'div', undefined, {
'id': 'verdiv'
});
// Get headline
var proposaldiv = addchild(verdiv, 'p', 'Proposal:')
contract.methods.proposal().call({})
.then(
function showproposal(result) {
proposaldiv.innerHTML = 'Proposal: ' + result;
},
function failproposal(err) {
proposaldiv.innerHTML = 'Failed to get proposal: ' + err
}
);
try
{
// Getting the title
contract.ballotTitle.call({ gas: 2111000 }, getBallotTitle);
// Getting the proposal list
// @TODO: get the proposal mapping's length
contract.proposalList.call(0, { gas: 2111000 }, getProposalList);
contract.proposalList.call(1, { gas: 2111000 }, getProposalList);
// Getting the ballot dates
contract.ballotVoteStarts.call({ gas: 2111000 }, getBallotVoteStarts);
contract.ballotVoteBefore.call({ gas: 2111000 }, getBallotVoteBefore);
close_alertar();
}
catch (error)
{
alertar(error);
}
}
///////////////////////////////////////////////////////////
// UTILS for ballot form creation
///////////////////////////////////////////////////////////
async function getBallotVoteStarts(err, rcpt){
if(err){
alertar(err);
}else{
$('#comienza_votacion').text(getDateFromTimestamp(rcpt));
var today = new Date();
if(rcpt > today.getTime()){
$("#form_votacion :input").prop("disabled", true);
alertar("La votación no ha comenzado aún.");
}
}
}
async function getBallotVoteBefore(err, rcpt){
if(err){
alertar(err);
}else{
$('#termina_votacion').text(getDateFromTimestamp(rcpt));
var today = new Date();
if(rcpt < today.getTime()){
$("#form_votacion :input").prop("disabled", true);
alertar("La votación ha finalizado.");
}
}
}
async function getBallotTitle(err, rcpt){
if(err){
alertar(err);
}else{
$('#titulo_votacion').text(rcpt);
}
}
async function getProposalList(err, rcpt){
if(err){
alertar(err);
}else{
var num = $('#proposals div').length;
var element = '<div class="radio"> \
<input type="radio" name="votacion" id="vote_'+num+'" required="required" aria-required="true"> \
<label for="vote_'+num+'">'+hex2ascii(rcpt)+'</label> \
</div>';
$('#proposals').append(element);
}
}
function hex2ascii(hexx) {
var hex = hexx.toString();
var str = '';
for (var i = 0; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
function pad (str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
function getDateFromTimestamp(timestamp){
var date_timestamp = new Date(parseInt(timestamp));
var date = pad(date_timestamp.getDate(),2) + '-' + pad(date_timestamp.getMonth()+1, 2) + '-' + date_timestamp.getFullYear() + ' ';
date += pad(date_timestamp.getHours(),2) + ':' + pad(date_timestamp.getMinutes(),2) + ':' + pad(date_timestamp.getSeconds(),2) + ' Hs.';
return date;
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
function access_accounts() {
if (gotEth) {
......@@ -565,4 +613,9 @@ function alertarSuccess(err){
document.getElementById('alert-wrapper').innerHTML = '<div class="alert alert-success" role="alert" id="alert"><i class="fas fa-check" aria-hidden="true"></i> <span id="alert-content"></span></div>';
document.getElementById('alert-content').innerHTML = err;
document.getElementById('alert').style.display = "block";
}
function close_alertar(){
document.getElementById('alert-content').innerHTML = '';
document.getElementById('alert').style.display = "none";
}
\ No newline at end of file
......@@ -32,12 +32,16 @@
<div id="alert-wrapper"></div>
<div id="voterow" class="row hide">
<section>
<h1 class="page-header">Título de la votación</h1>
<p class="max-container"><b>Dirección:</b> 0xe3e08934b6fa0b68972c08e0f545cee31ed039
<a class="btn-default btn btn-lg btn-address" aria-label="Cambiar dirección" href="smartVote.html">
<h1 class="page-header" id="titulo_votacion">Título de la votación</h1>
<p class="max-container"><b>Dirección:</b> <span id="contract_address">0xe3e08934b6fa0b68972c08e0f545cee31ed039</span>
<a class="btn-default btn btn-lg btn-address" aria-label="Cambiar dirección" href="index.html">
Cambiar
</a>
</p>
<p class="max-container">
<b>Comienza:</b> <span id="comienza_votacion"></span><br />
<b>Termina:</b> <span id="termina_votacion"></span>
</p>
<div class="resultados" style="display: none;">
<h2>Resultados</h2>
<div class="res_op">
......@@ -110,29 +114,13 @@
</table>
</div>
<form action="">
<form action="" id="form_votacion">
<div class="alert alert-danger" role="alert" id="errorForm">
<p>Comprobá los siguientes errores del formulario:</p>
<ul></ul>
</div>
<fieldset class="form-group votar">
<fieldset class="form-group votar" id="proposals">
<legend>Opciones para votar</legend>
<div class="radio">
<input type="radio" name="votacion" id="vote_0" required="required" aria-required="true">
<label for="vote_0">Opción 1</label>
</div>
<div class="radio">
<input type="radio" name="votacion" id="vote_1">
<label for="vote_1">Opción 2</label>
</div>
<div class="radio">
<input type="radio" name="votacion" id="vote_3">
<label for="vote_3">Opción 3</label>
</div>
<div class="radio">
<input type="radio" name="votacion" id="vote_4">
<label for="vote_4">Opción 4</label>
</div>
</fieldset>
<div class="form-group col-sm-6 col-sm-push-6">
<button class="btn-primary btn btn-block" type="submit" id="submit">Votar</button>
......@@ -185,6 +173,13 @@
var container = $('#errorForm');
var url = new URL(window.location.href);
var input_address = url.searchParams.get("input_address");
if(typeof input_address != "undefined" && input_address != ""){
$("#contract_address").text(input_address);
ver_votacion(input_address);
}
$("form").validate({
focusInvalid: false,
errorContainer: container,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment