summaryrefslogtreecommitdiff
path: root/demo/components/chat.js
blob: 5095dcdd242041e67e4da1ba3124a0875ba69fa6 (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
60
61
62
63
64
65
import { Name } from './name.js'
import Comments from './comment.js'

export default {

  components: { Name, Comments },

  data: ()=> ({
    message: '',
    channel: 'demo'
  }),

  methods: {
    messageObjects(objects) {
      return objects.filter(o=> 
                      'message' in o &&
                      'timestamp' in o &&
                      typeof o.message == 'string' &&
                      typeof o.timestamp == 'number')
                    .sortBy('timestamp')
    },

    sendMessage() {
      if (!this.message) return
      this.$graffitiUpdate({
        message: this.message,
        timestamp: Date.now(),
        _tags: [this.channel]
      })
      this.message = ''
    }
  },

  template: `
    <p>
      Chat Channel: <input v-model="channel"/>
    </p>

    <graffiti-objects :tags="[channel]" v-slot="{objects}">
      <ul v-for="object in messageObjects(objects)">
        <li>
          <em><Name :of="object._by"/></em>:
          {{ object.message }}

          <Comments :messageID="object._id" />

          <template v-if="object._by==$graffitiMyID">
            <button @click="object.message+='!!'">
              ‼️
            </button>

            <button @click="delete object._key">
              ❌
            </button>
          </template>
        </li>
      </ul>
    </graffiti-objects>

    <form @submit.prevent="sendMessage">
      <input v-model="message">
      <input type="submit" value="Submit"/>
    </form>`
}