summaryrefslogtreecommitdiff
path: root/demo/components/like-button.js
blob: 5c9b1a57c269e65cd6bcd3d6a6226829a70e22ce (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
export default {

  props: ['messageID'],

  methods: {
    likeObjects(objects, messageID=this.messageID) {
      return objects.filter(o=>
                      'like' in o &&
                      'timestamp' in o &&
                      o.like == messageID &&
                      typeof o.timestamp == 'number')

    },

    toggleLike(objects) {
      const myLikes = this.likeObjects(objects).mine
      if (myLikes.length) {
        myLikes.removeMine()
      } else {
        this.$graffitiUpdate({
          like: this.messageID,
          timestamp: Date.now(),
          _tags: [this.messageID]
        })
      }
    }
  },

  template: `
    <graffiti-objects :tags="[messageID]" v-slot="{objects}">
      <button @click="toggleLike(objects)" :class="likeObjects(objects).mine.length?'button-primary':''">
        👍 {{ likeObjects(objects).length }}
      </button>
    </graffiti-objects>`
}