pinafore/routes/_actions/search.js

24 lines
764 B
JavaScript
Raw Normal View History

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 () {
let { currentInstance, accessToken, queryInSearch } = store.get()
store.set({ searchLoading: true })
2018-02-11 18:41:01 +00:00
try {
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 {
store.set({ searchLoading: false })
2018-02-11 18:41:01 +00:00
}
2018-02-11 22:11:03 +00:00
}