Files
Servers/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AllocationsNodeUpdate.razor

148 lines
5.7 KiB
Plaintext

@using MoonCore.Blazor.Tailwind.Alerts
@using MoonlightServers.Shared.Http.Responses.Admin.Nodes
@using MoonCore.Blazor.Tailwind.Dt
@using MoonCore.Blazor.Tailwind.Modals
@using MoonCore.Blazor.Tailwind.Toasts
@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 card-header bg-gray-700 rounded-t-lg text-gray-500 bg-opacity-50 border-0 py-2 text-base font-semibold">
<span class="card-title">Actions</span>
</div>
<div class="card-body">
<div class="flex flex-col gap-y-3">
<button @onclick="AddAllocation" class="btn btn-primary">Create</button>
<button @onclick="AddAllocationRange" class="btn btn-tertiary">Create multiple</button>
<button @onclick="DeleteAllAllocations" class="btn btn-danger">Delete all</button>
</div>
</div>
</div>
</div>
<div class="col-span-1 md:col-span-2 -mb-3">
<DataTable @ref="Table" TItem="NodeAllocationDetailResponse">
<Configuration>
<Pagination TItem="NodeAllocationDetailResponse" ItemSource="LoadData" />
<DataTableColumn TItem="NodeAllocationDetailResponse" Field="@(x => x.IpAddress)" Name="IP Address"/>
<DataTableColumn TItem="NodeAllocationDetailResponse" Field="@(x => x.Port)" Name="Port"/>
<DataTableColumn TItem="NodeAllocationDetailResponse">
<ColumnTemplate>
<div class="flex justify-end items-center">
<a @onclick="() => UpdateAllocation(context)" @onclick:preventDefault href="#"
class="text-primary-500 mr-2 sm:mr-3">
<i class="icon-pencil text-base"></i>
</a>
<a @onclick="() => DeleteAllocation(context)" @onclick:preventDefault href="#"
class="text-danger-500">
<i class="icon-trash text-base"></i>
</a>
</div>
</ColumnTemplate>
</DataTableColumn>
</Configuration>
</DataTable>
</div>
</div>
@code
{
[Parameter] public NodeDetailResponse Node { get; set; }
private DataTable<NodeAllocationDetailResponse> Table;
private async Task<IPagedData<NodeAllocationDetailResponse>> LoadData(PaginationOptions options)
{
return await ApiClient.GetJson<PagedData<NodeAllocationDetailResponse>>(
$"api/admin/servers/nodes/{Node.Id}/allocations?page={options.Page}&pageSize={options.PerPage}"
);
}
private async Task AddAllocationRange()
{
Func<CreateNodeAllocationRangeRequest, Task> onSubmit = async request =>
{
await ApiClient.Post($"api/admin/servers/nodes/{Node.Id}/allocations/range", request);
await ToastService.Success("Successfully created allocations");
await Table.Refresh();
};
await ModalService.Launch<CreateMultipleAllocationModal>(parameters =>
{
parameters.Add("OnSubmit", onSubmit);
});
}
private async Task AddAllocation()
{
Func<CreateNodeAllocationRequest, Task> onSubmit = async request =>
{
await ApiClient.Post($"api/admin/servers/nodes/{Node.Id}/allocations", request);
await ToastService.Success("Successfully created allocation");
await Table.Refresh();
};
await ModalService.Launch<CreateAllocationModal>(parameters => { parameters.Add("OnSubmit", onSubmit); });
}
private async Task UpdateAllocation(NodeAllocationDetailResponse allocation)
{
Func<UpdateNodeAllocationRequest, Task> onSubmit = async request =>
{
await ApiClient.Patch($"api/admin/servers/nodes/{Node.Id}/allocations/{allocation.Id}", request);
await ToastService.Success("Successfully updated allocation");
await Table.Refresh();
};
await ModalService.Launch<UpdateAllocationModal>(parameters =>
{
parameters.Add("OnSubmit", onSubmit);
parameters.Add("Allocation", allocation);
});
}
private async Task DeleteAllocation(NodeAllocationDetailResponse allocation)
{
await AlertService.ConfirmDanger(
"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.Success("Successfully deleted allocation");
await Table.Refresh();
}
);
}
private async Task DeleteAllAllocations()
{
await AlertService.ConfirmDanger(
"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.Success("Successfully deleted allocations");
await Table.Refresh();
}
);
}
}