Added a mail template editor
This commit is contained in:
9
Moonlight/App/Models/Misc/MailTemplate.cs
Normal file
9
Moonlight/App/Models/Misc/MailTemplate.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using Moonlight.App.Helpers.Files;
|
||||||
|
|
||||||
|
namespace Moonlight.App.Models.Misc;
|
||||||
|
|
||||||
|
public class MailTemplate // This is just for the blazor table at /admin/system/mail
|
||||||
|
{
|
||||||
|
public string Name { get; set; } = "";
|
||||||
|
public FileData File { get; set; }
|
||||||
|
}
|
||||||
@@ -44,6 +44,11 @@
|
|||||||
<TL>Configuration</TL>
|
<TL>Configuration</TL>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li class="nav-item mt-2">
|
||||||
|
<a class="nav-link text-active-primary ms-0 me-10 py-5 @(Index == 9 ? "active" : "")" href="/admin/system/mail">
|
||||||
|
<TL>Mail</TL>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
149
Moonlight/Shared/Views/Admin/Sys/Mail.razor
Normal file
149
Moonlight/Shared/Views/Admin/Sys/Mail.razor
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
@page "/admin/system/mail"
|
||||||
|
|
||||||
|
@using Moonlight.Shared.Components.Navigations
|
||||||
|
@using Moonlight.Shared.Components.FileManagerPartials
|
||||||
|
@using Moonlight.App.Helpers.Files
|
||||||
|
@using Moonlight.App.Helpers
|
||||||
|
@using BlazorTable
|
||||||
|
@using Moonlight.App.Models.Misc
|
||||||
|
@using Moonlight.App.Services
|
||||||
|
@using Moonlight.App.Services.Interop
|
||||||
|
|
||||||
|
@inject SmartTranslateService SmartTranslateService
|
||||||
|
@inject ToastService ToastService
|
||||||
|
@inject AlertService AlertService
|
||||||
|
|
||||||
|
<OnlyAdmin>
|
||||||
|
<AdminSystemNavigation Index="9"/>
|
||||||
|
|
||||||
|
<LazyLoader @ref="LazyLoader" Load="Load">
|
||||||
|
@if (CurrentMailTemplate == null)
|
||||||
|
{
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">
|
||||||
|
<span class="card-title">
|
||||||
|
<TL>Mail templates</TL>
|
||||||
|
</span>
|
||||||
|
<div class="card-toolbar">
|
||||||
|
<WButton Text="@(SmartTranslateService.Translate("New mail template"))"
|
||||||
|
CssClasses="btn-sm btn-success"
|
||||||
|
OnClick="CreateNewMailTemplate">
|
||||||
|
</WButton>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="table-responsive">
|
||||||
|
<Table TableItem="MailTemplate" Items="MailTemplateFiles" PageSize="25" TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3" TableHeadClass="fw-bold text-muted">
|
||||||
|
<Column TableItem="MailTemplate" Title="@(SmartTranslateService.Translate("Name"))" Field="@(x => x.Name)" Sortable="true" Filterable="true">
|
||||||
|
<Template>
|
||||||
|
@{
|
||||||
|
var name = context.Name.Replace(Path.GetExtension(context.Name), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
<span>@(name)</span>
|
||||||
|
</Template>
|
||||||
|
</Column>
|
||||||
|
<Column TableItem="MailTemplate" Title="" Field="@(x => x.Name)" Filterable="false" Sortable="false">
|
||||||
|
<Template>
|
||||||
|
<div class="text-end">
|
||||||
|
<WButton Text="@(SmartTranslateService.Translate("Edit"))"
|
||||||
|
OnClick="() => EditTemplate(context)">
|
||||||
|
</WButton>
|
||||||
|
<DeleteButton OnClick="() => DeleteTemplate(context)"
|
||||||
|
Confirm="true">
|
||||||
|
</DeleteButton>
|
||||||
|
</div>
|
||||||
|
</Template>
|
||||||
|
</Column>
|
||||||
|
<Pager ShowPageNumber="true" ShowTotalCount="true"/>
|
||||||
|
</Table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<FileEditor Language="html"
|
||||||
|
HideControls="false"
|
||||||
|
InitialData="@(CurrentMailTemplateContent)"
|
||||||
|
OnCancel="OnCancelTemplateEdit"
|
||||||
|
OnSubmit="OnSubmitTemplateEdit"/>
|
||||||
|
}
|
||||||
|
</LazyLoader>
|
||||||
|
</OnlyAdmin>
|
||||||
|
|
||||||
|
@code
|
||||||
|
{
|
||||||
|
private MailTemplate[] MailTemplateFiles;
|
||||||
|
private FileAccess FileAccess;
|
||||||
|
private LazyLoader LazyLoader;
|
||||||
|
|
||||||
|
private MailTemplate? CurrentMailTemplate;
|
||||||
|
private string CurrentMailTemplateContent;
|
||||||
|
|
||||||
|
private async Task Load(LazyLoader arg)
|
||||||
|
{
|
||||||
|
FileAccess = new HostFileAccess(PathBuilder.Dir("storage"));
|
||||||
|
|
||||||
|
await FileAccess.Cd("resources");
|
||||||
|
await FileAccess.Cd("mail");
|
||||||
|
|
||||||
|
MailTemplateFiles = (await FileAccess.Ls())
|
||||||
|
.Where(x => x.IsFile)
|
||||||
|
.Select(x => new MailTemplate()
|
||||||
|
{
|
||||||
|
Name = x.Name,
|
||||||
|
File = x
|
||||||
|
})
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task EditTemplate(MailTemplate mailTemplate)
|
||||||
|
{
|
||||||
|
CurrentMailTemplate = mailTemplate;
|
||||||
|
|
||||||
|
CurrentMailTemplateContent = await FileAccess
|
||||||
|
.Read(CurrentMailTemplate.File);
|
||||||
|
|
||||||
|
await InvokeAsync(StateHasChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task DeleteTemplate(MailTemplate mailTemplate)
|
||||||
|
{
|
||||||
|
await FileAccess.Delete(mailTemplate.File);
|
||||||
|
await LazyLoader.Reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void OnCancelTemplateEdit()
|
||||||
|
{
|
||||||
|
CurrentMailTemplate = null;
|
||||||
|
await InvokeAsync(StateHasChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void OnSubmitTemplateEdit(string text)
|
||||||
|
{
|
||||||
|
await FileAccess.Write(CurrentMailTemplate!.File, text);
|
||||||
|
|
||||||
|
await ToastService.Success(
|
||||||
|
SmartTranslateService.Translate("Successfully saved file"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task CreateNewMailTemplate()
|
||||||
|
{
|
||||||
|
var name = await AlertService.Text(
|
||||||
|
SmartTranslateService.Translate("New mail template"),
|
||||||
|
SmartTranslateService.Translate("Enter the name of the new template"),
|
||||||
|
""
|
||||||
|
);
|
||||||
|
|
||||||
|
if(string.IsNullOrEmpty(name))
|
||||||
|
return;
|
||||||
|
|
||||||
|
await FileAccess.Write(new()
|
||||||
|
{
|
||||||
|
Name = name + ".html"
|
||||||
|
}, "");
|
||||||
|
|
||||||
|
await LazyLoader.Reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user