|
| 1 | +@using TomAndJerry.Services |
| 2 | +@inject ISearchService SearchService |
| 3 | +@inject IVideoService VideoService |
| 4 | +@inject NavigationManager NavigationManager |
| 5 | +@inject IJSRuntime JSRuntime |
| 6 | + |
| 7 | +<div class="relative"> |
| 8 | + <div class="flex items-center bg-white border border-gray-300 rounded-full shadow-sm hover:shadow-md transition-shadow"> |
| 9 | + <input type="text" |
| 10 | + @bind="searchTerm" |
| 11 | + @bind:event="oninput" |
| 12 | + @onkeydown="HandleKeyDown" |
| 13 | + class="flex-1 px-4 py-2 text-sm border-0 rounded-l-full focus:outline-none focus:ring-0" |
| 14 | + placeholder="Search Tom & Jerry episodes..."> |
| 15 | + |
| 16 | + <button class="px-4 py-2 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded-r-full transition-colors" |
| 17 | + @onclick="PerformSearch"> |
| 18 | + <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 19 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path> |
| 20 | + </svg> |
| 21 | + </button> |
| 22 | + </div> |
| 23 | + |
| 24 | + <!-- Search Suggestions Dropdown --> |
| 25 | + @if (showSuggestions && searchSuggestions.Any()) |
| 26 | + { |
| 27 | + <div class="absolute top-full left-0 right-0 mt-1 bg-white border border-gray-200 rounded-lg shadow-lg z-50 max-h-60 overflow-y-auto"> |
| 28 | + @foreach (var suggestion in searchSuggestions) |
| 29 | + { |
| 30 | + <button class="w-full px-4 py-2 text-left text-sm text-gray-700 hover:bg-gray-100 transition-colors" |
| 31 | + @onclick="() => SelectSuggestion(suggestion)"> |
| 32 | + <div class="flex items-center space-x-2"> |
| 33 | + <svg class="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 34 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path> |
| 35 | + </svg> |
| 36 | + <span>@suggestion</span> |
| 37 | + </div> |
| 38 | + </button> |
| 39 | + } |
| 40 | + </div> |
| 41 | + } |
| 42 | + |
| 43 | + <!-- Search Results Count --> |
| 44 | + @if (!string.IsNullOrEmpty(searchTerm) && searchResults.Any()) |
| 45 | + { |
| 46 | + <div class="absolute top-full left-0 right-0 mt-1 bg-white border border-gray-200 rounded-lg shadow-lg z-40 p-3"> |
| 47 | + <div class="flex items-center justify-between text-sm text-gray-600"> |
| 48 | + <span>@searchResults.Count() results found</span> |
| 49 | + <button class="text-red-600 hover:text-red-800 font-medium" |
| 50 | + @onclick="NavigateToSearchResults"> |
| 51 | + View all results |
| 52 | + </button> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + } |
| 56 | +</div> |
| 57 | + |
| 58 | +@code { |
| 59 | + private string searchTerm = string.Empty; |
| 60 | + private IEnumerable<string> searchSuggestions = Enumerable.Empty<string>(); |
| 61 | + private IEnumerable<TomAndJerry.Model.Video> searchResults = Enumerable.Empty<TomAndJerry.Model.Video>(); |
| 62 | + private bool showSuggestions = false; |
| 63 | + private Timer? searchTimer; |
| 64 | + |
| 65 | + protected override void OnInitialized() |
| 66 | + { |
| 67 | + SearchService.OnSearchResultsChanged += OnSearchResultsChanged; |
| 68 | + |
| 69 | + // Initialize search timer for debouncing |
| 70 | + searchTimer = new Timer(async _ => await DebouncedSearch(), null, Timeout.Infinite, Timeout.Infinite); |
| 71 | + } |
| 72 | + |
| 73 | + private void OnSearchResultsChanged(IEnumerable<TomAndJerry.Model.Video> results) |
| 74 | + { |
| 75 | + searchResults = results; |
| 76 | + InvokeAsync(StateHasChanged); |
| 77 | + } |
| 78 | + |
| 79 | + private async Task DebouncedSearch() |
| 80 | + { |
| 81 | + if (string.IsNullOrWhiteSpace(searchTerm)) |
| 82 | + { |
| 83 | + searchSuggestions = Enumerable.Empty<string>(); |
| 84 | + searchResults = Enumerable.Empty<TomAndJerry.Model.Video>(); |
| 85 | + showSuggestions = false; |
| 86 | + await InvokeAsync(StateHasChanged); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Get search suggestions |
| 91 | + searchSuggestions = await SearchService.GetSearchSuggestionsAsync(searchTerm); |
| 92 | + |
| 93 | + // Perform search |
| 94 | + await SearchService.SearchAsync(searchTerm); |
| 95 | + |
| 96 | + showSuggestions = searchSuggestions.Any(); |
| 97 | + await InvokeAsync(StateHasChanged); |
| 98 | + } |
| 99 | + |
| 100 | + private async Task HandleKeyDown(KeyboardEventArgs e) |
| 101 | + { |
| 102 | + if (e.Key == "Enter") |
| 103 | + { |
| 104 | + PerformSearch(); |
| 105 | + } |
| 106 | + else if (e.Key == "Escape") |
| 107 | + { |
| 108 | + showSuggestions = false; |
| 109 | + await InvokeAsync(StateHasChanged); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + // Debounce search input |
| 114 | + searchTimer?.Change(300, Timeout.Infinite); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private void PerformSearch() |
| 119 | + { |
| 120 | + if (!string.IsNullOrEmpty(searchTerm)) |
| 121 | + { |
| 122 | + NavigationManager.NavigateTo($"Search/{Uri.EscapeDataString(searchTerm)}"); |
| 123 | + } |
| 124 | + showSuggestions = false; |
| 125 | + } |
| 126 | + |
| 127 | + private void SelectSuggestion(string suggestion) |
| 128 | + { |
| 129 | + searchTerm = suggestion; |
| 130 | + showSuggestions = false; |
| 131 | + PerformSearch(); |
| 132 | + } |
| 133 | + |
| 134 | + private void NavigateToSearchResults() |
| 135 | + { |
| 136 | + if (!string.IsNullOrEmpty(searchTerm)) |
| 137 | + { |
| 138 | + NavigationManager.NavigateTo($"Search/{Uri.EscapeDataString(searchTerm)}"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public void Dispose() |
| 143 | + { |
| 144 | + SearchService.OnSearchResultsChanged -= OnSearchResultsChanged; |
| 145 | + searchTimer?.Dispose(); |
| 146 | + } |
| 147 | +} |
0 commit comments