parent
dc96cc2029
commit
0c06b19e6e
@ -1,32 +1,66 @@
|
||||
@page "/users"
|
||||
|
||||
|
||||
@using AdminPanel.Components;
|
||||
@using AdminPanel.Models;
|
||||
@using AdminPanel.Models
|
||||
|
||||
<PageTitle>User Panel</PageTitle>
|
||||
|
||||
<h1>User Panel</h1>
|
||||
|
||||
<div class="users-div">
|
||||
@if (Users == null)
|
||||
{
|
||||
<p>Fetching Data...</p>
|
||||
}
|
||||
else
|
||||
foreach (User user in Users)
|
||||
{
|
||||
<div class="user-cell">
|
||||
<UserComponent User=@user/>
|
||||
<Leaflet>
|
||||
<Header>
|
||||
see more
|
||||
</Header>
|
||||
<div id="users-div">
|
||||
|
||||
<MudOverlay Visible="IsAddingUser" DarkBackground>
|
||||
<MudForm id="add-account-form">
|
||||
<MudTextField T="string" Label="Name" Required @bind-Value="FormAccountName"/>
|
||||
<MudTextField T="string" Label="Email" Required @bind-Value="FormAccountEmail"/>
|
||||
<MudTextField T="string" InputType="InputType.Password" Label="Password" Required @bind-Value="FormAccountPassword"/>
|
||||
<MudCheckBox T="bool" Label="Is Administrator" @bind-Value="FormAccountIsAdmin"/>
|
||||
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Secondary" OnClick="@(() => IsAddingUser = false)">Cancel</MudButton>
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="ConfirmAddAccount">Add Account</MudButton>
|
||||
</MudForm>
|
||||
</MudOverlay>
|
||||
|
||||
<div style="display: flex; align-items: center">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Add" OnClick="@(() => IsAddingUser = true)" Color="@Color.Success" Class="add-item-btn"/>
|
||||
<MudText Align="Align.Center">Add Account</MudText>
|
||||
</div>
|
||||
<div style="display: flex; align-items: center">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Remove" OnClick="RemoveSelection" Color="@Color.Error" Class="remove-item-btn"/>
|
||||
<MudText Align="Align.Center">Remove Selection</MudText>
|
||||
</div>
|
||||
|
||||
|
||||
<Body>
|
||||
<p>Ratio</p>
|
||||
</Body>
|
||||
</Leaflet>
|
||||
</div>
|
||||
}
|
||||
<MudDataGrid
|
||||
T="User"
|
||||
MultiSelection="true"
|
||||
ServerData="LoadUsersFromServer"
|
||||
ColumnResizeMode="ResizeMode.Column"
|
||||
RowsPerPage="1"
|
||||
ReadOnly="false"
|
||||
EditMode="DataGridEditMode.Form"
|
||||
@bind-SelectedItems="SelectedItems"
|
||||
CommittedItemChanges="OnAccountEdited">
|
||||
<Columns>
|
||||
<SelectColumn T="User"/>
|
||||
<PropertyColumn Property="x => x.Name" Required/>
|
||||
<PropertyColumn Property="x => x.Email" Title="Email Address" Required/>
|
||||
<TemplateColumn Title="Is Administrator">
|
||||
<CellTemplate>
|
||||
<MudCheckBox Value="@context.Item!.IsAdmin" ReadOnly/>
|
||||
</CellTemplate>
|
||||
<EditTemplate>
|
||||
<MudText>Is Administrator :</MudText>
|
||||
<MudCheckBox @bind-Value="context.Item.IsAdmin"/>
|
||||
</EditTemplate>
|
||||
</TemplateColumn>
|
||||
<PropertyColumn Property="x => x.Id" IsEditable="false"/>
|
||||
<TemplateColumn>
|
||||
<CellTemplate>
|
||||
<MudIconButton Size="Size.Small" Icon="@Icons.Material.Outlined.Edit" OnClick="@context.Actions.StartEditingItemAsync"/>
|
||||
</CellTemplate>
|
||||
</TemplateColumn>
|
||||
</Columns>
|
||||
<PagerContent>
|
||||
<MudDataGridPager T="User" PageSizeOptions="new[] { 1, 2, 4, 10, 25, 50, 100 }"/>
|
||||
</PagerContent>
|
||||
</MudDataGrid>
|
||||
</div>
|
@ -0,0 +1,8 @@
|
||||
|
||||
|
||||
#add-account-form {
|
||||
background-color: white;
|
||||
visibility: hidden;
|
||||
color: #ffba00;
|
||||
}
|
||||
|
@ -1,11 +1,26 @@
|
||||
using AdminPanel;
|
||||
using AdminPanel.Services;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using MudBlazor.Services;
|
||||
|
||||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
||||
builder.RootComponents.Add<App>("#app");
|
||||
builder.RootComponents.Add<HeadOutlet>("head::after");
|
||||
|
||||
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
|
||||
builder.Services
|
||||
.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
var client = new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri("http://localhost:8080")
|
||||
|
||||
};
|
||||
|
||||
builder.Services.AddScoped<IUsersService>(sp => new HttpUsersService(client));
|
||||
//builder.Services.AddScoped<IUsersService, UsersServiceStub>();
|
||||
|
||||
builder.Services.AddMudServices();
|
||||
|
||||
|
||||
await builder.Build().RunAsync();
|
@ -0,0 +1,64 @@
|
||||
using System.Net.Http.Json;
|
||||
using AdminPanel.Models;
|
||||
|
||||
namespace AdminPanel.Services;
|
||||
|
||||
public class HttpUsersService : IUsersService
|
||||
{
|
||||
private HttpClient Client { get; }
|
||||
|
||||
public HttpUsersService(HttpClient client)
|
||||
{
|
||||
this.Client = client;
|
||||
}
|
||||
|
||||
private record ListUsersResponse(List<User> Users, uint TotalCount);
|
||||
|
||||
public async Task<(uint, List<User>)> ListUsers(uint from, uint len)
|
||||
{
|
||||
var httpResponse = await Client.GetAsync($"/api/admin/list-users?start={from}&n={len}");
|
||||
httpResponse.EnsureSuccessStatusCode();
|
||||
|
||||
var response = await httpResponse.Content.ReadFromJsonAsync<ListUsersResponse>()
|
||||
?? throw new Exception("Received non-parseable json from server");
|
||||
|
||||
return (response.TotalCount, response.Users);
|
||||
}
|
||||
|
||||
private record AddUserRequest(string Username, string Email, string Password, bool IsAdmin);
|
||||
|
||||
private record AddUserResponse(uint Id);
|
||||
|
||||
public async Task<User> AddUser(string username, string email, string password, bool isAdmin)
|
||||
{
|
||||
var httpResponse = await Client.PostAsJsonAsync("/api/admin/user/add", new AddUserRequest(username, email, password, isAdmin));
|
||||
httpResponse.EnsureSuccessStatusCode();
|
||||
|
||||
var response = await httpResponse.Content.ReadFromJsonAsync<AddUserResponse>()
|
||||
?? throw new Exception("Received non-parseable json from server");
|
||||
|
||||
return new User
|
||||
{
|
||||
Name = username,
|
||||
Email = email,
|
||||
IsAdmin = isAdmin,
|
||||
Id = response.Id
|
||||
};
|
||||
}
|
||||
|
||||
private record RemoveUsersRequest(uint[] Identifiers);
|
||||
|
||||
public async Task RemoveUsers(IEnumerable<uint> userIds)
|
||||
{
|
||||
var httpResponse = await Client.PostAsJsonAsync("/api/admin/user/remove-all", new RemoveUsersRequest(userIds.ToArray()));
|
||||
httpResponse.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
private record UpdateUserRequest(string Email, string Username, bool IsAdmin);
|
||||
|
||||
public async Task UpdateUser(User user)
|
||||
{
|
||||
var httpResponse = await Client.PostAsJsonAsync($"/api/admin/user/{user.Id}/update", new UpdateUserRequest(user.Email, user.Name, user.IsAdmin));
|
||||
httpResponse.EnsureSuccessStatusCode();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue