feat(ui): add PersonsListPage (/persons)
- Card grid with photo, name, category, face count, watchlist badge - Search bar + Known/Unknown filter - Create dialog via PersonFormDialog - 9 bUnit tests
This commit is contained in:
1 parent
5a548ab3d9
commit
3ef23aabab
2 files changed
+413
No files matched your search
@@ -0,0 +1,220 @@
|
||||
@page "/persons"
|
||||
@attribute [Authorize]
|
||||
@layout MainLayout
|
||||
@inject HeimdallApiClient Api
|
||||
@inject ISnackbar Snackbar
|
||||
@inject NavigationManager Nav
|
||||
@using Heimdall.Web.Components.Shared
|
||||
@using Heimdall.Web.Services
|
||||
|
||||
<MudText Typo="Typo.h1" Class="mb-1">
|
||||
<i class="fas fa-users mr-2"></i>
|
||||
Persons
|
||||
</MudText>
|
||||
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-4">
|
||||
@(_total > 0 ? $"{_total} person(s) registered" : "List of identified persons")
|
||||
</MudText>
|
||||
|
||||
@* ─── Filters ─── *@
|
||||
<MudPaper Elevation="1" Class="pa-4 mb-4" Style="border-radius: var(--hmd-radius-md);">
|
||||
<MudGrid>
|
||||
<MudItem xs="12" sm="6" md="5">
|
||||
<MudTextField @bind-Value="Search" Label="Search (name...)" Variant="Variant.Outlined"
|
||||
Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Filled.Search"
|
||||
Immediate="true" Class="mb-2 mb-md-0" />
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudSelect T="string" @bind-Value="_statusFilter" Label="Status" Variant="Variant.Outlined"
|
||||
Class="mb-2 mb-md-0">
|
||||
<MudSelectItem T="string" Value="@("")">All</MudSelectItem>
|
||||
<MudSelectItem T="string" Value="@("known")">Known</MudSelectItem>
|
||||
<MudSelectItem T="string" Value="@("unknown")">Unknown</MudSelectItem>
|
||||
</MudSelect>
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="6" md="3" Class="d-flex align-center">
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@ApplyFilters" Class="w-100">
|
||||
<MudIcon Icon="@Icons.Material.Filled.Search" Class="mr-1" Size="Size.Small" /> Filter
|
||||
</MudButton>
|
||||
</MudItem>
|
||||
</MudGrid>
|
||||
</MudPaper>
|
||||
|
||||
@* ─── 4 states ─── *@
|
||||
@if (_loading)
|
||||
{
|
||||
<MudGrid>
|
||||
@for (int i = 0; i < 4; i++)
|
||||
{
|
||||
<MudItem xs="12" sm="6" md="3">
|
||||
<MudPaper Elevation="0" Style="border-radius: var(--hmd-radius-md);">
|
||||
<MudSkeleton Variant="SkeletonVariant.Rectangular" Height="200px" />
|
||||
</MudPaper>
|
||||
</MudItem>
|
||||
}
|
||||
</MudGrid>
|
||||
}
|
||||
else if (_error)
|
||||
{
|
||||
<MudAlert Severity="Severity.Error" Variant="Variant.Filled" Class="mb-4">
|
||||
<MudText>Unable to load persons.</MudText>
|
||||
<MudButton Variant="Variant.Text" Color="Color.Inherit" OnClick="@LoadPersons" Class="mt-2">
|
||||
<MudIcon Icon="@Icons.Material.Filled.Refresh" Class="mr-1" Size="Size.Small" /> Retry
|
||||
</MudButton>
|
||||
</MudAlert>
|
||||
}
|
||||
else if (_Filtered is null || _Filtered.Count == 0)
|
||||
{
|
||||
<MudPaper Elevation="0" Class="pa-8 text-center" Style="border-radius: var(--hmd-radius-md); border: 1px dashed var(--mud-palette-lines-default);">
|
||||
<MudIcon Icon="@Icons.Material.Filled.PersonSearch" Size="Size.Large" Color="Color.Secondary" />
|
||||
<MudText Typo="Typo.h6" Color="Color.Secondary" Class="mt-4">
|
||||
@(_persons is null || _persons.Count == 0
|
||||
? "No person registered"
|
||||
: "No person matches the filters")
|
||||
</MudText>
|
||||
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-4">
|
||||
@if (_persons is null || _persons.Count == 0)
|
||||
{
|
||||
<text>Persons will appear here once the system recognises a face.</text>
|
||||
}
|
||||
else
|
||||
{
|
||||
<text>Adjust your filters or create a new person.</text>
|
||||
}
|
||||
</MudText>
|
||||
@if (_persons is null || _persons.Count == 0)
|
||||
{
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(() => _showForm = true)">
|
||||
<MudIcon Icon="@Icons.Material.Filled.Add" Class="mr-1" /> Add a person
|
||||
</MudButton>
|
||||
}
|
||||
</MudPaper>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="d-flex justify-space-between align-center mb-4">
|
||||
<MudText Typo="Typo.body2" Color="Color.Secondary">
|
||||
@_Filtered.Count person(s) shown
|
||||
</MudText>
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(() => _showForm = true)">
|
||||
<MudIcon Icon="@Icons.Material.Filled.Add" Class="mr-1" /> Add
|
||||
</MudButton>
|
||||
</div>
|
||||
|
||||
<MudGrid>
|
||||
@foreach (var person in _Filtered)
|
||||
{
|
||||
<MudItem xs="12" sm="6" md="4" lg="3">
|
||||
<MudCard Elevation="2" Style="border-radius: var(--hmd-radius-md); cursor: pointer; height: 100%;"
|
||||
@onclick="@(() => Nav.NavigateTo($"/persons/{person.Id}"))">
|
||||
<div class="d-flex align-center pa-4 gap-3">
|
||||
@if (!string.IsNullOrEmpty(person.PhotoUrl))
|
||||
{
|
||||
<MudAvatar Image="@person.PhotoUrl" Style="width:48px; height:48px;" />
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudAvatar Style="width:48px; height:48px; background: var(--mud-palette-primary);">
|
||||
<i class="fas fa-user"></i>
|
||||
</MudAvatar>
|
||||
}
|
||||
<div class="d-flex flex-column" style="min-width:0;">
|
||||
<MudText Typo="Typo.h6" Class="text-truncate">@person.DisplayName</MudText>
|
||||
<MudText Typo="Typo.body2" Color="Color.Secondary">
|
||||
@FormatCategory(person.Category)
|
||||
</MudText>
|
||||
</div>
|
||||
@if (person.IsWatchlisted)
|
||||
{
|
||||
<MudSpacer />
|
||||
<MudChip T="string" Size="Size.Small" Color="Color.Warning"
|
||||
Icon="@Icons.Material.Filled.Warning">
|
||||
Watchlisted
|
||||
</MudChip>
|
||||
}
|
||||
</div>
|
||||
<MudCardActions Justify="Justify.FlexEnd" Class="pa-4 pt-0">
|
||||
<MudChip T="string" Size="Size.Small" Variant="Variant.Outlined"
|
||||
Icon="@Icons.Material.Filled.CollectionsBookmark">
|
||||
@person.FaceCount face(s)
|
||||
</MudChip>
|
||||
<MudText Typo="Typo.caption" Color="Color.Secondary">
|
||||
Created @person.CreatedAt.ToString("d")
|
||||
</MudText>
|
||||
</MudCardActions>
|
||||
</MudCard>
|
||||
</MudItem>
|
||||
}
|
||||
</MudGrid>
|
||||
}
|
||||
|
||||
@* ─── Create form ─── *@
|
||||
<PersonFormDialog @bind-Visible="_showForm" Existing="null" OnSaved="@OnPersonCreated" />
|
||||
|
||||
@code {
|
||||
private List<PersonClientItem>? _persons;
|
||||
private List<PersonClientItem>? _Filtered;
|
||||
private bool _loading = true;
|
||||
private bool _error;
|
||||
private bool _showForm;
|
||||
private string _search = "";
|
||||
private string _statusFilter = "";
|
||||
private int _total = 0;
|
||||
|
||||
private string Search
|
||||
{
|
||||
get => _search;
|
||||
set
|
||||
{
|
||||
_search = value;
|
||||
ApplyFilters();
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task OnInitializedAsync() => await LoadPersons();
|
||||
|
||||
private async Task LoadPersons()
|
||||
{
|
||||
_loading = true;
|
||||
_error = false;
|
||||
try
|
||||
{
|
||||
_persons = await Api.GetPersonsAsync();
|
||||
_total = _persons?.Count ?? 0;
|
||||
ApplyFilters();
|
||||
}
|
||||
catch { _error = true; Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); }
|
||||
finally { _loading = false; }
|
||||
}
|
||||
|
||||
private void ApplyFilters()
|
||||
{
|
||||
if (_persons is null) { _Filtered = null; return; }
|
||||
var q = _search?.Trim();
|
||||
_Filtered = _persons
|
||||
.Where(p => string.IsNullOrEmpty(q)
|
||||
|| p.DisplayName.Contains(q, StringComparison.OrdinalIgnoreCase))
|
||||
.Where(p => string.IsNullOrEmpty(_statusFilter)
|
||||
|| (_statusFilter == "known" && p.Category != "unknown")
|
||||
|| (_statusFilter == "unknown" && p.Category == "unknown"))
|
||||
.OrderBy(p => p.DisplayName)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private async Task OnPersonCreated(PersonClientItem person)
|
||||
{
|
||||
_persons ??= [];
|
||||
_persons.Insert(0, person);
|
||||
ApplyFilters();
|
||||
_total = _persons.Count;
|
||||
Nav.NavigateTo($"/persons/{person.Id}");
|
||||
}
|
||||
|
||||
private static string FormatCategory(string? category) => category switch
|
||||
{
|
||||
"family" => "Family",
|
||||
"employee" => "Employee",
|
||||
"watchlist" => "Watchlist",
|
||||
"unknown" => "Unknown",
|
||||
_ => "—"
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
using Bunit;
|
||||
using Heimdall.Web.Components.Pages;
|
||||
using Heimdall.Web.Components.Shared;
|
||||
using Heimdall.Web.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MudBlazor;
|
||||
using Shouldly;
|
||||
|
||||
namespace Heimdall.Web.Tests.Components.Pages;
|
||||
|
||||
public class PersonsListPageTests : PageTestBase
|
||||
{
|
||||
private static readonly Guid PersonId = Guid.NewGuid();
|
||||
|
||||
private static PersonClientItem Person(string name = "Alice", string category = "family", int faces = 2) =>
|
||||
new()
|
||||
{
|
||||
Id = PersonId,
|
||||
DisplayName = name,
|
||||
Category = category,
|
||||
IsWatchlisted = false,
|
||||
FaceCount = faces,
|
||||
CreatedAt = DateTime.UtcNow.AddMonths(-1)
|
||||
};
|
||||
|
||||
private static PersonClientItem UnknownPerson() =>
|
||||
new()
|
||||
{
|
||||
Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
|
||||
DisplayName = "Unknown #1",
|
||||
Category = "unknown",
|
||||
IsWatchlisted = false,
|
||||
FaceCount = 1,
|
||||
CreatedAt = DateTime.UtcNow.AddMonths(-1)
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void PersonsListPage_AfficheListeDesPersonnes()
|
||||
{
|
||||
Handler.OnGet("api/persons", new List<PersonClientItem> { Person(), Person("Bob", "employee", 1) });
|
||||
LoginAs();
|
||||
|
||||
var cut = RenderPage<PersonsListPage>();
|
||||
|
||||
cut.WaitForAssertion(() =>
|
||||
{
|
||||
cut.Markup.ShouldContain("Alice");
|
||||
cut.Markup.ShouldContain("Bob");
|
||||
cut.Markup.ShouldContain("Family");
|
||||
cut.Markup.ShouldContain("Employee");
|
||||
cut.Markup.ShouldContain("2 face(s)");
|
||||
cut.Markup.ShouldContain("1 face(s)");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonsListPage_AucunePersonne_AfficheMessage()
|
||||
{
|
||||
Handler.OnGet("api/persons", new List<PersonClientItem>());
|
||||
LoginAs();
|
||||
|
||||
var cut = RenderPage<PersonsListPage>();
|
||||
|
||||
cut.WaitForAssertion(() =>
|
||||
{
|
||||
cut.Markup.ShouldContain("No person registered");
|
||||
cut.Markup.ShouldContain("Add a person");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonsListPage_ErreurChargement_AfficheAlerte()
|
||||
{
|
||||
Handler.OnGet("api/persons", System.Net.HttpStatusCode.InternalServerError);
|
||||
LoginAs();
|
||||
|
||||
var cut = RenderPage<PersonsListPage>();
|
||||
|
||||
cut.WaitForAssertion(() =>
|
||||
cut.Markup.ShouldContain("Unable to load persons."));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonsListPage_FiltreConnu_TransmetCategorieALApi()
|
||||
{
|
||||
// MudSelect interaction is fragile in bUnit (popover) — test the
|
||||
// client API directly: GetPersonsAsync(category) must send the
|
||||
// category query parameter (same approach as EventsPageTests).
|
||||
Handler.OnGet("api/persons", new List<PersonClientItem>());
|
||||
LoginAs();
|
||||
|
||||
var api = Services.GetRequiredService<HeimdallApiClient>();
|
||||
_ = api.GetPersonsAsync("family");
|
||||
|
||||
Handler.Requests.ShouldContain(r => r.Method == HttpMethod.Get
|
||||
&& r.RequestUri!.PathAndQuery.Contains("api/persons")
|
||||
&& r.RequestUri.Query.Contains("category=family", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonsListPage_FiltreInconnu_TransmetCategorieALApi()
|
||||
{
|
||||
Handler.OnGet("api/persons", new List<PersonClientItem>());
|
||||
LoginAs();
|
||||
|
||||
var api = Services.GetRequiredService<HeimdallApiClient>();
|
||||
_ = api.GetPersonsAsync("unknown");
|
||||
|
||||
Handler.Requests.ShouldContain(r => r.Method == HttpMethod.Get
|
||||
&& r.RequestUri!.PathAndQuery.Contains("api/persons")
|
||||
&& r.RequestUri.Query.Contains("category=unknown", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonsListPage_RechercheParNom_Filtre()
|
||||
{
|
||||
Handler.OnGet("api/persons", new List<PersonClientItem> { Person("Alice"), Person("Bob") });
|
||||
LoginAs();
|
||||
|
||||
var cut = RenderPage<PersonsListPage>();
|
||||
|
||||
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Alice"));
|
||||
|
||||
cut.FindAll("input")[0].Input("Bob");
|
||||
cut.WaitForAssertion(() =>
|
||||
{
|
||||
cut.Markup.ShouldContain("Bob");
|
||||
cut.Markup.ShouldNotContain("Alice");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonsListPage_RechercheAucunResultat()
|
||||
{
|
||||
Handler.OnGet("api/persons", new List<PersonClientItem> { Person("Alice") });
|
||||
LoginAs();
|
||||
|
||||
var cut = RenderPage<PersonsListPage>();
|
||||
|
||||
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Alice"));
|
||||
|
||||
cut.FindAll("input")[0].Input("zzz");
|
||||
cut.WaitForAssertion(() => cut.Markup.ShouldContain("No person matches the filters"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PersonsListPage_CreationPersonne_Reussi()
|
||||
{
|
||||
Handler.OnGet("api/persons", new List<PersonClientItem>());
|
||||
Handler.On(HttpMethod.Post, "api/persons",
|
||||
() => RoutedHandler.JsonResponse(System.Net.HttpStatusCode.OK,
|
||||
new PersonClientItem { Id = PersonId, DisplayName = "New Person", Category = "family" }));
|
||||
LoginAs();
|
||||
|
||||
var cut = RenderPage<PersonsListPage>();
|
||||
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Add a person"));
|
||||
|
||||
cut.FindAll("button").Single(b => b.TextContent.Contains("Add a person")).Click();
|
||||
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Name *"));
|
||||
|
||||
// The dialog content renders in the host markup (MudDialogProvider),
|
||||
// so interact via the host's elements directly.
|
||||
cut.FindAll("input")[0].Input("New Person");
|
||||
await cut.InvokeAsync(() => cut.FindAll("button").Single(b => b.TextContent.Trim() == "Create").Click());
|
||||
|
||||
cut.WaitForAssertion(() =>
|
||||
{
|
||||
Handler.Requests.Count(r => r.Method == HttpMethod.Post
|
||||
&& r.RequestUri!.PathAndQuery.Contains("api/persons")).ShouldBe(1);
|
||||
var snackbar = Services.GetRequiredService<ISnackbar>();
|
||||
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "").ShouldContain("Person created");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonsListPage_CreationPersonne_SansNom_Erreur()
|
||||
{
|
||||
Handler.OnGet("api/persons", new List<PersonClientItem>());
|
||||
LoginAs();
|
||||
|
||||
var cut = RenderPage<PersonsListPage>();
|
||||
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Add a person"));
|
||||
|
||||
cut.FindAll("button").Single(b => b.TextContent.Contains("Add a person")).Click();
|
||||
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Name *"));
|
||||
|
||||
// Find+Click in InvokeAsync: a direct Click may use a stale
|
||||
// event handler reference after the dialog re-rendered.
|
||||
cut.InvokeAsync(() => cut.FindAll("button").Single(b => b.TextContent.Trim() == "Create").Click());
|
||||
|
||||
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Name is required"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user