@@ -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
0 commit comments