Changed EmailEdit to use new message system

This commit is contained in:
Mia Rose Winter 2024-02-14 21:27:43 +01:00
parent 8d59c6fbeb
commit c8dce0b024
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E

View file

@ -4,6 +4,7 @@
@using Microsoft.Extensions.Options @using Microsoft.Extensions.Options
@using Wave.Data @using Wave.Data
@using Wave.Services @using Wave.Services
@using Wave.Utilities
@inject ILogger<EmailEdit> Logger @inject ILogger<EmailEdit> Logger
@inject IStringLocalizer<EmailEdit> Localizer @inject IStringLocalizer<EmailEdit> Localizer
@ -12,15 +13,10 @@
@inject NavigationManager Navigation @inject NavigationManager Navigation
@inject IEmailSender EmailSender @inject IEmailSender EmailSender
@inject EmailTemplateService TemplateService @inject EmailTemplateService TemplateService
@inject IMessageDisplay Messages
<PageTitle>@(TitlePrefix + Localizer["Title"])</PageTitle> <PageTitle>@(TitlePrefix + Localizer["Title"])</PageTitle>
@if (!string.IsNullOrWhiteSpace(Message)) {
<div class="alert alert-success">
<span>@Message</span>
</div>
}
<BoardComponent CenterContent="true"> <BoardComponent CenterContent="true">
<BoardCardComponent Heading="@Localizer["Title"]"> <BoardCardComponent Heading="@Localizer["Title"]">
<form method="post" @formname="Unsubscribe" @onsubmit="Unsubscribe_Submit"> <form method="post" @formname="Unsubscribe" @onsubmit="Unsubscribe_Submit">
@ -46,20 +42,22 @@
[Parameter, SupplyParameterFromQuery(Name = "newsletter")] [Parameter, SupplyParameterFromQuery(Name = "newsletter")]
public string? Newsletter { get; set; } public string? Newsletter { get; set; }
private string Message { get; set; } = string.Empty; [CascadingParameter]
public HttpContext? HttpContext { get; set; }
protected override async Task OnInitializedAsync() { protected override async Task OnInitializedAsync() {
if (Id is null || Token is null || Newsletter is null) { if (Id is null || Token is null || Newsletter is null) {
if (string.IsNullOrWhiteSpace(Message)) Message = Localizer["Load_Failure_Message"]; if (HttpContext?.Request.Method.ToUpper() is "POST")
Messages.ShowError(Localizer["Load_Failure_Message"]);
return; return;
} }
try { try {
if (await GetSubscriber() is null) { if (await GetSubscriber() is null) {
Message = Localizer["Load_Failure_Message"]; Messages.ShowError(Localizer["Load_Failure_Message"]);
} }
} catch (Exception) { } catch (Exception) {
Message = Localizer["Load_Failure_Message"]; Messages.ShowError(Localizer["Load_Failure_Message"]);
} }
} }
@ -68,13 +66,13 @@
await using var context = await ContextFactory.CreateDbContextAsync(); await using var context = await ContextFactory.CreateDbContextAsync();
var subscriber = await GetSubscriber(context); var subscriber = await GetSubscriber(context);
if (subscriber is null) { if (subscriber is null) {
Message = Localizer["Unsubscribe_Failure_Message"]; Messages.ShowError(Localizer["Unsubscribe_Failure_Message"]);
return; return;
} }
subscriber.Unsubscribed = true; subscriber.Unsubscribed = true;
await context.SaveChangesAsync(); await context.SaveChangesAsync();
Message = Localizer["Unsubscribe_Success"]; Messages.ShowSuccess(Localizer["Unsubscribe_Success"]);
var customization = Customizations.Value; var customization = Customizations.Value;
string body = TemplateService.Default( string body = TemplateService.Default(
@ -87,7 +85,7 @@
} catch (EmailNotSendException ex) { } catch (EmailNotSendException ex) {
Logger.LogWarning(ex, "Failed to send unsubscribe confirm email. The user has been unsubscribed anyway."); Logger.LogWarning(ex, "Failed to send unsubscribe confirm email. The user has been unsubscribed anyway.");
} catch (Exception) { } catch (Exception) {
Message = Localizer["Unsubscribe_Failure_Message"]; Messages.ShowError(Localizer["Unsubscribe_Failure_Message"]);
} }
} }