pinafore/src/routes/_actions/search.js

24 lines
764 B
JavaScript
Raw Normal View History

2018-02-11 19:41:01 +01:00
import { store } from '../_store/store'
import { toast } from '../_utils/toast'
import { search } from '../_api/search'
2018-02-11 23:11:03 +01:00
export async function doSearch () {
let { currentInstance, accessToken, queryInSearch } = store.get()
store.set({ searchLoading: true })
2018-02-11 19:41:01 +01:00
try {
let results = await search(currentInstance, accessToken, queryInSearch)
let { queryInSearch: newQueryInSearch } = store.get() // avoid race conditions
if (newQueryInSearch === queryInSearch) {
2018-02-11 19:41:01 +01: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 19:41:01 +01:00
}
2018-02-11 23:11:03 +01:00
}