Skip to content

Commit

Permalink
Add new user edit page
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahStolk committed Mar 6, 2024
1 parent 8725659 commit cd66a21
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 129 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
@page "/admin/spawnsets/edit/{Id:int}"
@inherits BaseAdminPage
@using DevilDaggersInfo.Web.ApiSpec.Admin.Players
@using DevilDaggersInfo.Web.ApiSpec.Admin.Spawnsets
@using DevilDaggersInfo.Web.Client.Components.Admin
@using DevilDaggersInfo.Web.Client.Components.Input
@using DevilDaggersInfo.Web.Client.Enums
@using DevilDaggersInfo.Web.Client.StateObjects.Admin.Spawnsets
@using DevilDaggersInfo.Web.Core.Claims
@using DevilDaggersInfo.Web.ApiSpec.Admin.Players
@using DevilDaggersInfo.Web.ApiSpec.Admin.Spawnsets

<AdminAuthorizer Base="this" RequiredRole="@Roles.Spawnsets">
<AdminEdit TStateObject="EditSpawnsetState" TModel="EditSpawnset" Name="Spawnset" OverviewUrl="/admin/spawnsets" ApiCall="Http.EditSpawnsetById" StateObject="_editSpawnsetState" Id="Id" OnPopulate="async ec => await PopulateAsync(ec)">
Expand Down

This file was deleted.

164 changes: 164 additions & 0 deletions src/DevilDaggersInfo.Web.Client/Pages/Admin/Users/EditPage.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
@page "/admin/users/edit/{Id:int}"
@using DevilDaggersInfo.Web.ApiSpec.Admin.Players
@using DevilDaggersInfo.Web.ApiSpec.Admin.Users
@using DevilDaggersInfo.Web.Client.Components
@using DevilDaggersInfo.Web.Client.Components.Admin
@using DevilDaggersInfo.Web.Client.Components.Input
@using DevilDaggersInfo.Web.Client.StateObjects.Admin.Users
@using DevilDaggersInfo.Web.Core.Claims
@using System.Security.Cryptography
@inherits BaseAdminPage

<AdminAuthorizer Base="this" RequiredRole="@Roles.Players">
<Heading Title="Admin - Edit User" />

<Heading Title="Assign Player" Level="2" />

<EditForm Model="@AssignPlayerState" OnValidSubmit="@AssignPlayerAsync">
<DataAnnotationsValidator />
<ValidationSummary />

<InputWrapper Label="Player"><SearchDropdown TKey="int" Values="@_playerNames" Converter="s => int.Parse(s)" @bind-Value="@AssignPlayerState.PlayerId" /></InputWrapper>

<button class="btn btn-gray p-1" type="submit">Assign player</button>
</EditForm>

<Heading Title="Reset Password" Level="2" />

<button class="btn btn-gray p-1" @onclick="() => ResetPasswordState.NewPassword = GeneratePassword()">Generate random</button>

<EditForm Model="@ResetPasswordState" OnValidSubmit="@ResetPasswordAsync">
<DataAnnotationsValidator />
<ValidationSummary />

<div class="grid gap-2 grid-cols-2 max-w-md">
<span>Password</span>
<input class="bg-black border-gray-3 border-2" @bind="@ResetPasswordState.NewPassword" />
</div>

<button class="btn btn-gray p-1" type="submit">Reset password</button>
</EditForm>

<Heading Title="Roles" Level="2" />

@if (_getUser == null)
{
<Loading />
}
else
{
List<(string RoleName, bool IsEnabled)> data =
[
(Roles.Admin, _getUser.IsAdmin),
(Roles.CustomLeaderboards, _getUser.IsCustomLeaderboardsMaintainer),
(Roles.Mods, _getUser.IsModsMaintainer),
(Roles.Players, _getUser.IsPlayersMaintainer),
(Roles.Spawnsets, _getUser.IsSpawnsetsMaintainer),
];
foreach ((string roleName, bool isEnabled) in data)
{
<div>
<button @onclick="@(() => ToggleRoleAsync(roleName))" class="btn @(GetRoleButtonColor(isEnabled)) p-1">@(GetRoleButtonText(isEnabled)) @(roleName)</button>
</div>
}
}
</AdminAuthorizer>

@code
{
private Dictionary<int, string>? _playerNames;
private GetUser? _getUser;

[Parameter]
[EditorRequired]
public int Id { get; set; }

[Parameter]
public AssignPlayerState AssignPlayerState { get; set; } = new();

[Parameter]
public ResetPasswordState ResetPasswordState { get; set; } = new();

private static string GetRoleButtonText(bool isEnabled)
{
return isEnabled ? "Disable" : "Enable";
}

private static string GetRoleButtonColor(bool isEnabled)
{
return isEnabled ? "btn-red" : "btn-green";
}

private static string GeneratePassword()
{
// ReSharper disable StringLiteralTypo
return RandomNumberGenerator.GetString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 20);
// ReSharper restore StringLiteralTypo
}

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

try
{
List<GetPlayerName> getPlayerNames = await Http.GetPlayerNames();
_playerNames = getPlayerNames.ToDictionary(m => m.Id, m => m.PlayerName);

_getUser = await Http.GetUserById(Id);
AssignPlayerState.PlayerId = _getUser.PlayerId ?? 0;
}
catch (AccessTokenNotAvailableException exception)
{
exception.Redirect();
}
}

private async Task AssignPlayerAsync()
{
try
{
HttpResponseMessage hrm = await Http.AssignPlayer(Id, AssignPlayerState.ToModel());

if (hrm.StatusCode == HttpStatusCode.OK)
NavigationManager.NavigateTo("/admin/users");
}
catch (AccessTokenNotAvailableException exception)
{
exception.Redirect();
}
}

private async Task ResetPasswordAsync()
{
try
{
HttpResponseMessage hrm = await Http.ResetPasswordForUserById(Id, ResetPasswordState.ToModel());

if (hrm.StatusCode == HttpStatusCode.OK)
NavigationManager.NavigateTo("/admin/users");
}
catch (AccessTokenNotAvailableException exception)
{
exception.Redirect();
}
}

private async Task ToggleRoleAsync(string roleName)
{
try
{
HttpResponseMessage hrm = await Http.ToggleRole(Id, new()
{
RoleName = roleName,
});

if (hrm.StatusCode == HttpStatusCode.OK)
NavigationManager.NavigateTo("/admin/users");
}
catch (AccessTokenNotAvailableException exception)
{
exception.Redirect();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ public class AssignPlayerState : IStateObject<AssignPlayer>
{
public int PlayerId { get; set; }

public AssignPlayer ToModel() => new()
public AssignPlayer ToModel()
{
PlayerId = PlayerId,
};
return new()
{
PlayerId = PlayerId,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ public class ResetPasswordState : IStateObject<ResetPassword>
{
public string NewPassword { get; set; } = string.Empty;

public ResetPassword ToModel() => new()
public ResetPassword ToModel()
{
NewPassword = NewPassword,
};
return new()
{
NewPassword = NewPassword,
};
}
}

0 comments on commit cd66a21

Please sign in to comment.