aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/UploadVoter.vue
blob: 563bc79fb8b4e578ed5a0cf2cb207bba8997515b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<template>
  <div>
    <div class="flex row items-center group" v-if="$store.getters['user/loggedIn']">
      <h5 style="margin: 0 1rem">
        <i class="fa"
           :class="{'fa-caret-up text-positive': upload.voting.sum > 0, 'fa-caret-down text-negative': upload.voting.sum < 0, 'fa-sort text-dark': upload.voting.sum === 0}">
        </i>
        {{ upload.voting.sum }}
      </h5>
      <span v-if="upload.author.username !== $store.state.user.decodedToken.username && !myVote">
        <q-btn color="negative" round small icon="fa-thumbs-down" @click="vote(-1)"></q-btn>
        <q-btn color="positive" round small icon="fa-thumbs-up" @click="vote(1)"></q-btn>
      </span>
      <span v-if="myVote">You voted <i :class="`fas fa-thumbs-${myVote.impact === 1 ? 'up text-positive' : 'down text-negative'}`"></i></span>
    </div>
    <div class="group" v-else>
      <i class="fa"
         :class="{'fa-caret-up text-positive': upload.voting.sum > 0, 'fa-caret-down text-negative': upload.voting.sum < 0, 'fa-sort text-dark': upload.voting.sum === 0}">
      </i>
      {{ upload.voting.sum }} votes
      <em>Please log in to vote</em>
    </div>
  </div>
</template>

<script>
  import {
    QBtn,
  } from 'quasar'

  export default {
    props: {
      upload: Object,
    },
    components: {
      QBtn,
    },
    data () {
      return {}
    },
    computed: {
      myVote () {
        if (!this.upload || !this.upload.voting || !Array.isArray(this.upload.voting.votes) || this.upload.voting.votes.length === 0) {
          return undefined
        }
        return this.upload.voting.votes[0]
      },
    },
    methods: {
      vote (impact) {
        let that = this
        this.$http.post(`/uploads/${this.upload._id}/vote`, {impact: impact}).then(response => that.$emit('voted'))
      },
    },
  }
</script>

<style lang="styl" type="text/stylus" scoped>
</style>