Files
Moonlight/Moonlight.Client/UI/Views/Admin/Users/Index.razor

80 lines
2.4 KiB
Plaintext

@page "/admin/users"
@using MoonCore.Blazor.FlyonUi.Common
@using MoonCore.Helpers
@using Moonlight.Shared.Http.Responses.Admin.Users
@using MoonCore.Blazor.FlyonUi.Grid
@using MoonCore.Blazor.FlyonUi.Grid.Columns
@using MoonCore.Common
@inject HttpApiClient ApiClient
@inject AlertService AlertService
@inject ToastService ToastService
<div class="mb-8">
<PageHeader Title="Users">
<a href="/admin/users/create" class="btn btn-primary">
Create
</a>
</PageHeader>
</div>
<DataGrid @ref="Grid"
ItemSource="ItemSource">
<PropertyColumn Field="x => x.Id" Sortable="true"/>
<PropertyColumn Field="x => x.Username" Sortable="true"/>
<PropertyColumn Field="x => x.Email" Sortable="true"/>
<TemplateColumn>
<td>
<div class="flex justify-end">
<a href="/admin/users/@(context.Id)" class="mr-2 sm:mr-3">
<i class="icon-pencil text-primary"></i>
</a>
<a href="#" @onclick="() => DeleteAsync(context)" @onclick:preventDefault>
<i class="icon-trash text-error"></i>
</a>
</div>
</td>
</TemplateColumn>
</DataGrid>
@code
{
private DataGrid<UserResponse> Grid;
private ItemSource<UserResponse> ItemSource => ItemSourceFactory.From(LoadItemsAsync);
private async Task<IEnumerable<UserResponse>> LoadItemsAsync(
int startIndex, int count, string? filter, SortOption? sortOption
)
{
var query = $"?startIndex={startIndex}&count={count}";
if (sortOption != null)
{
var dir = sortOption.Direction == SortDirection.Descending ? "desc" : "asc";
query += $"&orderBy={sortOption.Column}&orderByDir={dir}";
}
if (!string.IsNullOrEmpty(filter))
query += $"&filter={filter}";
return await ApiClient.GetJson<CountedData<UserResponse>>($"api/admin/users{query}");
}
private async Task DeleteAsync(UserResponse response)
{
await AlertService.ConfirmDangerAsync(
"User deletion",
$"Do you really want to delete the user '{response.Username}'",
async () =>
{
await ApiClient.Delete($"api/admin/users/{response.Id}");
await ToastService.SuccessAsync("Successfully deleted user");
await Grid.RefreshAsync();
}
);
}
}