147 lines
5.3 KiB
Plaintext
147 lines
5.3 KiB
Plaintext
@using LucideBlazor
|
|
@using Moonlight.Frontend.Helpers
|
|
@using MoonlightServers.Shared
|
|
@using MoonlightServers.Shared.Admin.Templates
|
|
@using ShadcnBlazor.Accordions
|
|
@using ShadcnBlazor.Buttons
|
|
@using ShadcnBlazor.Extras.AlertDialogs
|
|
@using ShadcnBlazor.Extras.Forms
|
|
@using ShadcnBlazor.Extras.Toasts
|
|
@using ShadcnBlazor.Fields
|
|
@using ShadcnBlazor.Inputs
|
|
|
|
@inject HttpClient HttpClient
|
|
@inject ToastService ToastService
|
|
@inject AlertDialogService AlertDialogService
|
|
|
|
<Accordion ClassName="-space-y-px" Type="AccordionType.Single">
|
|
<AccordionItem
|
|
ClassName="overflow-hidden border bg-card px-4 first:rounded-t-lg last:rounded-b-lg last:border-b"
|
|
Value="element">
|
|
<AccordionTrigger ClassName="hover:no-underline">
|
|
<div class="flex items-center gap-3">
|
|
<VariableIcon/>
|
|
<span class="text-left">@Request.DisplayName</span>
|
|
</div>
|
|
</AccordionTrigger>
|
|
<AccordionContent ClassName="ps-7">
|
|
<EnhancedEditForm Model="Request" OnValidSubmit="OnValidSubmitAsync">
|
|
<FieldGroup>
|
|
<DataAnnotationsValidator/>
|
|
<FormValidationSummary/>
|
|
|
|
<FieldSet>
|
|
<Field>
|
|
@{
|
|
var id = $"variableDisplayName{Variable.Id}";
|
|
}
|
|
<FieldLabel for="@id">Display Name</FieldLabel>
|
|
<TextInputField
|
|
@bind-Value="Request.DisplayName"
|
|
id="@id"/>
|
|
</Field>
|
|
|
|
<Field>
|
|
@{
|
|
var id = $"variableDescription{Variable.Id}";
|
|
}
|
|
<FieldLabel for="@id">Description</FieldLabel>
|
|
<TextareaInputField
|
|
@bind-Value="Request.Description"
|
|
id="@id"/>
|
|
</Field>
|
|
|
|
<Field>
|
|
@{
|
|
var id = $"variableKey{Variable.Id}";
|
|
}
|
|
<FieldLabel for="@id">Key</FieldLabel>
|
|
<TextInputField
|
|
@bind-Value="Request.EnvName"
|
|
id="@id"/>
|
|
</Field>
|
|
|
|
<Field>
|
|
@{
|
|
var id = $"variableDefaultValue{Variable.Id}";
|
|
}
|
|
<FieldLabel for="@id">Default Value</FieldLabel>
|
|
<TextInputField
|
|
@bind-Value="Request.DefaultValue"
|
|
id="@id"/>
|
|
</Field>
|
|
</FieldSet>
|
|
<Field Orientation="FieldOrientation.Horizontal" ClassName="justify-end">
|
|
<Button Variant="ButtonVariant.Destructive" @onclick="DeleteAsync">Delete</Button>
|
|
<SubmitButton>Save changes</SubmitButton>
|
|
</Field>
|
|
</FieldGroup>
|
|
</EnhancedEditForm>
|
|
</AccordionContent>
|
|
</AccordionItem>
|
|
</Accordion>
|
|
|
|
@code
|
|
{
|
|
[Parameter] public VariableDto Variable { get; set; }
|
|
[Parameter] public DetailedTemplateDto Template { get; set; }
|
|
[Parameter] public Func<Task> OnChanged { get; set; }
|
|
|
|
private UpdateVariableDto Request;
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
Request = new UpdateVariableDto()
|
|
{
|
|
DisplayName = Variable.DisplayName,
|
|
DefaultValue = Variable.DefaultValue,
|
|
EnvName = Variable.EnvName,
|
|
Description = Variable.Description
|
|
};
|
|
}
|
|
|
|
private async Task<bool> OnValidSubmitAsync(EditContext editContext, ValidationMessageStore validationMessageStore)
|
|
{
|
|
var response = await HttpClient.PutAsJsonAsync(
|
|
$"api/admin/servers/templates/{Template.Id}/variables/{Variable.Id}",
|
|
Request,
|
|
SerializationContext.Default.Options
|
|
);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
await ProblemDetailsHelper.HandleProblemDetailsAsync(response, Request, validationMessageStore);
|
|
return false;
|
|
}
|
|
|
|
await ToastService.SuccessAsync(
|
|
"Variable Update",
|
|
$"Successfully updated variable {Request.DisplayName}"
|
|
);
|
|
|
|
await OnChanged.Invoke();
|
|
|
|
return true;
|
|
}
|
|
|
|
private async Task DeleteAsync()
|
|
{
|
|
await AlertDialogService.ConfirmDangerAsync(
|
|
"Variable Deletion",
|
|
$"Do you really want to delete the variable {Variable.DisplayName}? This cannot be undone",
|
|
async () =>
|
|
{
|
|
var response = await HttpClient.DeleteAsync($"api/admin/servers/templates/{Template.Id}/variables/{Variable.Id}");
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
await ToastService.SuccessAsync(
|
|
"Variable Deletion",
|
|
$"Variable {Variable.DisplayName} successfully deleted"
|
|
);
|
|
|
|
await OnChanged.Invoke();
|
|
}
|
|
);
|
|
}
|
|
} |