aboutsummaryrefslogtreecommitdiff
path: root/src/components/user_autosuggest/user_autosuggest.js
blob: eff6ef75638d2b866f1ab27d27f841d3dc8bc7b3 (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
import BasicUserCard from '../basic_user_card/basic_user_card.vue'
import userSearchApi from '../../services/new_api/user_search.js'

const debounceMilliseconds = 500

export default {
  components: {
    BasicUserCard
  },
  data () {
    return {
      query: '',
      results: [],
      timeout: null
    }
  },
  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((data) => { this.results = data })
        }
      }, debounceMilliseconds)
    }
  }
}