Skip to content

Commit

Permalink
Voting added
Browse files Browse the repository at this point in the history
  • Loading branch information
imtoobose committed Sep 9, 2016
1 parent 13d9fbb commit 4f81f27
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 113 deletions.
35 changes: 18 additions & 17 deletions app/routes/addpolls.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function getpolldata(userid, req, res, next){

polls.find(searchfor, function(err, pollarr){
if(err) throw err;
console.log(pollarr);
res.locals.result = pollarr;
next();
});
Expand Down Expand Up @@ -69,6 +70,23 @@ router.get('/polls/getone/:pollid',
);

//=====POST REQUESTS====================================================

router.post('/vote/:pollid', function(req, res, next){
if(req.body.vote && req.params.pollid){
polls.findOne({_id: req.params.pollid}, function(err, found){
if(err) res.redirect('/');
found.updateVotes(+req.body.vote);
found.save(function(err){
if(err) throw err;
res.redirect('/single?pollid='+req.params.pollid);
});
});
}
else{
res.end(JSON.stringify({"error": "Invalid query"}));;
}
});

router.post('/polls',
function(req, res, next){
if(req.user){
Expand Down Expand Up @@ -102,23 +120,6 @@ router.post('/polls',
}
break;

//====VOTE ON A POLL================================================
case "vote":
if(req.body.pollid && req.body.option){
polls.findOne({_id: req.body.pollid}, function(err, found){
if(err) throw err;
found.updateVotes(+req.body.option);
found.save(function(err){
if(err) throw err;
res.end();
});
});
}
else{
res.end(JSON.stringify({"error": "Invalid query"}));;
}
break;

//====ADD AN OPTION====================================================
case "addoption":
if(req.body.pollid && req.body.option){
Expand Down
8 changes: 4 additions & 4 deletions app/static/dev/css/single.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
& .allcharts {
display:block;
margin-top:20px;

padding: 0 20px 0 20px;

& .charts{
display: inline-block;
float: right;
float: left;
position:relative;
width: 50%;

& canvas{
//box-shadow: 0px 2px 3px fade-out(#000, 0.8);
padding-bottom: 15px;
padding-top: 15px;
}
Expand All @@ -23,7 +23,7 @@

.options{
display: block;
float: right;
float: left;
padding-bottom: 20px;
padding-top: 50px;
width: 50%;
Expand Down
2 changes: 1 addition & 1 deletion app/static/dev/js/createCharts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function createCharts (res, allcharts, detailed){
a.classList.add('charts');
a.setAttribute('href', '/single?pollid='+id);
a.appendChild(canvas);
allcharts.appendChild(a);
allcharts.insertBefore(a, allcharts.firstChild);
var chart = new Chart(canvas, config);
}

Expand Down
7 changes: 4 additions & 3 deletions app/static/dist/css/single.css
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,11 @@ body {
display: block; }
.single .allcharts {
display: block;
margin-top: 20px; }
margin-top: 20px;
padding: 0 20px 0 20px; }
.single .allcharts .charts {
display: inline-block;
float: right;
float: left;
position: relative;
width: 50%; }
.single .allcharts .charts canvas {
Expand All @@ -239,7 +240,7 @@ body {

.options {
display: block;
float: right;
float: left;
padding-bottom: 20px;
padding-top: 50px;
width: 50%; }
Expand Down
85 changes: 0 additions & 85 deletions app/static/dist/js/createCanvas.js

This file was deleted.

2 changes: 1 addition & 1 deletion app/static/dist/js/createCharts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function createCharts(res, allcharts, detailed) {
a.classList.add('charts');
a.setAttribute('href', '/single?pollid=' + id);
a.appendChild(canvas);
allcharts.appendChild(a);
allcharts.insertBefore(a, allcharts.firstChild);
var chart = new Chart(canvas, config);
}

Expand Down
8 changes: 6 additions & 2 deletions app/views/single.pug
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ html

div.single
div.allcharts(id=pollid)
form.options(id="votes")
form.options(id="votes" method="post" action="vote/"+pollid)
ul.optionlist(id="optionlist")
input.submit(type="submit" value="vote")

script(src="https://cdnjs.cloudflare.com/ajax/libs/qwest/4.4.5/qwest.min.js")
script(src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.min.js")
Expand All @@ -28,17 +29,20 @@ html
_in.setAttribute('type', 'radio'),
_in.setAttribute('name', 'vote'),
_in.setAttribute('id', id),
_in.setAttribute('value', i),
label.setAttribute('for', id),
label.classList.add('votelabel'),
label.innerHTML = arr[i].optionName;

if(i===0) _in.setAttribute('checked', true);
li.appendChild(_in),
li.appendChild(label);

document.getElementById('optionlist').appendChild(li);
}
}



var a=document.getElementsByClassName('allcharts')[0],
i=a.getAttribute('id');
qwest.get('/polls/getone/'+i)
Expand Down

0 comments on commit 4f81f27

Please sign in to comment.