Files
Servers/MoonlightServers.Frontend/Admin/Nodes/Edit.razor

154 lines
5.0 KiB
Plaintext

@page "/admin/servers/nodes/{Id:int}"
@using System.Net
@using LucideBlazor
@using Moonlight.Frontend.Helpers
@using MoonlightServers.Shared
@using MoonlightServers.Shared.Admin.Nodes
@using ShadcnBlazor.Buttons
@using ShadcnBlazor.Cards
@using ShadcnBlazor.Emptys
@using ShadcnBlazor.Extras.Common
@using ShadcnBlazor.Extras.Forms
@using ShadcnBlazor.Extras.Toasts
@using ShadcnBlazor.Fields
@using ShadcnBlazor.Inputs
@inject HttpClient HttpClient
@inject NavigationManager Navigation
@inject ToastService ToastService
<LazyLoader Load="LoadAsync">
@if (Node == null)
{
<Empty>
<EmptyHeader>
<EmptyMedia Variant="EmptyMediaVariant.Icon">
<SearchIcon/>
</EmptyMedia>
<EmptyTitle>Node not found</EmptyTitle>
<EmptyDescription>
A node with this id cannot be found
</EmptyDescription>
</EmptyHeader>
</Empty>
}
else
{
<EnhancedEditForm Model="Request" OnValidSubmit="OnSubmitAsync" Context="formContext">
<div class="flex flex-row justify-between">
<div class="flex flex-col">
<h1 class="text-xl font-semibold">Update Node</h1>
<div class="text-muted-foreground">
Update node @Node.Name
</div>
</div>
<div class="flex flex-row gap-x-1.5">
<Button Variant="ButtonVariant.Secondary">
<Slot>
<a href="/admin/servers?tab=nodes" @attributes="context">
<ChevronLeftIcon/>
Back
</a>
</Slot>
</Button>
<SubmitButton>
<CheckIcon/>
Continue
</SubmitButton>
</div>
</div>
<div class="mt-8">
<Card>
<CardContent>
<DataAnnotationsValidator/>
<FieldGroup>
<FormValidationSummary/>
<FieldSet>
<FieldGroup>
<div class="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-8">
<Field>
<FieldLabel for="nodeName">Name</FieldLabel>
<TextInputField
@bind-Value="Request.Name"
id="nodeName"/>
</Field>
<Field>
<FieldLabel for="nodeHttpEndpoint">HTTP Endpoint</FieldLabel>
<TextInputField
@bind-Value="Request.HttpEndpointUrl"
id="nodeHttpEndpoint"
placeholder="http://example.com:8080"/>
</Field>
</div>
</FieldGroup>
</FieldSet>
</FieldGroup>
</CardContent>
</Card>
</div>
</EnhancedEditForm>
}
</LazyLoader>
@code
{
[Parameter] public int Id { get; set; }
private UpdateNodeDto Request;
private NodeDto? Node;
private async Task LoadAsync(LazyLoader _)
{
var response = await HttpClient.GetAsync($"api/admin/servers/nodes/{Id}");
if (!response.IsSuccessStatusCode)
{
if(response.StatusCode == HttpStatusCode.NotFound)
return;
response.EnsureSuccessStatusCode();
return;
}
Node = await response.Content.ReadFromJsonAsync<NodeDto>(SerializationContext.Default.Options);
if(Node == null)
return;
Request = new UpdateNodeDto()
{
Name = Node.Name,
HttpEndpointUrl = Node.HttpEndpointUrl
};
}
private async Task<bool> OnSubmitAsync(EditContext context, ValidationMessageStore validationMessageStore)
{
var response = await HttpClient.PutAsJsonAsync(
$"/api/admin/servers/nodes/{Id}",
Request,
SerializationContext.Default.Options
);
if (!response.IsSuccessStatusCode)
{
await ProblemDetailsHelper.HandleProblemDetailsAsync(response, Request, validationMessageStore);
return false;
}
await ToastService.SuccessAsync(
"Node Update",
$"Successfully updated node {Request.Name}"
);
Navigation.NavigateTo("/admin/servers?tab=nodes");
return true;
}
}