GravityProject / gravity

Open source social network built with Meteor
MIT License
131 stars 62 forks source link

Upvote/Downvote #122

Closed zaverichintan closed 8 years ago

zaverichintan commented 8 years ago

added like dislike functionality. User can click as many time user wishes.

TBD - 1 Like per user

pmuens commented 8 years ago

Cool! A check if someone has already up / downvoted would be great!

mattvella07 commented 8 years ago

Yea I agree, 1 like per user. And a way to remove their up/down vote would be good too. Maybe by clicking the up/down vote icon a second time will remove their up/down vote, like a toggle.

zaverichintan commented 8 years ago

Ok sure.

zaverichintan commented 8 years ago

I am trying something like this -

 if(Posts.find({ already_voted:  Meteor.userId() }) ){
 Posts.update( { _id: _id }, { $pull: { already_voted: Meteor.userId() } });
   console.log(" found");

 }else {
    Posts.update( { _id: _id }, { $push: { already_voted: Meteor.userId() } });
   console.log("not found");

 }

The objective is to see if the user id exists in already_voted field in posts collections . But it does not give proper output. For the first time it siting give not found as output but it gives found. I think this is happening as schema is not defined so the field already_voted is not present . Any help would be appreciated.

mattvella07 commented 8 years ago

Is already_voted an array containing userIDs of all users who voted on a particular post?

It's hard to be sure without seeing all the code, but I think I see the issue. When you are doing this part: Posts.find({ already_voted: Meteor.userId() })

That is looking for any post that has already_voted with a value of the current userId, so you need to include the id of the post in your find as well. Something like this: Posts.find({ _id: post_id, already_voted: Meteor.userId() })

But it doesn't look like the post id is stored in the id field of the DIV, so you would have to add that first that way you can get the post_id of the post that the user is trying to up/down vote. Hopefully this all makes sense.

zaverichintan commented 8 years ago

I agree to your approach. Tried doing that. The thing is that when song for the first time, field does not exist and it should give not found, instead it gives found.

mattvella07 commented 8 years ago

Hmmm...why don't you try checking if the count > 0 instead: if(Posts.find({ already_voted: Meteor.userId() }).count() > 0 )

zaverichintan commented 8 years ago

I will surely give it a try. Thank you.

zaverichintan commented 8 years ago

The color of thumb does not change. Is there some issue with the helper part ?

mattvella07 commented 8 years ago

I would say if it is liked add a class to the button and you can have different styles in the LESS file based on button.className

mattvella07 commented 8 years ago

I don't think self._id is giving you the id of the post like you are expecting, but I can't be sure. So try to console.log() it right before the Meteor.call to verify that it is giving you the correct post id.

zaverichintan commented 8 years ago

self._id is giving the id of post, the issue is that the function gets called two times. something like this -

hyvbhTES3dWcKkBL3
H6HLgcGarGZFisH6M
XwXr2xRDfSLETxnRo
hyvbhTES3dWcKkBL3
H6HLgcGarGZFisH6M
XwXr2xRDfSLETxnRo
false
true
false
false
true
false

something like this http://stackoverflow.com/questions/26913198/meteor-helper-called-multiple-times-by-single-template-variable

mattvella07 commented 8 years ago

It looks like those are all different IDs. Are you referring to the isLiked function that gets called twice? Because that shouldn't matter it should return the same value both times. Or are you referring to the click event for like that gets called twice? Because that would be an issue since it would like it then unlike it.

zaverichintan commented 8 years ago

The 1st and 4th are same as there are 3 posts. Isliked function in called twice ss meteor call makes 2 times calls.

mattvella07 commented 8 years ago

I see. If you want you can commit what you have to your fork of gravity and I can download it and try to see if I can figure out a way to fix it.

zaverichintan commented 8 years ago

Done with the updated commit. Toggle of likes is working. The color when class liked is added changes to blue is done. The helper function perform same function (meteor call) two times.

pmuens commented 8 years ago

@zaverichintan Alright. Thank you. This means that it's ready to be reviewed?

mattvella07 commented 8 years ago

The toggling if likes is working, but I'm not seeing the color change after a post has been liked. And the name and like button are lined up, not a huge deal but would be nice to have it all lined up.

zaverichintan commented 8 years ago

@The toggling part works but to implement color change If already liked is not working due to 2 calls to the function. I am not sure why that thing happens may be due server and client side rendering.
Kindly have a look @mattvella07. As your approach in jobBoard.js to subscribe in helper might solve this issue. @pmuens One part is left (while leaving the posts, the meteor call occurs two times)

mattvella07 commented 8 years ago

@zaverichintan Ok, I think I figured it out. Most of what you have is correct, but the issue is that Meteor.call is asynchronous so the view is rendered before the callback function executes which is why the 'liked' class is never assigned. If you remove the Meteor.call and just do the Posts.find() in the isLiked helper function it will work.

@pmuens If you want to merge this PR I can make the adjustments or @zaverichintan you can make them if you like.

zaverichintan commented 8 years ago

I will try it out. The async behavior of meteor call us the reason behind multiple calls that's the reason in my opinion too. I Will try to have direct access to collection.

mattvella07 commented 8 years ago

Here's what I had:

isLiked: function() {
    if(Posts.find( { _id: this._id , already_voted: { "$in" : [Meteor.userId()]} }).count() === 1) {
      return 'liked';
    } 

    return '';
  }
zaverichintan commented 8 years ago

Done. Thanks a lot @mattvella07 . Meteor call occurs two times, so subscribing the data (posts) and then rendering directly in helper. @pmuens Finally done, can be merged .

pmuens commented 8 years ago

Good job! Thank you!