Skip to content

Commit b32b1bf

Browse files
committed
voting now updates on front end and on backend. Fully synched.
1 parent b27b29a commit b32b1bf

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed

app/scripts/controllers/main.js

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,19 @@ angular.module('codeSearchApp')
4343
})
4444
.success(function(data){
4545
var parsedData = angular.fromJson(data);
46-
console.log(parsedData);
46+
console.log(parsedData.snippits);
47+
parsedData.snippits.sort(function(a, b) {
48+
if (a.snippitScore < b.snippitScore) {
49+
return 1;
50+
} else if (a.snippitScore > b.snippitScore) {
51+
return -1;
52+
} else {
53+
return 0;
54+
}
55+
});
4756
$timeout(function(){
4857
$scope.codeSnippits = parsedData.snippits;
58+
console.log($scope.codeSnippits);
4959
var pages = Math.ceil( $scope.codeSnippits.length / $scope.page.resultsPerPage );
5060
$scope.pageArray = [];
5161
for ( var i = 0; i < pages; i++ ) {
@@ -56,58 +66,52 @@ angular.module('codeSearchApp')
5666
};
5767

5868
$scope.snippitVote = function(votePreference, snippitObj) {
69+
70+
var indexPreSort = $scope.codeSnippits.indexOf(snippitObj);
71+
var scopeSnippit = $scope.codeSnippits[indexPreSort];
72+
5973
if (!$rootScope.currentUser) {
60-
$scope.notSignedIn = true;
74+
scopeSnippit.notSignedIn = true;
6175
$timeout(function(){
62-
$scope.notSignedIn = false;
76+
scopeSnippit.notSignedIn = false;
6377
}, 3000);
6478
} else {
6579

6680
var githubId = $rootScope.currentUser.github_id;
67-
var indexPreSort = $scope.codeSnippits.indexOf(snippitObj);
68-
var originalSnippitArray = $scope.codeSnippits[indexPreSort];
69-
var snippitScore = originalSnippitArray.snippitScore;
70-
var snippitVoterInfo = originalSnippitArray.snippitVoters;
71-
72-
if (typeof snippitVoterInfo[githubId] === 'undefined') {
73-
console.log('no vote: ', snippitVoterInfo[githubId]);
74-
snippitVoterInfo[githubId] = 0;
75-
}
76-
77-
originalSnippitArray.snippitScore += votePreference;
78-
snippitVoterInfo[githubId] += votePreference;
79-
80-
console.log(snippitVoterInfo[githubId]);
81-
82-
83-
// if (snippitVotersObj[githubId] === 1) {
84-
85-
// }
81+
var snippitScore = scopeSnippit.snippitScore;
82+
var snippitVoterInfo = scopeSnippit.snippitVoters;
83+
84+
if (snippitVoterInfo[githubId] === votePreference) {
85+
scopeSnippit.duplicateVote = true;
86+
$timeout(function(){
87+
scopeSnippit.duplicateVote = false;
88+
}, 3000);
89+
} else {
90+
91+
if (typeof snippitVoterInfo[githubId] === 'undefined') {
92+
console.log('no vote: ', snippitVoterInfo[githubId]);
93+
snippitVoterInfo[githubId] = 0;
94+
}
8695

87-
// if (votePreference = 1) {
88-
// $scope.codeSnippits[index].snippitScore++;
89-
// $scope.codeSnippits[index[upVote = true;
90-
// $scope.downVote = false;
91-
// } else {
92-
// $scope.codeSnippits[index].snippitScore--;
93-
// $scope.upvote = false;
94-
// $scope.downVote = true;
95-
// }
96+
scopeSnippit.snippitScore += votePreference;
97+
snippitVoterInfo[githubId] += votePreference;
9698

99+
console.log(snippitVoterInfo[githubId]);
97100

98-
// var snippit = snippitObj.snippit;
99-
// var filePath = snippitObj.filePath;
101+
var snippit = snippitObj.snippit;
102+
var filePath = snippitObj.filePath;
100103

101-
// var snippitData = {
102-
// snippit: snippit,
103-
// votePreference: votePreference,
104-
// filePath: filePath
105-
// };
106-
// console.log(snippitData);
107-
// $http.post('/api/snippitVote', snippitData)
108-
// .success(function(data){
109-
// console.log(data);
110-
// });
104+
var snippitData = {
105+
snippit: snippit,
106+
votePreference: votePreference,
107+
filePath: filePath
108+
};
109+
console.log(snippitData);
110+
$http.post('/api/snippitVote', snippitData)
111+
.success(function(data){
112+
console.log(data);
113+
});
114+
}
111115
}
112116
};
113117

app/views/partials/main.html

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,16 @@ <h3>Results:</h3>
8989
</div>
9090
<br>
9191
<div class="snippit" ng-repeat="snippitObj in codeSnippits |
92-
orderBy : '-snippitScore' |
9392
startPageFrom : page.currPage*page.resultsPerPage |
9493
limitTo : page.resultsPerPage track by $index">
9594
<div class="row">
9695
<pre class="col-md-10" ng-click="openModal('lg', snippitObj)" >{{snippitObj.snippit}}</pre>
9796
<div class="col-md-2">
98-
<!-- <i class="glyphicon glyphicon-thumbs-up" ng-click="snippitVote(1, snippitObj)" ng-hide="snippitObj.snippitVoters[currentUser.github_id] === 1"></i>
99-
{{snippitObj.snippitScore}}
100-
<i class="glyphicon glyphicon-thumbs-down" ng-click="snippitVote(-1, snippitObj)" ng-hide="snippitObj.snippitVoters[currentUser.github_id] === -1"></i>
101-
--> <i class="glyphicon glyphicon-thumbs-up" ng-click="snippitVote(1, snippitObj)"></i>
97+
<i class="glyphicon glyphicon-thumbs-up" ng-click="snippitVote(1, snippitObj)"></i>
10298
{{snippitObj.snippitScore}}
10399
<i class="glyphicon glyphicon-thumbs-down" ng-click="snippitVote(-1, snippitObj)"></i>
104-
<p ng-show="notSignedIn">Please sign in to vote</p>
100+
<p ng-show="snippitObj.notSignedIn">Please sign in to vote</p>
101+
<p ng-show="snippitObj.duplicateVote">Already voted on this</p>
105102
</div>
106103
</div>
107104
</div>

0 commit comments

Comments
 (0)