How to check the user who gave the vote

92 views Asked by At

I want to make a vote system like stackoverflow, vote is working well, but I am still constrained by adding a class on if user gives vote.

I have json data like this:

[{
    "id": 157,
    "questionvote": 
     [
        {
            "user": 6,
            "question": 157,
            "value": true
        },
        {
            "user": 3,
            "question": 157,
            "value": false
        }
      ]
 }]

I use vue, and the template for the vote is like this-

  <span type="submit" @click='positif(upvote, q)' v-bind:class="{ on: classVote }" class="vote"> </span>

and I want to check if the user who has ever given vote to the question then class vote is added on

classVote: function () {
   return {
     /// how to check it
   }
}

How should I write it to be able to conditionally determine the class?

1

There are 1 answers

0
takeiteasy On BEST ANSWER
<span type="submit" @click='positif(upvote, q)' v-bind:class="classUpVote(q)" class="vote"> </span>

In methods:

methods:{
 classUpVote: function (q) {
  var qvote = q.questionvote
  var activeuser = this.usernow /// get current user
  var hasMatch = false

  for (var i = 0; i < qvote.length; ++i) {
   var quest = qvote[i];

   if(quest.user == activeuser && quest.value == true){
     hasMatch = true;
     break;
   }
  }

  return {
    on: hasMatch
  }
}
}