202 lines
8.2 KiB
Plaintext
202 lines
8.2 KiB
Plaintext
@using LucideBlazor
|
|
@using Moonlight.Shared.Http.Requests
|
|
@using Moonlight.Shared.Http.Responses
|
|
@using MoonlightServers.Shared
|
|
@using MoonlightServers.Shared.Admin.Templates
|
|
@using ShadcnBlazor.DataGrids
|
|
@using ShadcnBlazor.Buttons
|
|
@using ShadcnBlazor.Dropdowns
|
|
@using ShadcnBlazor.Extras.AlertDialogs
|
|
@using ShadcnBlazor.Extras.Toasts
|
|
@using ShadcnBlazor.Tabels
|
|
|
|
@inject HttpClient HttpClient
|
|
@inject AlertDialogService AlertDialogService
|
|
@inject ToastService ToastService
|
|
@inject NavigationManager NavigationManager
|
|
@inject IAuthorizationService AuthorizationService
|
|
|
|
<InputFile OnChange="OnFileSelectedAsync" id="import-template" class="hidden" multiple accept=".yml,.yaml,.json"/>
|
|
|
|
<div class="flex flex-row justify-between mt-5">
|
|
<div class="flex flex-col">
|
|
<h1 class="text-xl font-semibold">Templates</h1>
|
|
<div class="text-muted-foreground">
|
|
Manage templates
|
|
</div>
|
|
</div>
|
|
<div class="flex flex-row gap-x-1.5">
|
|
<Button Variant="ButtonVariant.Outline">
|
|
<Slot>
|
|
<label for="import-template" @attributes="context">
|
|
<HardDriveUploadIcon/>
|
|
Import
|
|
</label>
|
|
</Slot>
|
|
</Button>
|
|
<Button>
|
|
<Slot Context="buttonCtx">
|
|
<a @attributes="buttonCtx" href="/admin/servers/templates/create"
|
|
data-disabled="@(!CreateAccess.Succeeded)">
|
|
<PlusIcon/>
|
|
Create
|
|
</a>
|
|
</Slot>
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mt-3">
|
|
<DataGrid @ref="Grid" TGridItem="TemplateDto" Loader="LoadAsync" PageSize="10" ClassName="bg-card">
|
|
<PropertyColumn Field="u => u.Id"/>
|
|
<TemplateColumn IsFilterable="true" Identifier="@nameof(TemplateDto.Name)" Title="@nameof(TemplateDto.Name)">
|
|
<CellTemplate>
|
|
<TableCell>
|
|
<a class="text-primary" href="#"
|
|
@onclick="() => Edit(context)" @onclick:preventDefault>
|
|
@context.Name
|
|
</a>
|
|
</TableCell>
|
|
</CellTemplate>
|
|
</TemplateColumn>
|
|
<TemplateColumn Title="@nameof(TemplateDto.Description)" HeadClassName="hidden lg:table-cell">
|
|
<CellTemplate>
|
|
<TableCell ClassName="hidden lg:table-cell">
|
|
<div class="truncate max-w-md">@context.Description</div>
|
|
</TableCell>
|
|
</CellTemplate>
|
|
</TemplateColumn>
|
|
<PropertyColumn Field="u => u.Author"
|
|
HeadClassName="hidden xl:table-cell"
|
|
CellClassName="hidden xl:table-cell"/>
|
|
<PropertyColumn Field="u => u.Version"
|
|
HeadClassName="hidden xl:table-cell"
|
|
CellClassName="hidden xl:table-cell"/>
|
|
<TemplateColumn>
|
|
<CellTemplate>
|
|
<TableCell>
|
|
<div class="flex flex-row items-center justify-end me-3">
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger>
|
|
<Slot Context="dropdownSlot">
|
|
<Button Size="ButtonSize.IconSm" Variant="ButtonVariant.Ghost"
|
|
@attributes="dropdownSlot">
|
|
<EllipsisIcon/>
|
|
</Button>
|
|
</Slot>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent SideOffset="2">
|
|
<DropdownMenuItem OnClick="() => Export(context)">
|
|
Export
|
|
<DropdownMenuShortcut>
|
|
<HardDriveDownloadIcon/>
|
|
</DropdownMenuShortcut>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem OnClick="() => Edit(context)"
|
|
Disabled="@(!EditAccess.Succeeded)">
|
|
Edit
|
|
<DropdownMenuShortcut>
|
|
<PenIcon/>
|
|
</DropdownMenuShortcut>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem OnClick="() => DeleteAsync(context)"
|
|
Variant="DropdownMenuItemVariant.Destructive"
|
|
Disabled="@(!DeleteAccess.Succeeded)">
|
|
Delete
|
|
<DropdownMenuShortcut>
|
|
<TrashIcon/>
|
|
</DropdownMenuShortcut>
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</div>
|
|
</TableCell>
|
|
</CellTemplate>
|
|
</TemplateColumn>
|
|
</DataGrid>
|
|
</div>
|
|
|
|
@code
|
|
{
|
|
[CascadingParameter] public Task<AuthenticationState> AuthState { get; set; }
|
|
|
|
private DataGrid<TemplateDto> Grid;
|
|
|
|
private AuthorizationResult EditAccess;
|
|
private AuthorizationResult DeleteAccess;
|
|
private AuthorizationResult CreateAccess;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
var authState = await AuthState;
|
|
|
|
EditAccess = await AuthorizationService.AuthorizeAsync(authState.User, Permissions.Templates.Edit);
|
|
DeleteAccess = await AuthorizationService.AuthorizeAsync(authState.User, Permissions.Templates.Delete);
|
|
CreateAccess = await AuthorizationService.AuthorizeAsync(authState.User, Permissions.Templates.Create);
|
|
}
|
|
|
|
private async Task<DataGridResponse<TemplateDto>> LoadAsync(DataGridRequest<TemplateDto> request)
|
|
{
|
|
var query = $"?startIndex={request.StartIndex}&length={request.Length}";
|
|
var filterOptions = request.Filters.Count > 0 ? new FilterOptions(request.Filters) : null;
|
|
|
|
var response = await HttpClient.GetFromJsonAsync<PagedData<TemplateDto>>(
|
|
$"api/admin/servers/templates{query}&filterOptions={filterOptions}",
|
|
SerializationContext.Default.Options
|
|
);
|
|
|
|
return new DataGridResponse<TemplateDto>(response!.Data, response.TotalLength);
|
|
}
|
|
|
|
private void Edit(TemplateDto context) => NavigationManager.NavigateTo($"/admin/servers/templates/{context.Id}");
|
|
|
|
private void Export(TemplateDto dto) => NavigationManager.NavigateTo($"api/admin/servers/templates/{dto.Id}/export", true);
|
|
|
|
private async Task DeleteAsync(TemplateDto context)
|
|
{
|
|
await AlertDialogService.ConfirmDangerAsync(
|
|
"Template Deletion",
|
|
$"Do you really want to delete the template {context.Name}? This cannot be undone.",
|
|
async () =>
|
|
{
|
|
var response = await HttpClient.DeleteAsync($"api/admin/servers/templates/{context.Id}");
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
await Grid.RefreshAsync();
|
|
|
|
await ToastService.SuccessAsync(
|
|
"Template Deletion",
|
|
$"Successfully deleted template {context.Name}"
|
|
);
|
|
|
|
}
|
|
);
|
|
}
|
|
|
|
private async Task OnFileSelectedAsync(InputFileChangeEventArgs eventArgs)
|
|
{
|
|
var files = eventArgs.GetMultipleFiles();
|
|
|
|
foreach (var browserFile in files)
|
|
{
|
|
await using var contentStream = browserFile.OpenReadStream(browserFile.Size);
|
|
|
|
var response = await HttpClient.PostAsync(
|
|
"api/admin/servers/templates/import",
|
|
new StreamContent(contentStream)
|
|
);
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var importedTemplate = await response
|
|
.Content
|
|
.ReadFromJsonAsync<TemplateDto>(SerializationContext.Default.Options);
|
|
|
|
if (importedTemplate == null)
|
|
continue;
|
|
|
|
await Grid.RefreshAsync();
|
|
await ToastService.SuccessAsync("Template Import", $"Successfully imported template {importedTemplate.Name}");
|
|
}
|
|
}
|
|
} |