diff --git a/src/Heimdall.Web/Components/Shared/PersonFormDialog.razor b/src/Heimdall.Web/Components/Shared/PersonFormDialog.razor new file mode 100644 index 0000000..b4bdc53 --- /dev/null +++ b/src/Heimdall.Web/Components/Shared/PersonFormDialog.razor @@ -0,0 +1,139 @@ +@using Heimdall.Web.Services + + + + @(IsEdit ? "Edit Person" : "New Person") + + + + + + + — None — + Family + Employee + Watchlist + Unknown + + + @if (!string.IsNullOrEmpty(_error)) + { + @_error + } + + + Cancel + + @if (_saving) { } + @(IsEdit ? "Save" : "Create") + + + + +@code { + [Parameter] public bool Visible { get; set; } + [Parameter] public EventCallback VisibleChanged { get; set; } + [Parameter] public EventCallback 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); + } +} \ No newline at end of file diff --git a/tests/Heimdall.Web.Tests/Components/Shared/PersonFormDialogTests.cs b/tests/Heimdall.Web.Tests/Components/Shared/PersonFormDialogTests.cs new file mode 100644 index 0000000..1dbdd66 --- /dev/null +++ b/tests/Heimdall.Web.Tests/Components/Shared/PersonFormDialogTests.cs @@ -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(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(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(); + 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(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(); + 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(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")); + } +} \ No newline at end of file