aboutsummaryrefslogtreecommitdiff
path: root/src/components/user_autosuggest/user_autosuggest.js
blob: 75798b7e659ad48fafb80b16839e8883e58d6557 (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
import reject from 'lodash/reject'
import map from 'lodash/map'
import BlockCard from '../block_card/block_card.vue'
import userSearchApi from '../../services/new_api/user_search.js'

const debounceMilliseconds = 500

export default {
  components: {
    BlockCard
  },
  data () {
    return {
      query: '',
      results: [],
      timeout: null,
      resultsVisible: false
    }
  },
  computed: {
    filtered () {
      return reject(this.results, (userId) => {
        const user = this.$store.getters.findUser(userId)
        return !user || user.statusnet_blocking || user.id === this.$store.state.users.currentUser.id
      })
    }
  },
  watch: {
    query (val) {
      this.fetchResults(val)
    }
  },
  methods: {
    fetchResults (query) {
      clearTimeout(this.timeout)
      this.timeout = setTimeout(() => {
        this.results = []
        if (query) {
          userSearchApi.search({query, store: this.$store})
            .then((users) => {
              this.$store.dispatch('addNewUsers', users)
              this.results = map(users, 'id')
            })
        }
      }, debounceMilliseconds)
    },
    onInputClick () {
      this.resultsVisible = true
    },
    onClickOutside () {
      this.resultsVisible = false
    }
  }
}