Browse Source

added likes and dislikes

Johand 9 years ago
parent
commit
dc5033b3c3
2 changed files with 50 additions and 8 deletions
  1. 46 4
      app/app.js
  2. 4 4
      app/templates/room.html

+ 46 - 4
app/app.js

@@ -215,11 +215,9 @@ if (Meteor.isClient) {
 
     Template.room.events({
         "click #like": function(e) {
-            console.log("Liked");
             Meteor.call("likeSong", Session.get("currentSong").mid);
         },
         "click #dislike": function(e) {
-            console.log("Disliked");
             Meteor.call("dislikeSong", Session.get("currentSong").mid);
         },
         "click #report-prev": function(e) {
@@ -509,6 +507,52 @@ if (Meteor.isClient) {
     });
 
     Template.room.helpers({
+        likes: function() {
+            var playlist = Playlists.findOne({type: Session.get("type")});
+            var likes = 0;
+            playlist.songs.forEach(function(song) {
+                if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
+                    likes = song.likes;
+                    return;
+                }
+            });
+            return likes;
+        },
+        dislikes: function() {
+            var playlist = Playlists.findOne({type: Session.get("type")});
+            var dislikes = 0;
+            playlist.songs.forEach(function(song) {
+                if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
+                    dislikes = song.dislikes;
+                    return;
+                }
+            });
+            return dislikes;
+        },
+        liked: function() {
+            if (Meteor.userId()) {
+                var currentSong = Session.get("currentSong");
+                if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
+                    return "active";
+                } else {
+                    return "";
+                }
+            } else {
+                "";
+            }
+        },
+        disliked: function() {
+            if (Meteor.userId()) {
+                var currentSong = Session.get("currentSong");
+                if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
+                    return "active";
+                } else {
+                    return "";
+                }
+            } else {
+                "";
+            }
+        },
         type: function() {
             var parts = location.href.split('/');
             var id = parts.pop().toLowerCase();
@@ -1356,7 +1400,6 @@ if (Meteor.isServer) {
 
     Meteor.methods({
         likeSong: function(mid) {
-            console.log("Liked! ", mid);
             if (Meteor.userId()) {
                 var user = Meteor.user();
                 if (user.profile.liked.indexOf(mid) === -1) {
@@ -1377,7 +1420,6 @@ if (Meteor.isServer) {
             }
         },
         dislikeSong: function(mid) {
-            console.log("Disliked! ", mid);
             if (Meteor.userId()) {
                 var user = Meteor.user();
                 if (user.profile.disliked.indexOf(mid) === -1) {

+ 4 - 4
app/templates/room.html

@@ -53,11 +53,11 @@
                         </div>
                         <div class="col-md-3">
                         {{#if currentUser}}
-                            <button title="Smile to this song." type="button" id="like" class="btn btn-success btn-lg"><i class="fa fa-smile-o"> 79</i></button>
-                            <button title="I dislike this song." type="button" id="dislike" class="btn btn-danger btn-lg"><i class="fa fa-meh-o"> 14</i></button>
+                            <button title="Smile to this song." type="button" id="like" class="btn btn-success btn-lg {{liked}}"><i class="fa fa-smile-o"> {{likes}}</i></button>
+                            <button title="I dislike this song." type="button" id="dislike" class="btn btn-danger btn-lg {{disliked}}"><i class="fa fa-meh-o"> {{dislikes}}</i></button>
                         {{else}}
-                            <button title="You need to be logged to smile this song." type="button" class="btn btn-success btn-lg" disabled><i class="fa fa-smile-o"> 79</i></button> 
-                            <button title="You need to be logged to dislike this song." type="button" class="btn btn-danger btn-lg" disabled><i class="fa fa-meh-o"> 14</i></button>
+                            <button title="You need to be logged to smile this song." type="button" class="btn btn-success btn-lg" disabled><i class="fa fa-smile-o"> {{likes}}</i></button> 
+                            <button title="You need to be logged to dislike this song." type="button" class="btn btn-danger btn-lg" disabled><i class="fa fa-meh-o"> {{dislikes}}</i></button>
                         {{/if}}
                         <button title="Report this song!" type="button" id="report-modal" class="btn btn-warning btn-lg report-button" data-toggle="modal" data-target="#reportModal"><i class="fa fa-flag"></i></button>
                         </div>