LANCommander/LANCommander.Server/UI/Pages/Servers/Components/ServerHttpPathEditor.razor

87 lines
3 KiB
Text

@using System.Collections.ObjectModel
@inject NavigationManager NavigationManager
@inject ServerHttpPathService ServerHttpPathService
@inject IMessageService MessageService
@inject ILogger<ServerHttpPathEditor> Logger
<Flex Direction="FlexDirection.Vertical" Gap="FlexGap.Large">
<Table TItem="ServerHttpPath" DataSource="@_httpPaths" HidePagination="true" Responsive>
<PropertyColumn Property="p => p.LocalPath" Title="Local Path">
<FilePicker @bind-Value="context.LocalPath" AllowDirectories Title="Select Local Path" Root="@WorkingDirectory" />
</PropertyColumn>
<PropertyColumn Property="p => p.Path">
<Input Type="InputType.Text" @bind-Value="context.Path" />
</PropertyColumn>
<ActionColumn>
<Space Style="display: flex; justify-content: end">
@if (context != null && context.Id != Guid.Empty)
{
<SpaceItem>
<a class="ant-btn ant-btn-text ant-btn-icon-only" href="@NavigationManager.ToAbsoluteUri($"Server/{ServerId}/{context.Path.TrimStart('/')}")" target="_blank">
<Icon Type="@IconType.Outline.Eye" />
</a>
</SpaceItem>
}
<SpaceItem>
<Button OnClick="() => Remove(context)" Type="@ButtonType.Text" Danger Icon="@IconType.Outline.Close" />
</SpaceItem>
</Space>
</ActionColumn>
</Table>
<Flex Justify="FlexJustify.End">
<Button OnClick="Add" Type="@ButtonType.Primary">Add Path</Button>
</Flex>
</Flex>
@code {
[Parameter] public Guid ServerId { get; set; }
[Parameter] public string WorkingDirectory { get; set; }
ObservableCollection<ServerHttpPath> _httpPaths = new();
protected override async Task OnInitializedAsync() => await LoadHttpPaths();
async Task LoadHttpPaths()
{
_httpPaths = new ObservableCollection<ServerHttpPath>(await ServerHttpPathService.GetAsync(p => p.ServerId == ServerId));
}
void Add()
{
_httpPaths.Add(new ServerHttpPath
{
ServerId = ServerId,
});
}
async Task Remove(ServerHttpPath httpPath)
{
if (httpPath.Id != Guid.Empty)
await ServerHttpPathService.DeleteAsync(httpPath);
_httpPaths.Remove(httpPath);
}
public async Task Save()
{
try
{
foreach (var httpPath in _httpPaths)
{
if (httpPath.Id == Guid.Empty)
await ServerHttpPathService.AddAsync(httpPath);
else
await ServerHttpPathService.UpdateAsync(httpPath);
}
await LoadHttpPaths();
MessageService.Success("HTTP paths updated!");
}
catch (Exception ex)
{
MessageService.Error("Could not update HTTP paths!");
Logger.LogError(ex, "Could not update HTTP paths!");
}
}
}