Skip to content

Commit

Permalink
feat: add advanced search for custom search queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Thundernerd committed Jan 1, 2024
1 parent 2eea7be commit 7894419
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 12 deletions.
9 changes: 8 additions & 1 deletion src/Mangarr.Frontend/Pages/Manga/Link/Content.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
@using Mangarr.Shared.Models
<div id="wrapper" class="overflow-auto p-4">
<div class="mb-4">
<form class="border rounded p-2">
<p class="lead">Advanced</p>
<input class="form-control mt-2" type="text" @oninput="SearchQueryChanged" placeholder="Custom search query"/>
</form>
</div>

@if (_isSearching)
{
<Spinner/>
Expand All @@ -12,7 +19,7 @@
{
@foreach (ProviderModel item in _items)
{
<ContentSource Item="@item" Id="@Id"/>
<ContentSource Item="@item" Id="@Id" CustomSearchQuery="@CustomSearchQuery"/>
}
}
</div>
44 changes: 40 additions & 4 deletions src/Mangarr.Frontend/Pages/Manga/Link/Content.razor.cs
Original file line number Diff line number Diff line change
@@ -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<ProviderModel> _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();

Expand All @@ -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);
}
}
58 changes: 51 additions & 7 deletions src/Mangarr.Frontend/Pages/Manga/Link/ContentSource.razor.cs
Original file line number Diff line number Diff line change
@@ -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<ProviderMangaModel> _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<ProviderMangaModel> _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<ProviderSearchResponse> 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
Expand Down Expand Up @@ -65,8 +112,5 @@ private async void SearchAsync()
}
}
}

_isSearching = false;
await InvokeAsync(StateHasChanged);
}
}

0 comments on commit 7894419

Please sign in to comment.