Skip to content

Commit 64839f7

Browse files
authored
Merge pull request #19 from bfirsh/vote-refactoring
Various pieces of vote refactoring
2 parents c6a5cc9 + aa05c12 commit 64839f7

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

result-app/server.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,24 @@ function getVotes(client) {
4545
if (err) {
4646
console.error("Error performing query: " + err);
4747
} else {
48-
var data = result.rows.reduce(function(obj, row) {
49-
obj[row.vote] = row.count;
50-
return obj;
51-
}, {});
52-
io.sockets.emit("scores", JSON.stringify(data));
48+
var votes = collectVotesFromResult(result);
49+
io.sockets.emit("scores", JSON.stringify(votes));
5350
}
5451

5552
setTimeout(function() {getVotes(client) }, 1000);
5653
});
5754
}
5855

56+
function collectVotesFromResult(result) {
57+
var votes = {a: 0, b: 0};
58+
59+
result.rows.forEach(function (row) {
60+
votes[row.vote] = parseInt(row.count);
61+
});
62+
63+
return votes;
64+
}
65+
5966
app.use(cookieParser());
6067
app.use(bodyParser());
6168
app.use(methodOverride('X-HTTP-Method-Override'));

result-app/views/app.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ var bg1 = document.getElementById('background-stats-1');
55
var bg2 = document.getElementById('background-stats-2');
66

77
app.controller('statsCtrl', function($scope){
8-
var animateStats = function(a,b){
9-
if(a+b>0){
10-
var percentA = a/(a+b)*100;
11-
var percentB = 100-percentA;
12-
bg1.style.width= percentA+"%";
13-
bg2.style.width = percentB+"%";
14-
}
15-
};
16-
178
$scope.aPercent = 50;
189
$scope.bPercent = 50;
1910

@@ -23,15 +14,16 @@ app.controller('statsCtrl', function($scope){
2314
var a = parseInt(data.a || 0);
2415
var b = parseInt(data.b || 0);
2516

26-
animateStats(a, b);
17+
var percentages = getPercentages(a, b);
18+
19+
bg1.style.width = percentages.a + "%";
20+
bg2.style.width = percentages.b + "%";
2721

28-
$scope.$apply(function() {
29-
if(a + b > 0){
30-
$scope.aPercent = a/(a+b) * 100;
31-
$scope.bPercent = b/(a+b) * 100;
32-
$scope.total = a + b
33-
}
34-
});
22+
$scope.$apply(function () {
23+
$scope.aPercent = percentages.a;
24+
$scope.bPercent = percentages.b;
25+
$scope.total = a + b;
26+
});
3527
});
3628
};
3729

@@ -43,3 +35,16 @@ app.controller('statsCtrl', function($scope){
4335
init();
4436
});
4537
});
38+
39+
function getPercentages(a, b) {
40+
var result = {};
41+
42+
if (a + b > 0) {
43+
result.a = Math.round(a / (a + b) * 100);
44+
result.b = 100 - result.a;
45+
} else {
46+
result.a = result.b = 50;
47+
}
48+
49+
return result;
50+
}

result-app/views/index.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
</div>
3232
</div>
3333
</div>
34-
<div id="result" ng-if="total > 100">
35-
{{total}} votes
34+
<div id="result">
35+
<span ng-if="total == 0">No votes yet</span>
36+
<span ng-if="total == 1">{{total}} vote</span>
37+
<span ng-if="total >= 2">{{total}} votes</span>
3638
</div>
3739
<script src="socket.io.js"></script>
3840
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>

result-app/views/stylesheets/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ body{
4747
vertical-align:middle;
4848
}
4949
#result{
50+
z-index: 3;
5051
position: absolute;
5152
bottom: 40px;
5253
right: 20px;

0 commit comments

Comments
 (0)