Skip to content

Commit f8674b7

Browse files
committed
fix(search): search on enter and only add to history on blur
1 parent 433bb39 commit f8674b7

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/components/views/search/SearchNavbar.vue

+22-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ import IconMovie from '~/components/icons/IconMovie.vue';
2828
import IconRestore from '~/components/icons/IconRestore.vue';
2929
import IconScreen from '~/components/icons/IconScreen.vue';
3030
import IconYoutube from '~/components/icons/IconYoutube.vue';
31+
import { Logger } from '~/services/logger.service';
3132
import { ResolveExternalLinks } from '~/settings/external.links';
32-
import { SupportedSearchType, useSearchStoreRefs } from '~/stores/data/search.store';
33+
import {
34+
SupportedSearchType,
35+
useSearchStore,
36+
useSearchStoreRefs,
37+
} from '~/stores/data/search.store';
3338
import { debounce } from '~/utils/debounce.utils';
3439
import { useI18n } from '~/utils/i18n.utils';
3540
import { useDebouncedSearch } from '~/utils/store.utils';
@@ -49,6 +54,7 @@ const { reverse } = defineProps({
4954
},
5055
});
5156
57+
const { fetchSearchResults, addToHistory } = useSearchStore();
5258
const { search, types, query, pageSize, loading, history } = useSearchStoreRefs();
5359
5460
const typeOptions = ref<TraktSearchType[]>(SupportedSearchType);
@@ -216,11 +222,23 @@ const route = useRoute();
216222
217223
const placement = computed(() => (reverse ? 'top' : 'bottom'));
218224
225+
const saveHistory = async () => {
226+
try {
227+
await addToHistory();
228+
} catch (e) {
229+
Logger.error('Failed to save search history', e);
230+
}
231+
};
232+
233+
const searchNow = () => {
234+
fetchSearchResults(undefined, debouncedSearch.value);
235+
};
236+
219237
onActivated(() => {
220-
if (!search.value) inputRef.value?.focus();
221238
const _search = route?.query?.search;
222239
if (typeof _search !== 'string') return;
223240
search.value = _search.trim();
241+
saveHistory();
224242
});
225243
</script>
226244

@@ -275,6 +293,8 @@ onActivated(() => {
275293
clearable
276294
:options="historyOptions"
277295
:to="parentElement"
296+
@blur="saveHistory"
297+
@keydown.enter="searchNow"
278298
>
279299
<template #prefix>
280300
<NIcon :component="IconLoop" />

src/stores/data/search.store.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const useSearchStore = defineStore(SearchStoreConstants.Store, () => {
7373
const history = ref<Set<string>>(new Set());
7474

7575
const saveHistory = debounce(() => storage.local.set(SearchStoreConstants.LocalHistory, [...history.value]), 1000);
76-
const saveSearch = debounce(() => storage.local.set(SearchStoreConstants.LocalLast, { value: search.value, date: Date.now() }), 1000);
76+
const saveSearch = debounce(() => storage.local.set(SearchStoreConstants.LocalLast, { value: search.value, date: Date.now() }), 200);
7777

7878
const addToHistory = debounce((value: string = search.value) => {
7979
if (!value || value.trim().length < minSearchLength) return;
@@ -136,12 +136,12 @@ export const useSearchStore = defineStore(SearchStoreConstants.Store, () => {
136136
const loadingPlaceholder = useLoadingPlaceholder<SearchResult>(pageSize);
137137

138138
const { isAuthenticated } = useAuthSettingsStoreRefs();
139-
const fetchSearchResults = async ({ page, limit = pageSize.value }: { page?: number; limit?: number } = {}) => {
139+
const fetchSearchResults = async ({ page, limit = pageSize.value }: { page?: number; limit?: number } = {}, _search = search.value?.trim()) => {
140140
if (!isAuthenticated.value) {
141141
Logger.error('Cannot fetch search results, user is not authenticated');
142142
return;
143143
}
144-
if (!search.value?.trim()) {
144+
if (!_search) {
145145
pagination.value = {};
146146
searchResults.value = [];
147147
return;
@@ -152,14 +152,14 @@ export const useSearchStore = defineStore(SearchStoreConstants.Store, () => {
152152
}
153153
if (firstLoad.value) firstLoad.value = false;
154154

155-
Logger.debug('Fetching search results', { types: types.value, query: query.value, search: search.value });
155+
Logger.debug('Fetching search results', { types: types.value, query: query.value, search: _search });
156156

157157
loading.value = true;
158158
const { clearLoading } = debounceLoading(searchResults, loadingPlaceholder, { clear: !page });
159159
const request: TraktSearch = {
160160
type: types.value,
161161
escape: !query.value,
162-
query: search.value,
162+
query: _search,
163163
pagination: { page, limit },
164164
};
165165
try {
@@ -191,7 +191,7 @@ export const useSearchStore = defineStore(SearchStoreConstants.Store, () => {
191191
watch(search, async () => {
192192
if (!search.value || search.value.length < minSearchLength) return;
193193
await fetchSearchResults();
194-
addToHistory().catch(e => Logger.error('Failed to save search history', e));
194+
saveSearch().catch(e => Logger.error('Failed to save search', e));
195195
});
196196

197197
watch([pageSize, types], async () => {

0 commit comments

Comments
 (0)