From 66b14baee8a1b2bea0e9357bd85e6a8e4a285877 Mon Sep 17 00:00:00 2001 From: Mia Winter Date: Tue, 28 May 2024 13:33:09 +0200 Subject: [PATCH] Implemented editing Email Subscribers --- Wave.sln | 2 +- Wave/Components/Pages/EditSubscriber.razor | 103 +++++++++++++++ Wave/Components/Pages/Subscribers.razor | 12 +- .../Pages/EditSubscriber.de-DE.resx | 125 ++++++++++++++++++ .../Pages/EditSubscriber.en-GB.resx | 101 ++++++++++++++ .../Components/Pages/EditSubscriber.resx | 125 ++++++++++++++++++ .../Components/Pages/Subscribers.de-DE.resx | 3 + .../Components/Pages/Subscribers.resx | 3 + 8 files changed, 470 insertions(+), 4 deletions(-) create mode 100644 Wave/Components/Pages/EditSubscriber.razor create mode 100644 Wave/Resources/Components/Pages/EditSubscriber.de-DE.resx create mode 100644 Wave/Resources/Components/Pages/EditSubscriber.en-GB.resx create mode 100644 Wave/Resources/Components/Pages/EditSubscriber.resx diff --git a/Wave.sln b/Wave.sln index 92ec29f..7124296 100644 --- a/Wave.sln +++ b/Wave.sln @@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wave", "Wave\Wave.csproj", EndProject Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{FE5DA24A-8490-4DCE-BDFB-49C9CF656F8A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wave.Tests", "Wave.Tests\Wave.Tests.csproj", "{54BFBF0E-5918-4830-BCDD-135BAD702529}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wave.Tests", "Wave.Tests\Wave.Tests.csproj", "{54BFBF0E-5918-4830-BCDD-135BAD702529}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Wave/Components/Pages/EditSubscriber.razor b/Wave/Components/Pages/EditSubscriber.razor new file mode 100644 index 0000000..9996b17 --- /dev/null +++ b/Wave/Components/Pages/EditSubscriber.razor @@ -0,0 +1,103 @@ +@page "/Subscribers/edit/{id:guid}" +@using Microsoft.EntityFrameworkCore +@using Wave.Data +@using Wave.Utilities + +@attribute [Authorize(Roles = "Admin")] + +@inject ILogger Logger +@inject IStringLocalizer Localizer +@inject IDbContextFactory ContextFactory +@inject IMessageDisplay Message +@inject NavigationManager Navigation + +@(Localizer["Title"] + TitlePostfix) + + + + @if (Model is null) { +

not found

