mirror of
				https://github.com/mmahdium/TBW.git
				synced 2025-11-04 09:09:24 +01:00 
			
		
		
		
	Improve search logic
This commit is contained in:
		@@ -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>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user