pinafore/src/routes/_actions/search.js

25 wiersze
852 B
JavaScript
Czysty Zwykły widok Historia

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