diff --git a/src/Mangarr.Frontend/Pages/Manga/Link/Content.razor b/src/Mangarr.Frontend/Pages/Manga/Link/Content.razor index 956e611..b6d4b15 100644 --- a/src/Mangarr.Frontend/Pages/Manga/Link/Content.razor +++ b/src/Mangarr.Frontend/Pages/Manga/Link/Content.razor @@ -1,5 +1,12 @@ @using Mangarr.Shared.Models
+
+
+

Advanced

+ +
+
+ @if (_isSearching) { @@ -12,7 +19,7 @@ { @foreach (ProviderModel item in _items) { - + } }
diff --git a/src/Mangarr.Frontend/Pages/Manga/Link/Content.razor.cs b/src/Mangarr.Frontend/Pages/Manga/Link/Content.razor.cs index f228ed2..a44177a 100644 --- a/src/Mangarr.Frontend/Pages/Manga/Link/Content.razor.cs +++ b/src/Mangarr.Frontend/Pages/Manga/Link/Content.razor.cs @@ -1,19 +1,20 @@ using FluentResults; +using Mangarr.Frontend.Api; using Mangarr.Shared.Models; using Mangarr.Shared.Responses; -using Mangarr.Frontend.Api; using Microsoft.AspNetCore.Components; namespace Mangarr.Frontend.Pages.Manga.Link; public partial class Content { - [Parameter] public string Id { get; set; } - [Inject] public BackendApi BackendApi { get; set; } = null!; - private readonly List _items = new(); + private CancellationTokenSource _cancellationTokenSource = new(); private bool _isSearching; + [Parameter] public string Id { get; set; } + [Parameter] public string? CustomSearchQuery { get; set; } + [Inject] public BackendApi BackendApi { get; set; } = null!; protected override void OnInitialized() => SearchAsync(); @@ -37,4 +38,39 @@ private async void SearchAsync() _isSearching = false; await InvokeAsync(StateHasChanged); } + + private Task SearchQueryChanged(ChangeEventArgs args) + { + string query = args.Value?.ToString() ?? string.Empty; + _cancellationTokenSource.Cancel(); + _cancellationTokenSource = new CancellationTokenSource(); + return UpdateSearch(query, _cancellationTokenSource.Token); + } + + private async Task UpdateSearch(string query, CancellationToken ct) + { + _isSearching = true; + await InvokeAsync(StateHasChanged); + + // Capture the cancellation of the search in case other keys are pressed + try + { + await Task.Delay(500, ct); + } + catch (TaskCanceledException) + { + return; + } + + // In case we're slipping through + if (ct.IsCancellationRequested) + { + return; + } + + CustomSearchQuery = query; + + _isSearching = false; + await InvokeAsync(StateHasChanged); + } } diff --git a/src/Mangarr.Frontend/Pages/Manga/Link/ContentSource.razor.cs b/src/Mangarr.Frontend/Pages/Manga/Link/ContentSource.razor.cs index df08a1b..8f6cfcc 100644 --- a/src/Mangarr.Frontend/Pages/Manga/Link/ContentSource.razor.cs +++ b/src/Mangarr.Frontend/Pages/Manga/Link/ContentSource.razor.cs @@ -1,31 +1,78 @@ using FluentResults; +using Mangarr.Frontend.Api; using Mangarr.Shared.Models; using Mangarr.Shared.Responses; -using Mangarr.Frontend.Api; using Microsoft.AspNetCore.Components; namespace Mangarr.Frontend.Pages.Manga.Link; public partial class ContentSource { + private readonly List _items = new(); + private string? _customSearchQuery; + private bool _isSearching; [Parameter] public string Id { get; set; } + [Parameter] public string? CustomSearchQuery { get; set; } [Parameter] public ProviderModel Item { get; set; } = null!; [Inject] public BackendApi BackendApi { get; set; } = null!; - private readonly List _items = new(); - private bool _isSearching; - private string Identifier => Item.Identifier; private string Title => Item.Name; protected override void OnInitialized() => SearchAsync(); + protected override void OnParametersSet() + { + if (CustomSearchQuery != _customSearchQuery) + { + SearchAsync(); + _customSearchQuery = CustomSearchQuery; + } + } + private async void SearchAsync() { _isSearching = true; await InvokeAsync(StateHasChanged); + if (!string.IsNullOrEmpty(CustomSearchQuery)) + { + await SearchByCustom(); + } + else + { + await SearchById(); + } + + _isSearching = false; + await InvokeAsync(StateHasChanged); + } + + private async Task SearchByCustom() + { + Result result = await BackendApi.SearchProvider(Item.Identifier, CustomSearchQuery); + + if (result.IsFailed) + { + // TODO: Log error + } + else + { + foreach (ProviderMangaModel providerMangaModel in result.Value.Data) + { + if (_items.Any(x => x.Id == providerMangaModel.Id)) + { + continue; + } + + _items.Add(providerMangaModel); + } + } + } + + private async Task SearchById() + { if (!int.TryParse(Id, out int id)) { // TODO: Log error @@ -65,8 +112,5 @@ private async void SearchAsync() } } } - - _isSearching = false; - await InvokeAsync(StateHasChanged); } }