Wave/Wave/Components/Pages/ArticleEditor.razor

82 lines
2.1 KiB
Plaintext

@page "/article/new"
@page "/article/{id:guid}/edit"
@using Vite.AspNetCore
@using Wave.Data
@using Wave.Utilities
@rendermode InteractiveServer
@attribute [Authorize(Policy = "ArticleEditPermissions")]
@inject ILogger<ArticleEditor> Logger
@inject NavigationManager Navigation
@inject IStringLocalizer<ArticleEditor> Localizer
@inject IMessageDisplay Message
@inject IViteManifest ViteManifest
@inject IViteDevServerStatus ViteServer
@inject IJSRuntime JS
<PageTitle>@(Localizer["EditorTitle"] + TitlePostfix)</PageTitle>
<h1 class="text-3xl lg:text-5xl font-light mb-6 text-primary">@Localizer["EditorTitle"]</h1>
<div id="editor">
<div class="flex place-content-center">
<div class="flex flex-col gap-2 items-center">
<span class="loading loading-spinner loading-lg"></span>
<p>Loading Interactive Editor </p>
</div>
</div>
</div>
@code {
[CascadingParameter(Name = "TitlePostfix")]
private string TitlePostfix { get; set; } = default!;
[Parameter]
public Guid? Id { get; set; }
private Data.Transactional.ArticleView? Article { get; set; }
private bool Saving { get; set; }
private IReadOnlyList<Category> Categories { get; } = [];
private bool ReactImported { get; set; }
protected override async Task OnAfterRenderAsync(bool first) {
if (!first) return;
if (!ReactImported) {
ReactImported = true;
string mainModule = ViteServer.IsEnabled
? $"{ViteServer.ServerUrlWithBasePath}/main.tsx"
: $"/dist/{ViteManifest["main.tsx"]!.File}";
await JS.InvokeVoidAsync("import", mainModule);
}
Article = new(Guid.NewGuid(), "", "", "", "", "", ArticleStatus.Draft, DateTimeOffset.MaxValue, []);
}
private async Task OnValidSubmit() {
try {
Saving = true;
if (false is false) {
Message.ShowError("Permission denied.");
return;
}
Message.ShowSuccess(Localizer["Save_Success"]);
if (Navigation.Uri.EndsWith("/article/new")) {
Navigation.NavigateTo($"/article/{Id!.Value}/edit", false, true);
}
} catch (Exception ex) {
Message.ShowError(Localizer["Save_Error"]);
Logger.LogError(ex, "Failed to save article.");
} finally {
Saving = false;
await InvokeAsync(StateHasChanged);
}
}
}