pinafore/routes/_actions/search.js

26 lines
837 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 () {
2018-02-11 19:41:01 +01:00
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
let queryInSearch = store.get('queryInSearch')
store.set({searchLoading: true})
try {
let results = await search(instanceName, accessToken, queryInSearch)
let currentQueryInSearch = store.get('queryInSearch') // avoid race conditions
if (currentQueryInSearch === 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})
}
2018-02-11 23:11:03 +01:00
}