2021-07-05 03:19:04 +00:00
|
|
|
import { store } from '../_store/store.js'
|
|
|
|
import { toast } from '../_components/toast/toast.js'
|
|
|
|
import { search } from '../_api/search.js'
|
|
|
|
import { formatIntl } from '../_utils/formatIntl.js'
|
2018-02-11 18:41:01 +00:00
|
|
|
|
2018-02-11 22:11:03 +00:00
|
|
|
export async function doSearch () {
|
2019-08-03 20:49:37 +00:00
|
|
|
const { currentInstance, accessToken, queryInSearch } = store.get()
|
2018-08-30 04:42:57 +00:00
|
|
|
store.set({ searchLoading: true })
|
2018-02-11 18:41:01 +00:00
|
|
|
try {
|
2019-08-03 20:49:37 +00:00
|
|
|
const results = await search(currentInstance, accessToken, queryInSearch)
|
|
|
|
const { queryInSearch: newQueryInSearch } = store.get() // avoid race conditions
|
2018-04-19 16:37:05 +00:00
|
|
|
if (newQueryInSearch === queryInSearch) {
|
2018-02-11 18:41:01 +00:00
|
|
|
store.set({
|
|
|
|
searchResultsForQuery: queryInSearch,
|
|
|
|
searchResults: results
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2020-11-29 22:13:27 +00:00
|
|
|
/* no await */ toast.say(formatIntl('intl.searchError', { error: (e.message || '') }))
|
2018-02-11 18:41:01 +00:00
|
|
|
console.error(e)
|
|
|
|
} finally {
|
2018-08-30 04:42:57 +00:00
|
|
|
store.set({ searchLoading: false })
|
2018-02-11 18:41:01 +00:00
|
|
|
}
|
2018-02-11 22:11:03 +00:00
|
|
|
}
|