Show search results asynchronously
This commit is contained in:
parent
8b63ade65b
commit
42934f4299
|
@ -95,7 +95,7 @@ const Search = {
|
||||||
this.$router.push({ name: 'search', query: { query } })
|
this.$router.push({ name: 'search', query: { query } })
|
||||||
this.$refs.searchInput.focus()
|
this.$refs.searchInput.focus()
|
||||||
},
|
},
|
||||||
search (query, searchType = null) {
|
async search (query, searchType = null) {
|
||||||
if (!query) {
|
if (!query) {
|
||||||
this.loading = false
|
this.loading = false
|
||||||
return
|
return
|
||||||
|
@ -116,52 +116,74 @@ const Search = {
|
||||||
this.lastMediaFetchCount = 0
|
this.lastMediaFetchCount = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
let searchOffset
|
const searchTypes = searchType
|
||||||
if (searchType === 'statuses') {
|
? [searchType]
|
||||||
searchOffset = this.statusesOffset
|
: ['statuses', 'media', 'accounts', 'hashtags']
|
||||||
} else if (searchType === 'media') {
|
|
||||||
searchOffset = this.mediaOffset
|
let oldStasusesLength = this.statuses.length
|
||||||
|
let oldMediaLength = this.media.length
|
||||||
|
|
||||||
|
let skipMediaSearch = !this.canSearchMediaPosts
|
||||||
|
|
||||||
|
for (const searchType of searchTypes) {
|
||||||
|
if (searchType === 'media' && skipMediaSearch) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
let searchOffset
|
||||||
|
if (searchType === 'statuses') {
|
||||||
|
searchOffset = this.statusesOffset
|
||||||
|
} else if (searchType === 'media') {
|
||||||
|
searchOffset = this.mediaOffset
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await this.$store.dispatch('search', {
|
||||||
|
q: query,
|
||||||
|
resolve: true,
|
||||||
|
offset: searchOffset,
|
||||||
|
'type': searchType,
|
||||||
|
following:
|
||||||
|
'followingOnly' in this.filter && this.filter.followingOnly
|
||||||
|
})
|
||||||
|
|
||||||
|
for (const value of Object.values(data)) {
|
||||||
|
if (value.length) {
|
||||||
|
this.loading = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always append to old results. If new results are empty, this doesn't change anything
|
||||||
|
this.userIds = this.userIds.concat(map(data.accounts, 'id'))
|
||||||
|
this.statuses = uniqBy(this.statuses.concat(data.statuses), 'id')
|
||||||
|
if ('media' in data) {
|
||||||
|
this.media = uniqBy(this.media.concat(data.media), 'id')
|
||||||
|
}
|
||||||
|
this.hashtags = this.hashtags.concat(data.hashtags)
|
||||||
|
|
||||||
|
if (isNewSearch) {
|
||||||
|
this.currenResultTab = this.getActiveTab()
|
||||||
|
if (searchType === 'statuses' && data.statuses.length === 0) {
|
||||||
|
// safe to assume that there are no media posts
|
||||||
|
skipMediaSearch = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.loadedInitially = true
|
||||||
|
|
||||||
|
if (searchType === 'statuses') {
|
||||||
|
// Offset from whatever we already have
|
||||||
|
this.statusesOffset = this.statuses.length
|
||||||
|
// Because the amount of new statuses can actually be zero, compare to old length instead
|
||||||
|
this.lastStatusFetchCount = this.statuses.length - oldStasusesLength
|
||||||
|
}
|
||||||
|
if (searchType === 'media') {
|
||||||
|
this.mediaOffset = this.media.length
|
||||||
|
this.lastMediaFetchCount = this.media.length - oldMediaLength
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.$store.dispatch('search', {
|
this.lastQuery = query
|
||||||
q: query,
|
this.loading = false
|
||||||
resolve: true,
|
|
||||||
offset: searchOffset,
|
|
||||||
'type': searchType,
|
|
||||||
following:
|
|
||||||
'followingOnly' in this.filter && this.filter.followingOnly
|
|
||||||
})
|
|
||||||
.then(data => {
|
|
||||||
this.loading = false
|
|
||||||
|
|
||||||
let oldStasusesLength = this.statuses.length
|
|
||||||
let oldMediaLength = this.media.length
|
|
||||||
|
|
||||||
// Always append to old results. If new results are empty, this doesn't change anything
|
|
||||||
this.userIds = this.userIds.concat(map(data.accounts, 'id'))
|
|
||||||
this.statuses = uniqBy(this.statuses.concat(data.statuses), 'id')
|
|
||||||
if ('media' in data) {
|
|
||||||
this.media = uniqBy(this.media.concat(data.media), 'id')
|
|
||||||
}
|
|
||||||
this.hashtags = this.hashtags.concat(data.hashtags)
|
|
||||||
|
|
||||||
if (isNewSearch) {
|
|
||||||
this.currenResultTab = this.getActiveTab()
|
|
||||||
}
|
|
||||||
this.loadedInitially = true
|
|
||||||
|
|
||||||
if (!searchType || searchType === 'statuses') {
|
|
||||||
// Offset from whatever we already have
|
|
||||||
this.statusesOffset = this.statuses.length
|
|
||||||
// Because the amount of new statuses can actually be zero, compare to old lenght instead
|
|
||||||
this.lastStatusFetchCount = this.statuses.length - oldStasusesLength
|
|
||||||
}
|
|
||||||
if (!searchType || searchType === 'media') {
|
|
||||||
this.mediaOffset = this.media.length
|
|
||||||
this.lastMediaFetchCount = this.media.length - oldMediaLength
|
|
||||||
}
|
|
||||||
this.lastQuery = query
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
resultCount (tabName) {
|
resultCount (tabName) {
|
||||||
const length = this[tabName].length
|
const length = this[tabName].length
|
||||||
|
|
Loading…
Reference in New Issue