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

View file

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