summaryrefslogtreecommitdiff
path: root/demo/components/like-button.js
blob: 1c9649a8f8f6f08af730bd8181126b6dc13a301c (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) {
      return objects.filter(o=>
                      'like' in o &&
                      'timestamp' in o &&
                      o.like == this.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>`
}