Improve search logic

This commit is contained in:
2025-10-20 21:10:26 +03:30
parent 1efac711a9
commit 4d6f2db9ee
2 changed files with 19 additions and 2 deletions

View File

@@ -36,7 +36,7 @@ function onSubmit() {
@input="emit('update:modelValue', ($event.target as HTMLInputElement).value)" @input="emit('update:modelValue', ($event.target as HTMLInputElement).value)"
/> />
</label> </label>
<button class="btn btn-neutral join-item">Search</button> <!-- <button class="btn btn-neutral join-item">Search</button> -->
</div> </div>
</form> </form>
</template> </template>

View File

@@ -1,6 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { loadMoreMovies, searchMovies } from '@/lib/api' import { loadMoreMovies, searchMovies } from '@/lib/api'
import { onMounted, ref } from 'vue' import { onMounted, ref, watch } from 'vue'
import SearchBar from '@/components/SearchBar.vue' import SearchBar from '@/components/SearchBar.vue'
import MovieList from '@/components/MovieList.vue' import MovieList from '@/components/MovieList.vue'
import type { MovieType } from '@/types/Movie' import type { MovieType } from '@/types/Movie'
@@ -23,6 +23,7 @@ async function searchMovie() {
searchPage.value = 1 searchPage.value = 1
const result = await searchMovies(searchQuery.value) const result = await searchMovies(searchQuery.value)
if (!result.Response) { if (!result.Response) {
movies.value = []
seachError.value = result.ErrorMessage seachError.value = result.ErrorMessage
return return
} }
@@ -41,6 +42,7 @@ async function searchMovie() {
loadingImages.value[m.imdbID] = true loadingImages.value[m.imdbID] = true
} }
} catch (error) { } catch (error) {
movies.value = []
console.error(error) console.error(error)
seachError.value = (error as Error).message seachError.value = (error as Error).message
} finally { } finally {
@@ -82,6 +84,21 @@ onMounted(() => {
movies.value = state.movieList movies.value = state.movieList
} }
}) })
let timeoutId: number | null = null
watch(searchQuery, () => {
if (timeoutId) {
clearTimeout(timeoutId)
}
timeoutId = window.setTimeout(() => {
if (searchQuery.value.length > 2) {
searchMovie()
}
timeoutId = null
}, 500)
})
</script> </script>
<template> <template>