fixed Article Editor double-load bug

This commit is contained in:
Mia Rose Winter 2024-03-26 13:55:42 +01:00
parent 85464def08
commit 670612500c
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
2 changed files with 15 additions and 9 deletions

View file

@ -6,6 +6,7 @@
@using System.Security.Claims
@rendermode @(new InteractiveServerRenderMode(true))
@attribute [Authorize(Policy = "ArticleEditPermissions")]
@inject UserManager<ApplicationUser> UserManager
@inject IStringLocalizer<ArticleEditor> Localizer
@ -36,18 +37,22 @@
[CascadingParameter(Name = "TitlePostfix")]
private string TitlePostfix { get; set; } = default!;
[CascadingParameter]
private Task<AuthenticationState>? AuthenticationState { get; set; }
private Task<AuthenticationState> AuthenticationState { get; set; } = default!;
[Parameter]
public Guid? Id { get; set; }
private ApplicationUser? User { get; set; }
private ClaimsPrincipal? ClaimsUser { get; set; }
protected override async Task OnInitializedAsync() {
if (AuthenticationState is null) throw new ApplicationException("???");
var state = await AuthenticationState;
ClaimsUser = state.User;
var user = await UserManager.GetUserAsync(state.User);
User = user ?? throw new ApplicationException("???2");
protected override async Task OnAfterRenderAsync(bool firstRender) {
if (firstRender) {
if (User is not null) return;
var state = await AuthenticationState;
ClaimsUser = state.User;
var user = await UserManager.GetUserAsync(state.User);
User = user ?? throw new ApplicationException("???2");
await InvokeAsync(StateHasChanged);
}
}
}

View file

@ -128,7 +128,8 @@
protected override async Task OnInitializedAsync() {
await using var context = await ContextFactory.CreateDbContextAsync();
Categories = await context.Set<Category>().IgnoreQueryFilters().OrderBy(c => c.Color).ToListAsync();
if (Categories.Count < 1)
Categories = await context.Set<Category>().IgnoreQueryFilters().OrderBy(c => c.Color).ToListAsync();
Article? article = null;
if (Id is not null) {