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

115 lines
No EOL
3.2 KiB
Text

@using System.Net;
@using System.Diagnostics;
@using Hangfire;
@using LANCommander.Server.Jobs.Background;
@using Microsoft.EntityFrameworkCore;
@inject NavigationManager Navigator
@inject RedistributableService RedistributableService
@inject IMessageService MessageService
@inject IJSRuntime JS
@inject ILogger<ImportUploadDialog> Logger
@{
RenderFragment Footer =
@<Template>
<Button OnClick="Start" Disabled="@(File == null || Status != "")" Type="@ButtonType.Primary">Upload</Button>
<Button OnClick="Cancel">Cancel</Button>
</Template>;
}
<Modal Visible="@Visible" Title="Import Redistributable" OnOk="Start" OnCancel="Cancel" Footer="@Footer">
<ChunkUploader @ref="ChunkUploader" Accept=".lcx" @bind-File="File" @bind-Status="Status" OnUploadCompleted="OnUploadCompleted" OnUploadError="OnUploadError">
<Text>
<p>Drag and Drop</p>
<p>or</p>
<p>
<Button Type="@ButtonType.Primary" Style="margin-top: 8px;">Browse</Button>
</p>
</Text>
<Hint>Only LCX files are supported for importing redistributables</Hint>
</ChunkUploader>
</Modal>
@code {
[Parameter] public EventCallback<Guid> OnRedistributableImported { get; set; }
[Parameter] public RenderFragment Hint { get; set; }
IBrowserFile File;
ChunkUploader ChunkUploader;
bool IsValid = false;
bool Visible = false;
string Filename;
string Status;
private async Task Start()
{
await ChunkUploader.Start();
}
private async Task Clear()
{
await ChunkUploader.Clear();
}
private async Task Cancel()
{
await ChunkUploader.Clear();
Visible = false;
}
public async Task Open()
{
Visible = true;
await InvokeAsync(StateHasChanged);
}
public async Task OnUploadCompleted(string data)
{
if (Guid.TryParse(data, out var objectKey))
{
try
{
var redistributable = await RedistributableService.Import(objectKey);
Visible = false;
await InvokeAsync(StateHasChanged);
if (OnRedistributableImported.HasDelegate)
await OnRedistributableImported.InvokeAsync(redistributable.Id);
MessageService.Success($"{redistributable.Name} successfully imported!");
}
catch (Exception ex)
{
MessageService.Error("An unknown error occurred while trying to import");
Logger.LogError(ex, "An unknown error occurred while trying to import");
}
}
else
{
Visible = false;
await InvokeAsync(StateHasChanged);
MessageService.Error("Import file failed to upload!");
Logger.LogError("Import file failed to upload!");
}
await ChunkUploader.Clear();
}
public async Task OnUploadError(string message)
{
Visible = false;
await InvokeAsync(StateHasChanged);
MessageService.Error("An error occurred while trying to import");
Logger.LogError($"An error occurred while trying to import: {message}");
}
}