Improved ManageUsers Page layout

This commit is contained in:
Mia Rose Winter 2024-01-18 19:42:55 +01:00
parent 669b4df68b
commit 7fd3b5e933
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
4 changed files with 52 additions and 39 deletions

View file

@ -2,6 +2,7 @@
@using Microsoft.AspNetCore.Authorization @using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Identity @using Microsoft.AspNetCore.Identity
@using Wave.Data @using Wave.Data
@using Humanizer
@rendermode InteractiveServer @rendermode InteractiveServer
@attribute [Authorize(Policy = "RoleAssignPermissions")] @attribute [Authorize(Policy = "RoleAssignPermissions")]
@ -15,27 +16,29 @@
<section class="mb-3 overflow-x-auto"> <section class="mb-3 overflow-x-auto">
<h3 class="text-xl lg:text-3xl mb-2"> <h3 class="text-xl lg:text-3xl mb-2">
@Localizer["Authors"] @Localizer[CurrentRole.ToString().Pluralize()]
<!-- TODO:: implement --> </h3>
<div class="flex gap-2">
<InputSelect class="select select-bordered select-sm w-full max-w-xs" Value="CurrentRole"
ValueExpression="() => CurrentRole"
TValue="Role" ValueChanged="async c => await LoadAsync(c)">
@foreach (var role in Enum.GetValues<Role>()) {
<option value="@role">@Localizer[role.ToString()]</option>
}
</InputSelect>
<button class="btn btn-sm btn-square btn-ghost text-success"> <button class="btn btn-sm btn-square btn-ghost text-success">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v6m3-3H9m12 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" /> <path stroke-linecap="round" stroke-linejoin="round" d="M12 9v6m3-3H9m12 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg> </svg>
</button> </button>
</h3> </div>
<UserTable Users="Authors" RemoveCallback="@(async u => await Remove(u, "Author"))" /> @if (Busy) {
</section> <div class="flex place-content-center">
<section class="mb-3 overflow-x-auto"> <span class="loading loading-spinner loading-lg"></span>
<h3 class="text-xl lg:text-3xl mb-2">@Localizer["Reviewers"]</h3> </div>
<UserTable Users="Reviewers" RemoveCallback="@(async u => await Remove(u, "Reviewer"))" /> } else {
</section> <UserTable Users="Users" RemoveCallback="@(async u => await RemoveAsync(u, CurrentRole))"/>
<section class="mb-3 overflow-x-auto"> }
<h3 class="text-xl lg:text-3xl mb-2">@Localizer["Moderators"]</h3>
<UserTable Users="Moderators" RemoveCallback="@(async u => await Remove(u, "Moderator"))" />
</section>
<section class="mb-3 overflow-x-auto">
<h3 class="text-xl lg:text-3xl mb-2">@Localizer["Administrators"]</h3>
<UserTable Users="Admins" RemoveCallback="@(async u => await Remove(u, "Admin"))" />
</section> </section>
<ToastComponent @ref="Toast" /> <ToastComponent @ref="Toast" />
@ -43,35 +46,45 @@
@code { @code {
[CascadingParameter(Name = "TitlePrefix")] [CascadingParameter(Name = "TitlePrefix")]
private string TitlePrefix { get; set; } = default!; private string TitlePrefix { get; set; } = default!;
public ToastComponent Toast { get; set; } = null!; public ToastComponent Toast { get; set; } = null!;
private IList<ApplicationUser> Authors { get; set; } = new List<ApplicationUser>();
private IList<ApplicationUser> Reviewers { get; set; } = new List<ApplicationUser>();
private IList<ApplicationUser> Moderators { get; set; } = new List<ApplicationUser>();
private IList<ApplicationUser> Admins { get; set; } = new List<ApplicationUser>();
[CascadingParameter] [CascadingParameter]
private Task<AuthenticationState> AuthenticationState { get; set; } = default!; private Task<AuthenticationState> AuthenticationState { get; set; } = default!;
private ApplicationUser? User { get; set; } private ApplicationUser? User { get; set; }
private Role CurrentRole { get; set; } = Role.Admin;
private List<ApplicationUser> Users { get; } = [];
private bool Busy { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender) { protected override async Task OnAfterRenderAsync(bool firstRender) {
if (!firstRender) return; if (!firstRender) return;
var state = await AuthenticationState; var state = await AuthenticationState;
User = await UserManager.GetUserAsync(state.User); User = await UserManager.GetUserAsync(state.User);
await LoadAsync(CurrentRole);
Authors = await UserManager.GetUsersInRoleAsync("Author");
Reviewers = await UserManager.GetUsersInRoleAsync("Reviewer");
Moderators = await UserManager.GetUsersInRoleAsync("Moderator");
Admins = await UserManager.GetUsersInRoleAsync("Admin");
} }
private async Task Remove(ApplicationUser user, string role) { private async Task LoadAsync(Role role) {
try {
Busy = true;
var users = await UserManager.GetUsersInRoleAsync(role.ToString());
Users.Clear();
CurrentRole = role;
Users.AddRange(users);
} finally {
Busy = false;
}
await InvokeAsync(StateHasChanged);
}
private async Task RemoveAsync(ApplicationUser user, Role role) {
if (User is null) return; if (User is null) return;
if (user.Id == User.Id) { if (user.Id == User.Id) {
Toast.ShowError(Localizer["Error_EditSelf"]); Toast.ShowError(Localizer["Error_EditSelf"]);
return; return;
} }
await UserManager.RemoveFromRoleAsync(user, role); await UserManager.RemoveFromRoleAsync(user, role.ToString());
}
private enum Role {
Author, Reviewer, Moderator, Admin
} }
} }

View file

@ -110,7 +110,7 @@
<data name="Moderators" xml:space="preserve"> <data name="Moderators" xml:space="preserve">
<value>Moderator*innen</value> <value>Moderator*innen</value>
</data> </data>
<data name="Administrators" xml:space="preserve"> <data name="Admins" xml:space="preserve">
<value>Administrator*innen</value> <value>Administrator*innen</value>
</data> </data>
<data name="Error_EditSelf" xml:space="preserve"> <data name="Error_EditSelf" xml:space="preserve">

View file

@ -110,7 +110,7 @@
<data name="Moderators" xml:space="preserve"> <data name="Moderators" xml:space="preserve">
<value>Moderators</value> <value>Moderators</value>
</data> </data>
<data name="Administrators" xml:space="preserve"> <data name="Admins" xml:space="preserve">
<value>Administrators</value> <value>Administrators</value>
</data> </data>
<data name="Error_EditSelf" xml:space="preserve"> <data name="Error_EditSelf" xml:space="preserve">

File diff suppressed because one or more lines are too long