LANCommander/LANCommander.Server/UI/Components/ArchiveEditor.razor
2024-08-04 18:44:33 -05:00

138 lines
4.6 KiB
Text

@using System.Net;
@using System.Diagnostics;
@using Hangfire;
@using LANCommander.Server.Jobs.Background;
@using Microsoft.EntityFrameworkCore;
@inject HttpClient HttpClient
@inject NavigationManager Navigator
@inject ArchiveService ArchiveService
@inject ModalService ModalService
@inject IMessageService MessageService
@inject IJSRuntime JS
@inject ILogger<ArchiveEditor> Logger
<Space Direction="DirectionVHType.Vertical" Style="width: 100%">
<SpaceItem>
<Table TItem="Archive" DataSource="@Archives" HidePagination="true" Responsive>
<PropertyColumn Property="a => a.Version">
<Input Type="text" Bordered="false" @bind-Value="context.Version" OnBlur="() => Update(context)" />
</PropertyColumn>
<PropertyColumn Property="a => a.CompressedSize">
@ByteSizeLib.ByteSize.FromBytes(context.CompressedSize)
</PropertyColumn>
<PropertyColumn Property="a => a.CreatedBy">
@context.CreatedBy?.UserName
</PropertyColumn>
<PropertyColumn Property="a => a.CreatedOn" Format="MM/dd/yyyy hh:mm tt" DefaultSortOrder="@SortDirection.Descending" />
<ActionColumn Title="">
<Space Style="display: flex; justify-content: end">
<SpaceItem>
<a href="/Download/Archive/@context.Id" target="_blank" class="ant-btn ant-btn-text ant-btn-icon-only">
<Icon Type="@IconType.Outline.Download" />
</a>
</SpaceItem>
<SpaceItem>
<Button Icon="@IconType.Outline.FolderOpen" Type="@ButtonType.Text" OnClick="() => BrowseArchive(context)" />
</SpaceItem>
<SpaceItem>
<Popconfirm Title="Are you sure you want to delete this archive?" OnConfirm="() => Delete(context)">
<Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger />
</Popconfirm>
</SpaceItem>
</Space>
</ActionColumn>
</Table>
</SpaceItem>
<SpaceItem>
<GridRow Justify="end">
<GridCol>
<Button OnClick="UploadArchive" Type="@ButtonType.Primary">Upload Archive</Button>
</GridCol>
</GridRow>
</SpaceItem>
</Space>
<ArchiveUploader @ref="Uploader" GameId="GameId" RedistributableId="RedistributableId" OnArchiveUploaded="LoadData" />
@code {
[Parameter] public Guid GameId { get; set; }
[Parameter] public Guid RedistributableId { get; set; }
ICollection<Archive> Archives { get; set; }
ArchiveUploader Uploader;
protected override async Task OnInitializedAsync()
{
await LoadData();
HttpClient.BaseAddress = new Uri(Navigator.BaseUri);
}
private async Task LoadData()
{
if (GameId != Guid.Empty)
Archives = await ArchiveService.Get(a => a.GameId == GameId).ToListAsync();
else if (RedistributableId != Guid.Empty)
Archives = await ArchiveService.Get(a => a.RedistributableId == RedistributableId).ToListAsync();
}
private async Task Download(Archive archive)
{
string url = $"/Download/Game/{archive.Id}";
await JS.InvokeAsync<object>("open", url, "_blank");
}
private async Task UploadArchive()
{
await Uploader.Open();
}
private async Task BrowseArchive(Archive archive)
{
var modalOptions = new ModalOptions()
{
Title = "Browse Archive",
Maximizable = false,
DefaultMaximized = true,
Closable = true,
WrapClassName = "file-picker-dialog",
};
var modalRef = await ModalService.CreateModalAsync<ArchiveBrowserDialog, Guid, Guid>(modalOptions, archive.Id);
}
private async Task Update(Archive archive)
{
try
{
await ArchiveService.Update(archive);
MessageService.Success("Archive updated!");
}
catch (Exception ex)
{
MessageService.Error("Archive could not be updated.");
Logger.LogError(ex, "Archive could not be updated.");
}
}
private async Task Delete(Archive archive)
{
try
{
await ArchiveService.Delete(archive);
await LoadData();
MessageService.Success("Archive deleted!");
}
catch (Exception ex)
{
MessageService.Error("Archive could not be deleted.");
Logger.LogError(ex, "Archive could not be deleted.");
}
}
}