aboutsummaryrefslogtreecommitdiff
path: root/src/components/chat_new/chat_new.js
blob: 0da681f7007d83e94ea0d651521847e6bdf38d9a (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
import { throttle } from 'lodash'
import { mapState, mapGetters } from 'vuex'
import BasicUserCard from '../basic_user_card/basic_user_card.vue'
import UserAvatar from '../user_avatar/user_avatar.vue'

const chatNew = {
  components: {
    BasicUserCard,
    UserAvatar
  },
  data () {
    return {
      suggestions: [],
      userIds: [],
      loading: false,
      query: ''
    }
  },
  async created () {
    const { chats } = await this.backendInteractor.chats()
    chats.forEach(chat => this.suggestions.push(chat.account))
  },
  computed: {
    users () {
      return this.userIds.map(userId => this.findUser(userId))
    },
    availableUsers () {
      if (this.query.length !== 0) {
        return this.users
      } else {
        return this.suggestions
      }
    },
    ...mapState({
      currentUser: state => state.users.currentUser,
      backendInteractor: state => state.api.backendInteractor
    }),
    ...mapGetters(['findUser'])
  },
  methods: {
    goBack () {
      this.$emit('cancel')
    },
    goToChat (user) {
      this.$router.push({ name: 'chat', params: { recipient_id: user.id } })
    },
    onInput () {
      this.search(this.query)
    },
    addUser (user) {
      this.selectedUserIds.push(user.id)
      this.query = ''
    },
    removeUser (userId) {
      this.selectedUserIds = this.selectedUserIds.filter(id => id !== userId)
    },
    search: throttle(function (query) {
      if (!query) {
        this.loading = false
        return
      }

      this.loading = true
      this.userIds = []
      this.$store.dispatch('search', { q: query, resolve: true, type: 'accounts' })
        .then(data => {
          this.loading = false
          this.userIds = data.accounts.map(a => a.id)
        })
    })
  }
}

export default chatNew