Files
Servers/MoonlightServers.Frontend/UI/Components/Nodes/UpdatePartials/Allocations.razor

157 lines
5.7 KiB
Plaintext

@using MoonCore.Blazor.FlyonUi.Alerts
@using MoonCore.Blazor.FlyonUi.Grid
@using MoonCore.Blazor.FlyonUi.Grid.Columns
@using MoonCore.Blazor.FlyonUi.Modals
@using MoonCore.Blazor.FlyonUi.Toasts
@using MoonlightServers.Shared.Http.Responses.Admin.Nodes
@using MoonCore.Helpers
@using MoonCore.Models
@using MoonlightServers.Frontend.UI.Components.Nodes.Modals
@using MoonlightServers.Shared.Http.Requests.Admin.NodeAllocations
@using MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations
@inject HttpApiClient ApiClient
@inject ModalService ModalService
@inject ToastService ToastService
@inject AlertService AlertService
<div class="grid grid-cols-1 md:grid-cols-3 md:gap-x-5">
<div class="col-span-1">
<div class="card">
<div class="card-header">
<span class="card-title">Actions</span>
</div>
<div class="card-body">
<div class="flex flex-col gap-y-3">
<button type="button" @onclick="AddAllocationAsync" class="btn btn-primary">Create</button>
<button type="button" @onclick="AddAllocationRangeAsync" class="btn btn-accent">Create multiple
</button>
<button type="button" @onclick="DeleteAllAllocationsAsync" class="btn btn-error">Delete all</button>
</div>
</div>
</div>
</div>
<div class="col-span-1 md:col-span-2 -mb-3">
<DataGrid @ref="Grid"
TGridItem="NodeAllocationResponse"
EnablePagination="true"
ItemsProvider="ItemsProviderAsync">
<PropertyColumn Field="x => x.IpAddress" Title="IP Address" />
<PropertyColumn Field="x => x.Port"/>
<TemplateColumn>
<td>
<div class="flex justify-end items-center">
<a @onclick="() => UpdateAllocationAsync(context)"
@onclick:preventDefault href="#"
class="text-primary mr-2 sm:mr-3">
<i class="icon-pencil text-base"></i>
</a>
<a @onclick="() => DeleteAllocationAsync(context)"
@onclick:preventDefault href="#"
class="text-error">
<i class="icon-trash text-base"></i>
</a>
</div>
</td>
</TemplateColumn>
</DataGrid>
</div>
</div>
@code
{
[Parameter] public NodeResponse Node { get; set; }
private DataGrid<NodeAllocationResponse> Grid;
private async Task<DataGridItemResult<NodeAllocationResponse>> ItemsProviderAsync(
DataGridItemRequest request
)
{
var query = $"?startIndex={request.StartIndex}&count={request.Count}";
var countedData = await ApiClient.GetJson<CountedData<NodeAllocationResponse>>(
$"api/admin/servers/nodes/{Node.Id}/allocations{query}"
);
return new()
{
TotalCount = countedData.TotalCount,
Items = countedData.Items
};
}
private async Task AddAllocationRangeAsync()
{
Func<CreateNodeAllocationRangeRequest, Task> onSubmit = async request =>
{
await ApiClient.Post($"api/admin/servers/nodes/{Node.Id}/allocations/range", request);
await ToastService.SuccessAsync("Successfully created allocations");
await Grid.RefreshAsync();
};
await ModalService.LaunchAsync<CreateMultipleAllocationModal>(parameters => { parameters.Add("OnSubmit", onSubmit); });
}
private async Task AddAllocationAsync()
{
Func<CreateNodeAllocationRequest, Task> onSubmit = async request =>
{
await ApiClient.Post($"api/admin/servers/nodes/{Node.Id}/allocations", request);
await ToastService.SuccessAsync("Successfully created allocation");
await Grid.RefreshAsync();
};
await ModalService.LaunchAsync<CreateAllocationModal>(parameters => { parameters.Add("OnSubmit", onSubmit); });
}
private async Task UpdateAllocationAsync(NodeAllocationResponse allocation)
{
Func<UpdateNodeAllocationRequest, Task> onSubmit = async request =>
{
await ApiClient.Patch($"api/admin/servers/nodes/{Node.Id}/allocations/{allocation.Id}", request);
await ToastService.SuccessAsync("Successfully updated allocation");
await Grid.RefreshAsync();
};
await ModalService.LaunchAsync<UpdateAllocationModal>(parameters =>
{
parameters.Add("OnSubmit", onSubmit);
parameters.Add("Allocation", allocation);
});
}
private async Task DeleteAllocationAsync(NodeAllocationResponse allocation)
{
await AlertService.ConfirmDangerAsync(
"Delete allocation",
"Do you really want to delete the selected allocation? This cannot be undone",
async () =>
{
await ApiClient.Delete($"api/admin/servers/nodes/{Node.Id}/allocations/{allocation.Id}");
await ToastService.SuccessAsync("Successfully deleted allocation");
await Grid.RefreshAsync();
}
);
}
private async Task DeleteAllAllocationsAsync()
{
await AlertService.ConfirmDangerAsync(
"Delete all allocations",
"Do you really want to delete all allocations? This cannot be undone",
async () =>
{
await ApiClient.Delete($"api/admin/servers/nodes/{Node.Id}/allocations/all");
await ToastService.SuccessAsync("Successfully deleted allocations");
await Grid.RefreshAsync();
}
);
}
}