2018-02-11 18:41:01 +00:00
|
|
|
import { store } from '../_store/store'
|
|
|
|
import { toast } from '../_utils/toast'
|
|
|
|
import { search } from '../_api/search'
|
|
|
|
|
2018-02-11 22:11:03 +00:00
|
|
|
export async function doSearch () {
|
2018-04-19 16:37:05 +00:00
|
|
|
let { 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 {
|
2018-04-19 16:37:05 +00:00
|
|
|
let results = await search(currentInstance, accessToken, queryInSearch)
|
|
|
|
let { queryInSearch: newQueryInSearch } = store.get() // avoid race conditions
|
|
|
|
if (newQueryInSearch === queryInSearch) {
|
2018-02-11 18:41:01 +00:00
|
|
|
store.set({
|
|
|
|
searchResultsForQuery: queryInSearch,
|
|
|
|
searchResults: results
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
toast.say('Error during search: ' + (e.name || '') + ' ' + (e.message || ''))
|
|
|
|
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
|
|
|
}
|