yandere_fe/src/components/user_autosuggest/user_autosuggest.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

import reject from 'lodash/reject'
import map from 'lodash/map'
import BlockCard from '../block_card/block_card.vue'
2019-04-02 03:12:31 -07:00
import userSearchApi from '../../services/new_api/user_search.js'
const debounceMilliseconds = 500
2019-04-02 03:12:31 -07:00
export default {
components: {
BlockCard
2019-04-02 03:12:31 -07:00
},
data () {
return {
query: '',
2019-04-02 03:12:31 -07:00
results: [],
2019-04-02 10:18:36 -07:00
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)
2019-04-02 03:12:31 -07:00
}
},
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')
2019-04-02 10:18:36 -07:00
})
}
}, debounceMilliseconds)
2019-04-02 10:18:36 -07:00
},
onInputClick () {
this.resultsVisible = true
},
onClickOutside () {
this.resultsVisible = false
2019-04-02 03:12:31 -07:00
}
}
}