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