pinafore/src/routes/_actions/search.js
2018-12-22 15:37:51 -08:00

24 lines
775 B
JavaScript

import { store } from '../_store/store'
import { toast } from '../_components/toast/toast'
import { search } from '../_api/search'
export async function doSearch () {
let { currentInstance, accessToken, queryInSearch } = store.get()
store.set({ searchLoading: true })
try {
let results = await search(currentInstance, accessToken, queryInSearch)
let { queryInSearch: newQueryInSearch } = store.get() // avoid race conditions
if (newQueryInSearch === queryInSearch) {
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 })
}
}