aboutsummaryrefslogtreecommitdiff
path: root/src/components/user_search/user_search.js
blob: 62dafdf134f14a9bdbb81453567d72c5f245a5e6 (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
import FollowCard from '../follow_card/follow_card.vue'
import map from 'lodash/map'

const userSearch = {
  components: {
    FollowCard
  },
  props: [
    'query'
  ],
  data () {
    return {
      username: '',
      userIds: [],
      loading: false
    }
  },
  computed: {
    users () {
      return this.userIds.map(userId => this.$store.getters.findUser(userId))
    }
  },
  mounted () {
    this.search(this.query)
  },
  watch: {
    query (newV) {
      this.search(newV)
    }
  },
  methods: {
    newQuery (query) {
      this.$router.push({ name: 'user-search', query: { query } })
      this.$refs.userSearchInput.focus()
    },
    search (query) {
      if (!query) {
        this.users = []
        return
      }
      this.loading = true
      this.$store.dispatch('searchUsers', query)
        .then((res) => {
          this.loading = false
          this.userIds = map(res, 'id')
        })
    }
  }
}

export default userSearch