feat(ui): add PersonFormDialog shared component
- Reusable create/edit dialog for person management - 4 bUnit tests: validation, create, edit, server error
This commit is contained in:
1 parent
89cf524165
commit
5a548ab3d9
2 files changed
+240
No files matched your search
@@ -0,0 +1,139 @@
|
||||
@using Heimdall.Web.Services
|
||||
|
||||
<MudDialog @bind-Visible="Visible">
|
||||
<TitleContent>
|
||||
<MudText Typo="Typo.h6">@(IsEdit ? "Edit Person" : "New Person")</MudText>
|
||||
</TitleContent>
|
||||
<DialogContent>
|
||||
<MudTextField @bind-Value="_displayName" Label="Name *" Variant="Variant.Outlined"
|
||||
Class="mb-4" Required="true" Immediate="true"
|
||||
HelperText="Display name of the person" />
|
||||
<MudTextField @bind-Value="_photoUrl" Label="Photo URL" Variant="Variant.Outlined"
|
||||
Class="mb-4" HelperText="URL of the main photo (optional)" />
|
||||
<MudTextField @bind-Value="_notes" Label="Notes" Variant="Variant.Outlined"
|
||||
Class="mb-4" Lines="3"
|
||||
HelperText="Free-form notes about this person" />
|
||||
<MudSelect T="string" @bind-Value="_category" Label="Category" Variant="Variant.Outlined"
|
||||
Class="mb-4">
|
||||
<MudSelectItem T="string" Value="@("")">— None —</MudSelectItem>
|
||||
<MudSelectItem T="string" Value="@("family")">Family</MudSelectItem>
|
||||
<MudSelectItem T="string" Value="@("employee")">Employee</MudSelectItem>
|
||||
<MudSelectItem T="string" Value="@("watchlist")">Watchlist</MudSelectItem>
|
||||
<MudSelectItem T="string" Value="@("unknown")">Unknown</MudSelectItem>
|
||||
</MudSelect>
|
||||
<MudSwitch T="bool" @bind-Value="_isWatchlisted" Color="Color.Warning"
|
||||
Label="Watchlisted"
|
||||
HelperText="Prioritised in alert notifications" Class="mb-4" />
|
||||
@if (!string.IsNullOrEmpty(_error))
|
||||
{
|
||||
<MudAlert Severity="Severity.Error" Class="mb-4">@_error</MudAlert>
|
||||
}
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton Variant="Variant.Text" OnClick="@Cancel">Cancel</MudButton>
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@Submit"
|
||||
Disabled="@(_saving)">
|
||||
@if (_saving) { <MudProgressCircular Size="Size.Small" Class="mr-1" /> }
|
||||
@(IsEdit ? "Save" : "Create")
|
||||
</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[Parameter] public bool Visible { get; set; }
|
||||
[Parameter] public EventCallback<bool> VisibleChanged { get; set; }
|
||||
[Parameter] public EventCallback<PersonClientItem> OnSaved { get; set; }
|
||||
[Parameter] public PersonClientItem? Existing { get; set; }
|
||||
|
||||
[Inject] private HeimdallApiClient Api { get; set; } = default!;
|
||||
[Inject] private ISnackbar Snackbar { get; set; } = default!;
|
||||
|
||||
private bool IsEdit => Existing is not null;
|
||||
private string _displayName = "";
|
||||
private string? _photoUrl;
|
||||
private string? _notes;
|
||||
private string? _category;
|
||||
private bool _isWatchlisted;
|
||||
private bool _saving;
|
||||
private string _error = "";
|
||||
|
||||
protected override void OnParametersSet()
|
||||
{
|
||||
if (Existing is not null)
|
||||
{
|
||||
_displayName = Existing.DisplayName;
|
||||
_photoUrl = Existing.PhotoUrl;
|
||||
_notes = Existing.Notes;
|
||||
_category = Existing.Category;
|
||||
_isWatchlisted = Existing.IsWatchlisted;
|
||||
}
|
||||
else
|
||||
{
|
||||
_displayName = ""; _photoUrl = null; _notes = null;
|
||||
_category = null; _isWatchlisted = false;
|
||||
}
|
||||
_error = "";
|
||||
}
|
||||
|
||||
private async Task Submit()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_displayName))
|
||||
{
|
||||
_error = "Name is required";
|
||||
return;
|
||||
}
|
||||
_saving = true;
|
||||
_error = "";
|
||||
try
|
||||
{
|
||||
PersonClientItem? result;
|
||||
if (IsEdit)
|
||||
{
|
||||
var ok = await Api.UpdatePersonAsync(new PersonClientItem
|
||||
{
|
||||
Id = Existing!.Id,
|
||||
DisplayName = _displayName.Trim(),
|
||||
Notes = _notes,
|
||||
PhotoUrl = _photoUrl,
|
||||
Category = _category,
|
||||
IsWatchlisted = _isWatchlisted,
|
||||
FaceCount = Existing.FaceCount,
|
||||
CreatedAt = Existing.CreatedAt
|
||||
});
|
||||
if (!ok) { _error = "Failed to update person"; return; }
|
||||
result = new PersonClientItem
|
||||
{
|
||||
Id = Existing!.Id,
|
||||
DisplayName = _displayName.Trim(),
|
||||
Notes = _notes,
|
||||
PhotoUrl = _photoUrl,
|
||||
Category = _category,
|
||||
IsWatchlisted = _isWatchlisted,
|
||||
FaceCount = Existing.FaceCount,
|
||||
CreatedAt = Existing.CreatedAt
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
var apiResult = await Api.CreatePersonAsync(
|
||||
_displayName.Trim(), _category, _notes, _isWatchlisted, _photoUrl);
|
||||
if (!apiResult.Succeeded || apiResult.Data is null)
|
||||
{
|
||||
_error = apiResult.ErrorMessage ?? "Failed to create person";
|
||||
return;
|
||||
}
|
||||
result = apiResult.Data;
|
||||
}
|
||||
Snackbar.Add(IsEdit ? "Person updated" : "Person created", Severity.Success);
|
||||
await VisibleChanged.InvokeAsync(false);
|
||||
await OnSaved.InvokeAsync(result);
|
||||
}
|
||||
catch { _error = UiErrors.OperationFailed(); }
|
||||
finally { _saving = false; }
|
||||
}
|
||||
|
||||
private async Task Cancel()
|
||||
{
|
||||
await VisibleChanged.InvokeAsync(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
using Bunit;
|
||||
using Heimdall.Web.Components.Shared;
|
||||
using Heimdall.Web.Services;
|
||||
using Heimdall.Web.Tests.Components.Pages;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MudBlazor;
|
||||
using Shouldly;
|
||||
|
||||
namespace Heimdall.Web.Tests.Components.FormDialogs;
|
||||
|
||||
public class PersonFormDialogTests : PageTestBase
|
||||
{
|
||||
private static readonly Guid PersonId = Guid.NewGuid();
|
||||
|
||||
private static PersonClientItem Existing() => new()
|
||||
{
|
||||
Id = PersonId,
|
||||
DisplayName = "Alice",
|
||||
Category = "family",
|
||||
IsWatchlisted = false,
|
||||
FaceCount = 0,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void PersonFormDialog_Creation_SansNom_Erreur()
|
||||
{
|
||||
LoginAs();
|
||||
var cut = RenderPage<PersonFormDialog>(p => p.Add(x => x.Visible, true));
|
||||
|
||||
cut.FindAll("div.mud-dialog-actions button").Single(b => b.TextContent.Trim() == "Create").Click();
|
||||
|
||||
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Name is required"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PersonFormDialog_Creation_Reussi_AppellePost()
|
||||
{
|
||||
Handler.On(HttpMethod.Post, "api/persons",
|
||||
() => RoutedHandler.JsonResponse(System.Net.HttpStatusCode.OK,
|
||||
new PersonClientItem { Id = PersonId, DisplayName = "New Person" }));
|
||||
LoginAs();
|
||||
|
||||
var cut = RenderPage<PersonFormDialog>(p => p.Add(x => x.Visible, true));
|
||||
|
||||
cut.FindAll("input")[0].Input("New Person");
|
||||
await cut.InvokeAsync(() => cut.FindAll("div.mud-dialog-actions 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 async Task PersonFormDialog_Modification_AppellePut()
|
||||
{
|
||||
Handler.On(HttpMethod.Put, "api/persons/", System.Net.HttpStatusCode.OK);
|
||||
LoginAs();
|
||||
|
||||
var cut = RenderPage<PersonFormDialog>(p =>
|
||||
{
|
||||
p.Add(x => x.Visible, true);
|
||||
p.Add(x => x.Existing, Existing());
|
||||
});
|
||||
|
||||
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Edit Person"));
|
||||
|
||||
cut.FindAll("input")[0].Input("Alice Updated");
|
||||
await cut.InvokeAsync(() => cut.FindAll("div.mud-dialog-actions button")
|
||||
.Single(b => b.TextContent.Trim() == "Save").Click());
|
||||
|
||||
cut.WaitForAssertion(() =>
|
||||
{
|
||||
Handler.Requests.Count(r => r.Method == HttpMethod.Put
|
||||
&& r.RequestUri!.PathAndQuery.Contains("api/persons")).ShouldBe(1);
|
||||
var snackbar = Services.GetRequiredService<ISnackbar>();
|
||||
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
||||
.ShouldContain(m => m.Contains("updated", StringComparison.OrdinalIgnoreCase));
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PersonFormDialog_Creation_ErreurServeur_AfficheMessage()
|
||||
{
|
||||
Handler.On(HttpMethod.Post, "api/persons",
|
||||
() => RoutedHandler.JsonResponse(System.Net.HttpStatusCode.BadRequest,
|
||||
new { error = "Display name already exists" }));
|
||||
LoginAs();
|
||||
|
||||
var cut = RenderPage<PersonFormDialog>(p => p.Add(x => x.Visible, true));
|
||||
|
||||
cut.FindAll("input")[0].Input("New Person");
|
||||
await cut.InvokeAsync(() => cut.FindAll("div.mud-dialog-actions button").Single(b => b.TextContent.Trim() == "Create").Click());
|
||||
|
||||
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Display name already exists"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user