Add creation of TokenUploadLocation

This commit is contained in:
Ivan Yaremenchuk 2022-08-30 21:09:30 -05:00
parent 0a5a4a2f42
commit a574a67275
9 changed files with 143 additions and 44 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components;
using NftFaucetRadzen.Models;
using NftFaucetRadzen.Plugins;
using Radzen;
namespace NftFaucetRadzen.Pages;
@ -33,7 +34,20 @@ public partial class CreateTokenPage
if (!IsValid())
return;
DialogService.Close(Model);
var token = new Token
{
Id = Guid.NewGuid(),
Name = Model.Name,
Description = Model.Description,
CreatedAt = DateTime.Now,
Image = new TokenMedia
{
FileName = Model.FileName,
FileSize = Model.FileSize!.Value,
FileData = Model.FileData,
},
};
DialogService.Close(token);
}
private bool IsValid()

View File

@ -3,7 +3,7 @@
<PageTitle>Create upload</PageTitle>
<RadzenContent Container="main">
<RadzenSteps Change="@OnChange" ShowStepsButtons="false">
<RadzenSteps Change="@(async x => await OnChange(x))" ShowStepsButtons="false">
<Steps>
<RadzenStepsItem Text="Select uploader">
<CardList Data="@Data" @bind-SelectedItems="@SelectedUploaderIds"/>
@ -21,7 +21,21 @@
}
</RadzenStepsItem>
<RadzenStepsItem Text="Upload" Disabled="@(SelectedUploader == null || !SelectedUploader.IsInitialized)">
<h3 class="mt-4">Uploading... Wait!</h3>
@if (FileLocation == null)
{
<h3 class="mt-4">Uploading... Wait!</h3>
}
else if (FileLocation.Value.IsSuccess)
{
<h3 class="mt-4">Upload succeeded!</h3>
<p>@FileLocation.Value.Value.OriginalString</p>
}
else
{
<h3 class="mt-4">Upload failed! :(</h3>
<p>@FileLocation.Value.Error</p>
<RadzenButton Text="Retry" Click="@(async () => await Upload())" />
}
</RadzenStepsItem>
</Steps>
</RadzenSteps>

View File

@ -1,3 +1,4 @@
using CSharpFunctionalExtensions;
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components;
using NftFaucetRadzen.Components;
@ -28,6 +29,9 @@ public partial class CreateUploadPage : BasicComponent
[Inject]
protected NotificationService NotificationService { get; set; }
[Parameter]
public IToken Token { get; set; }
protected override void OnInitialized()
{
Data = AppState.Storage.Uploaders.Select(MapCardListItem).ToArray();
@ -37,7 +41,7 @@ public partial class CreateUploadPage : BasicComponent
private Guid[] SelectedUploaderIds { get; set; }
private IUploader SelectedUploader => AppState?.Storage?.Uploaders?.FirstOrDefault(x => x.Id == SelectedUploaderIds?.FirstOrDefault());
private IReadOnlyCollection<ConfigurationItem> ConfigurationItems { get; set; } = Array.Empty<ConfigurationItem>();
private string FileLocation { get; set; }
private Result<Uri>? FileLocation { get; set; }
private bool ModelIsValid => IsValid();
private CardListItem MapCardListItem(IUploader uploader)
@ -58,13 +62,35 @@ public partial class CreateUploadPage : BasicComponent
}.Where(x => x != null).ToArray(),
};
private void OnChange(int pageNumber)
private async Task OnChange(int pageNumber)
{
if (pageNumber == 1)
{
ConfigurationItems = SelectedUploader.GetConfigurationItems();
StateHasChangedSafe();
}
else if (pageNumber == 2)
{
await Upload();
}
}
private async Task Upload()
{
FileLocation = await SelectedUploader.Upload(Token);
if (FileLocation.HasValue)
{
if (FileLocation.Value.IsSuccess)
{
NotificationService.Notify(NotificationSeverity.Success, "Upload succeeded", FileLocation.Value.Value.OriginalString);
}
else
{
NotificationService.Notify(NotificationSeverity.Error, "Upload failed", FileLocation.Value.Error);
}
}
StateHasChangedSafe();
}
private async Task VerifyConfiguration()
@ -86,8 +112,16 @@ public partial class CreateUploadPage : BasicComponent
if (!IsValid())
return;
DialogService.Close(FileLocation);
var uploadLocation = new TokenUploadLocation
{
Id = Guid.NewGuid(),
Name = SelectedUploader.ShortName,
Location = FileLocation!.Value!.Value!.OriginalString,
CreatedAt = DateTime.Now,
UploaderId = SelectedUploader.Id,
};
DialogService.Close(uploadLocation);
}
private bool IsValid() => !string.IsNullOrWhiteSpace(FileLocation);
private bool IsValid() => FileLocation != null && FileLocation.Value.IsSuccess;
}

View File

