Pinafore-Web-Client-Frontend/src/routes/_actions/search.js

25 lines
840 B
JavaScript
Raw Normal View History

2018-02-11 19:41:01 +01:00
import { store } from '../_store/store'
2018-12-23 00:37:51 +01:00
import { toast } from '../_components/toast/toast'
2018-02-11 19:41:01 +01:00
import { search } from '../_api/search'
import { formatIntl } from '../_utils/formatIntl'
2018-02-11 19:41:01 +01:00
2018-02-11 23:11:03 +01:00
export async function doSearch () {
2019-08-03 22:49:37 +02:00
const { currentInstance, accessToken, queryInSearch } = store.get()
store.set({ searchLoading: true })
2018-02-11 19:41:01 +01:00
try {
2019-08-03 22:49:37 +02:00
const results = await search(currentInstance, accessToken, queryInSearch)
const { 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) {
/* no await */ toast.say(formatIntl('intl.searchError', { error: (e.message || '') }))
2018-02-11 19:41:01 +01:00
console.error(e)
} finally {
store.set({ searchLoading: false })
2018-02-11 19:41:01 +01:00
}
2018-02-11 23:11:03 +01:00
}