100 lines
3.1 KiB
Plaintext
100 lines
3.1 KiB
Plaintext
@using Moonlight.Frontend.Helpers
|
|
@using Moonlight.Frontend.UI.Admin.Components
|
|
@using Moonlight.Shared.Http
|
|
@using Moonlight.Shared.Http.Requests.Admin.ApiKeys
|
|
@using Moonlight.Shared.Http.Responses
|
|
@using ShadcnBlazor.Dialogs
|
|
@using ShadcnBlazor.Extras.Forms
|
|
@using ShadcnBlazor.Extras.Toasts
|
|
@using ShadcnBlazor.Fields
|
|
@using ShadcnBlazor.Inputs
|
|
|
|
@inherits ShadcnBlazor.Extras.Dialogs.DialogBase
|
|
|
|
@inject HttpClient HttpClient
|
|
@inject ToastService ToastService
|
|
|
|
<DialogHeader>
|
|
<DialogTitle>Create new API key</DialogTitle>
|
|
<DialogDescription>
|
|
Define a name, description, and select the permissions that the key should have.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<EnhancedEditForm Model="Request" OnValidSubmit="OnSubmitAsync">
|
|
|
|
<FieldGroup>
|
|
<DataAnnotationsValidator/>
|
|
<FormValidationSummary/>
|
|
|
|
<FieldSet>
|
|
<Field>
|
|
<FieldLabel for="keyName">Name</FieldLabel>
|
|
<TextInputField @bind-Value="Request.Name" id="keyName" placeholder="My API key"/>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel for="keyDescription">Description</FieldLabel>
|
|
<TextareaInputField @bind-Value="Request.Description" id="keyDescription" placeholder="What this key is for"/>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel for="keyValidUntil">Valid until</FieldLabel>
|
|
<DateTimeInputField @bind-Value="Request.ValidUntil" id="keyValidUntil" />
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel>Permissions</FieldLabel>
|
|
<FieldContent>
|
|
<PermissionSelector Permissions="Permissions"/>
|
|
</FieldContent>
|
|
</Field>
|
|
</FieldSet>
|
|
<Field Orientation="FieldOrientation.Horizontal" ClassName="justify-end">
|
|
<SubmitButton>Save changes</SubmitButton>
|
|
</Field>
|
|
</FieldGroup>
|
|
</EnhancedEditForm>
|
|
|
|
@code
|
|
{
|
|
[Parameter] public Func<Task> OnSubmit { get; set; }
|
|
|
|
private CreateApiKeyDto Request;
|
|
|
|
private List<string> Permissions = new();
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
Request = new()
|
|
{
|
|
Permissions = [],
|
|
ValidUntil = DateTimeOffset.UtcNow
|
|
};
|
|
}
|
|
|
|
private async Task<bool> OnSubmitAsync(EditContext editContext, ValidationMessageStore validationMessageStore)
|
|
{
|
|
Request.Permissions = Permissions.ToArray();
|
|
Request.ValidUntil = Request.ValidUntil.ToUniversalTime();
|
|
|
|
var response = await HttpClient.PostAsJsonAsync(
|
|
"/api/admin/apiKeys",
|
|
Request,
|
|
SerializationContext.Default.Options
|
|
);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
await ProblemDetailsHelper.HandleProblemDetailsAsync(response, Request, validationMessageStore);
|
|
return false;
|
|
}
|
|
|
|
await ToastService.SuccessAsync(
|
|
"API Key creation",
|
|
$"Successfully created API key {Request.Name}"
|
|
);
|
|
|
|
await OnSubmit.Invoke();
|
|
|
|
await CloseAsync();
|
|
return true;
|
|
}
|
|
} |