@ -56,23 +56,15 @@ public partial class TokensPage : BasicComponent
private async Task OpenCreateTokenDialog()
{
var newFileModel = (NewFileModel) await DialogService.OpenAsync<CreateTokenPage>("Create new token",
var token = (IToken) await DialogService.OpenAsync<CreateTokenPage>("Create new token",
new Dictionary<string, object>(),
new DialogOptions() { Width = "700px", Height = "570px", Resizable = true, Draggable = true });
var token = new Token
if (token == null)
{
Id = Guid.NewGuid(),
Name = newFileModel.Name,
Description = newFileModel.Description,
CreatedAt = DateTime.Now,
Image = new TokenMedia
{
FileName = newFileModel.FileName,
FileSize = newFileModel.FileSize!.Value,
FileData = newFileModel.FileData,
},
};
return;
}
AppState.Storage.Tokens ??= new List<IToken>();
AppState.Storage.Tokens.Add(token);
RefreshData();

View File

@ -1,3 +1,4 @@
using System.Globalization;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using NftFaucetRadzen.Components;
@ -41,9 +42,8 @@ public partial class UploadLocationsPage : BasicComponent
=> new CardListItem
{
Id = uploadLocation.Id,
Header = uploadLocation.Name,// $"{uploadLocation.StorageType} ({uploadLocation.UploadProvider})",
// ToDo: Add image of upload location type
// ImageLocation = uploadLocation.Image.FileData,
Header = uploadLocation.Name,
ImageLocation = GetUploaderImageLocation(uploadLocation.UploaderId),
Properties = new[]
{
new CardListItemProperty
@ -51,31 +51,47 @@ public partial class UploadLocationsPage : BasicComponent
Name = "Id",
Value = uploadLocation.Id.ToString(),
},
new CardListItemProperty
{
Name = "Location",
Value = uploadLocation.Location,
},
new CardListItemProperty
{
Name = "CreatedAt",
Value = uploadLocation.CreatedAt.ToString(CultureInfo.InvariantCulture),
},
},
};
private string GetUploaderImageLocation(Guid uploaderId)
{
var uploader = AppState?.Storage?.Uploaders?.FirstOrDefault(x => x.Id == uploaderId);
if (uploader == null)
{
return null;
}
return "./images/" + uploader.ImageName;
}
private async Task OpenCreateUploadDialog()
{
await DialogService.OpenAsync<CreateUploadPage>("Create new upload",
new Dictionary<string, object>(),
var uploadLocation = (ITokenUploadLocation) await DialogService.OpenAsync<CreateUploadPage>("Create new upload",
new Dictionary<string, object>
{
{ "Token", AppState.SelectedToken },
},
new DialogOptions() { Width = "1000px", Height = "700px", Resizable = true, Draggable = true });
//
// var token = new Token
// {
// Id = Guid.NewGuid(),
// Name = newFileModel.Name,
// Description = newFileModel.Description,
// CreatedAt = DateTime.Now,
// Image = new TokenMedia
// {
// FileName = newFileModel.FileName,
// FileSize = newFileModel.FileSize!.Value,
// FileData = newFileModel.FileData,
// },
// };
// AppState.Storage.Tokens ??= new List<IToken>();
// AppState.Storage.Tokens.Add(token);
// RefreshData();
// StateHasChangedSafe();
if (uploadLocation == null)
{
return;
}
AppState.Storage.UploadLocations ??= new List<ITokenUploadLocation>();
AppState.Storage.UploadLocations.Add(uploadLocation);
RefreshData();
StateHasChangedSafe();
}
}

View File

@ -0,0 +1,10 @@
namespace NftFaucetRadzen.Plugins;
public class TokenUploadLocation : ITokenUploadLocation
{
public Guid Id { get; set; } = Guid.NewGuid();
public string Name { get; set; }
public string Location { get; set; }
public DateTime CreatedAt { get; set; }
public Guid UploaderId { get; set; }
}

View File

@ -12,4 +12,5 @@ public interface IUploader
public bool IsInitialized { get; }
public IReadOnlyCollection<ConfigurationItem> GetConfigurationItems();
public Task<Result> TryInitialize(IReadOnlyCollection<ConfigurationItem> configurationItems);
public Task<Result<Uri>> Upload(IToken token);
}

View File

@ -53,4 +53,17 @@ public class InfuraDedicatedGatewayUploader : IUploader
IsInitialized = true;
return Result.Success();
}
public async Task<Result<Uri>> Upload(IToken token)
{
await Task.Delay(TimeSpan.FromSeconds(2));
if (Random.Shared.Next(1, 101) > 80)
{
return Result.Success(new Uri("https://example.com"));
}
else
{
return Result.Failure<Uri>("FAKE UPLOAD FAILURE");
}
}
}

View File

@ -20,4 +20,9 @@ public class NftStorageUploader : IUploader
{
throw new NotImplementedException();
}
public Task<Result<Uri>> Upload(IToken token)
{
throw new NotImplementedException();
}
}