+ } else { + +

@Localizer["Title"]

+

@Email

+ + + +
+ +
+ + +
+ } +
+
+ +@code { + [CascadingParameter(Name = "TitlePostfix")] + private string TitlePostfix { get; set; } = default!; + [Parameter] + public Guid Id { get; set; } + + [SupplyParameterFromForm(FormName = "EditSubscriber")] + private EditModel? Model { get; set; } + + private string Email { get; set; } = string.Empty; + + protected override async Task OnInitializedAsync() { + await using var context = await ContextFactory.CreateDbContextAsync(); + var subscriber = await context.Set() + .IgnoreQueryFilters() + .IgnoreAutoIncludes() + .FirstOrDefaultAsync(s => s.Id == Id); + + if (subscriber is null) { + Message.ShowError(Localizer["Error_NotFound"]); + } else if (Model is null) { + Email = subscriber.Email; + Model = new EditModel { + Name = subscriber.Name ?? string.Empty, + Subscribed = !subscriber.Unsubscribed + }; + await InvokeAsync(StateHasChanged); + } + } + private async Task Submit() { + if (Model is null) return; + + try { + await using var context = await ContextFactory.CreateDbContextAsync(); + var subscriber = await context.Set() + .IgnoreQueryFilters() + .IgnoreAutoIncludes() + .FirstOrDefaultAsync(s => s.Id == Id); + + if (subscriber is null) { + Message.ShowError(Localizer["Error_NotFound"]); + return; + } + + subscriber.Name = string.IsNullOrWhiteSpace(Model.Name) ? null : Model.Name; + subscriber.Unsubscribed = !Model.Subscribed; + + context.Update(subscriber); + await context.SaveChangesAsync(); + } catch (Exception ex) { + Logger.LogError(ex, "Failed to save changes to Email Subscriber {Email}.", Email); + Message.ShowError(Localizer["Submit_Error"]); + } + Message.ShowSuccess(Localizer["Submit_Success"]); + + Navigation.NavigateTo("/subscribers"); + } + + private sealed class EditModel { + public string Name { get; set; } = string.Empty; + public bool Subscribed { get; set; } = false; + } +} diff --git a/Wave/Components/Pages/Subscribers.razor b/Wave/Components/Pages/Subscribers.razor index 69c7aa2..624b186 100644 --- a/Wave/Components/Pages/Subscribers.razor +++ b/Wave/Components/Pages/Subscribers.razor @@ -13,7 +13,6 @@ @inject IDbContextFactory ContextFactory @inject ILogger Logger @inject IMessageDisplay Message -@inject IOptions Features @@ -77,6 +76,7 @@ @Localizer["Header_LastOpen"] @Localizer["Header_UnsubscribeReason"] @Localizer["Header_Subscribed"] + @@ -87,7 +87,14 @@ @context.LastMailReceived?.ToString("g") @context.LastMailOpened?.ToString("g") @context.UnsubscribeReason - + + + + + + @Localizer["Subscriber_Edit"] + + @@ -142,7 +149,6 @@ [SupplyParameterFromQuery] public int Items { get; set; } - private const int ItemsPerPage = 10; private int TotalPages { get; set; } [SupplyParameterFromForm(FormName = "AddSubscribers")] diff --git a/Wave/Resources/Components/Pages/EditSubscriber.de-DE.resx b/Wave/Resources/Components/Pages/EditSubscriber.de-DE.resx new file mode 100644 index 0000000..01f6d65 --- /dev/null +++ b/Wave/Resources/Components/Pages/EditSubscriber.de-DE.resx @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Abonnent Bearbeiten + + + Anna Muster + + + Name (Optional) + + + Aktiv (erhält E-Mails) + + + Speichern + + + Abonnent nicht gefunden (Vielleicht wurde dieser gelöscht?) + + + Unbekannter Fehler beim Speichern ihrer Änderungen. + + + Ihre Änderungen wurden erfolgreich gespeichert + + \ No newline at end of file diff --git a/Wave/Resources/Components/Pages/EditSubscriber.en-GB.resx b/Wave/Resources/Components/Pages/EditSubscriber.en-GB.resx new file mode 100644 index 0000000..4fdb1b6 --- /dev/null +++ b/Wave/Resources/Components/Pages/EditSubscriber.en-GB.resx @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Wave/Resources/Components/Pages/EditSubscriber.resx b/Wave/Resources/Components/Pages/EditSubscriber.resx new file mode 100644 index 0000000..fd115c3 --- /dev/null +++ b/Wave/Resources/Components/Pages/EditSubscriber.resx @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Edit Subscriber + + + Name (optional) + + + John Smith + + + Active (receives Emails) + + + Save + + + Subscriber not Found (maybe it has been deleted?) + + + Unexpected Error trying to save your changes. + + + Changes saved Successfully + + \ No newline at end of file diff --git a/Wave/Resources/Components/Pages/Subscribers.de-DE.resx b/Wave/Resources/Components/Pages/Subscribers.de-DE.resx index 817bf38..96d1caa 100644 --- a/Wave/Resources/Components/Pages/Subscribers.de-DE.resx +++ b/Wave/Resources/Components/Pages/Subscribers.de-DE.resx @@ -147,4 +147,7 @@ anna.muster@example.de; Anna Muster; Spam; peter.muster@example.de;;; + + Bearbeiten + \ No newline at end of file diff --git a/Wave/Resources/Components/Pages/Subscribers.resx b/Wave/Resources/Components/Pages/Subscribers.resx index 605449c..1d278ff 100644 --- a/Wave/Resources/Components/Pages/Subscribers.resx +++ b/Wave/Resources/Components/Pages/Subscribers.resx @@ -150,4 +150,7 @@ john.smith@example.com; John Smith; Spam; jay.smith@example.com;;; + + Edit + \ No newline at end of file