summaryrefslogtreecommitdiffstats
path: root/src/components/group/GroupSender.vue
blob: a815561fcb9a4e710c94e61c9ad7d5d20942c948 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<template>
  <div
      class="bg-gray-900 w-full m-2 mb-0 border-4"
      :class="{
        'border-red-900': isFocus,
        'border-blue-900': !isFocus
      }"
  >

      <h1 class="text-2xl p-2 bg-blue-900"> {{group.name}} </h1>
      <div class="overflow-auto h-64 p-2" ref="messages">
        <Message v-for="message in messages" :key="message.id" :message="message" />
      </div>

      <form @submit="send_message">
          <input
              v-model="message"
              class="w-full bg-blue-900 p-2" placeholder="Napisz cos..." type="text" ref="input"
              @focus="isFocus=true"
              @blur="isFocus=false"
              @keydown="hotkeys"
          />
      </form>
  </div>
</template>

<script>
import Message from '@/components/group/Message.vue'
import ChatService from '@/services/ChatService.js'

export default {
  data() {
    return {
      message: "",
      messages: [],
      isFocus: false
    }
  },

  props: {
    group: Object,
    socket: Object
  },

  methods: {
      close() {
        this.$emit('close')
      },

    send_message(e) {
      e.preventDefault()
      console.log(this.message)
      this.socket.emit("send_group_message", {message: this.message, group: this.group})
      this.message = ""
    },

    async get_messages() {
      let {status, data} = await ChatService.get_group_messages(this.group.id)

      console.log(data)

      if(status===200){
        this.messages = data.messages
        this.scroll_down()
      }
    },

    push_message(message) {
      if(message.receiver == this.group.id){
        this.messages.push(message)
        this.scroll_down()
      }
    },

    scroll_down() {
      this.$nextTick(function () {
        const messages = this.$refs.messages
        console.log(messages)
        messages.scrollTop = messages.scrollHeight
      })
    },

    hotkeys(e) {
        switch(e.code) {
            case 'Delete':
                e.preventDefault()
                this.$emit('close', this.group.id)
                break
            case 'Escape':
                e.preventDefault()
                this.$refs.input.blur()
                break
        }
    }
  },

  components: {
    Message
  },

  created() {
    this.$nextTick(function () {
      this.$refs.input.focus()

      this.socket.emit("join_group", {group_id: this.group.id})
      this.socket.on("receive_group_message", ({message}) => {
        this.push_message(message)
      })
      this.get_messages()
    })
  }
}
</script>