From a574a6727527993e4a84d8d0a7ab753a6b9e7efd Mon Sep 17 00:00:00 2001 From: Ivan Yaremenchuk Date: Tue, 30 Aug 2022 21:09:30 -0500 Subject: [PATCH] Add creation of TokenUploadLocation --- .../Pages/CreateTokenPage.razor.cs | 16 ++++- NftFaucetRadzen/Pages/CreateUploadPage.razor | 18 +++++- .../Pages/CreateUploadPage.razor.cs | 42 +++++++++++-- NftFaucetRadzen/Pages/TokensPage.razor.cs | 20 ++---- .../Pages/UploadLocationsPage.razor.cs | 62 ++++++++++++------- .../Plugins/TokenUploadLocation.cs | 10 +++ .../Plugins/UploadPlugins/IUploader.cs | 1 + .../InfuraDedicatedGatewayUploader.cs | 13 ++++ .../Uploaders/NftStorageUploader.cs | 5 ++ 9 files changed, 143 insertions(+), 44 deletions(-) create mode 100644 NftFaucetRadzen/Plugins/TokenUploadLocation.cs diff --git a/NftFaucetRadzen/Pages/CreateTokenPage.razor.cs b/NftFaucetRadzen/Pages/CreateTokenPage.razor.cs index 30bbbe4..601fe46 100644 --- a/NftFaucetRadzen/Pages/CreateTokenPage.razor.cs +++ b/NftFaucetRadzen/Pages/CreateTokenPage.razor.cs @@ -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() diff --git a/NftFaucetRadzen/Pages/CreateUploadPage.razor b/NftFaucetRadzen/Pages/CreateUploadPage.razor index 1c32503..2070b17 100644 --- a/NftFaucetRadzen/Pages/CreateUploadPage.razor +++ b/NftFaucetRadzen/Pages/CreateUploadPage.razor @@ -3,7 +3,7 @@ Create upload - + @@ -21,7 +21,21 @@ } -

Uploading... Wait!

+ @if (FileLocation == null) + { +

Uploading... Wait!

+ } + else if (FileLocation.Value.IsSuccess) + { +

Upload succeeded!

+

@FileLocation.Value.Value.OriginalString

+ } + else + { +

Upload failed! :(

+

@FileLocation.Value.Error

+ + }
diff --git a/NftFaucetRadzen/Pages/CreateUploadPage.razor.cs b/NftFaucetRadzen/Pages/CreateUploadPage.razor.cs index 30a1c3a..d9025ef 100644 --- a/NftFaucetRadzen/Pages/CreateUploadPage.razor.cs +++ b/NftFaucetRadzen/Pages/CreateUploadPage.razor.cs @@ -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 ConfigurationItems { get; set; } = Array.Empty(); - private string FileLocation { get; set; } + private Result? 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; } diff --git a/NftFaucetRadzen/Pages/TokensPage.razor.cs b/NftFaucetRadzen/Pages/TokensPage.razor.cs index 453ffd0..c900291 100644 --- a/NftFaucetRadzen/Pages/TokensPage.razor.cs +++ b/NftFaucetRadzen/Pages/TokensPage.razor.cs @@ -56,23 +56,15 @@ public partial class TokensPage : BasicComponent private async Task OpenCreateTokenDialog() { - var newFileModel = (NewFileModel) await DialogService.OpenAsync("Create new token", + var token = (IToken) await DialogService.OpenAsync("Create new token", new Dictionary(), 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(); AppState.Storage.Tokens.Add(token); RefreshData(); diff --git a/NftFaucetRadzen/Pages/UploadLocationsPage.razor.cs b/NftFaucetRadzen/Pages/UploadLocationsPage.razor.cs index 37e1dde..dfb879e 100644 --- a/NftFaucetRadzen/Pages/UploadLocationsPage.razor.cs +++ b/NftFaucetRadzen/Pages/UploadLocationsPage.razor.cs @@ -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("Create new upload", - new Dictionary(), + var uploadLocation = (ITokenUploadLocation) await DialogService.OpenAsync("Create new upload", + new Dictionary + { + { "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(); - // AppState.Storage.Tokens.Add(token); - // RefreshData(); - // StateHasChangedSafe(); + + if (uploadLocation == null) + { + return; + } + + AppState.Storage.UploadLocations ??= new List(); + AppState.Storage.UploadLocations.Add(uploadLocation); + RefreshData(); + StateHasChangedSafe(); } } diff --git a/NftFaucetRadzen/Plugins/TokenUploadLocation.cs b/NftFaucetRadzen/Plugins/TokenUploadLocation.cs new file mode 100644 index 0000000..6ddebee --- /dev/null +++ b/NftFaucetRadzen/Plugins/TokenUploadLocation.cs @@ -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; } +} diff --git a/NftFaucetRadzen/Plugins/UploadPlugins/IUploader.cs b/NftFaucetRadzen/Plugins/UploadPlugins/IUploader.cs index bc70068..76781c5 100644 --- a/NftFaucetRadzen/Plugins/UploadPlugins/IUploader.cs +++ b/NftFaucetRadzen/Plugins/UploadPlugins/IUploader.cs @@ -12,4 +12,5 @@ public interface IUploader public bool IsInitialized { get; } public IReadOnlyCollection GetConfigurationItems(); public Task TryInitialize(IReadOnlyCollection configurationItems); + public Task> Upload(IToken token); } diff --git a/NftFaucetRadzen/Plugins/UploadPlugins/InfuraDedicatedGateway/Uploaders/InfuraDedicatedGatewayUploader.cs b/NftFaucetRadzen/Plugins/UploadPlugins/InfuraDedicatedGateway/Uploaders/InfuraDedicatedGatewayUploader.cs index 3a3cb34..e5dcd52 100644 --- a/NftFaucetRadzen/Plugins/UploadPlugins/InfuraDedicatedGateway/Uploaders/InfuraDedicatedGatewayUploader.cs +++ b/NftFaucetRadzen/Plugins/UploadPlugins/InfuraDedicatedGateway/Uploaders/InfuraDedicatedGatewayUploader.cs @@ -53,4 +53,17 @@ public class InfuraDedicatedGatewayUploader : IUploader IsInitialized = true; return Result.Success(); } + + public async Task> 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("FAKE UPLOAD FAILURE"); + } + } } diff --git a/NftFaucetRadzen/Plugins/UploadPlugins/NftStorage/Uploaders/NftStorageUploader.cs b/NftFaucetRadzen/Plugins/UploadPlugins/NftStorage/Uploaders/NftStorageUploader.cs index 53c64f2..500830e 100644 --- a/NftFaucetRadzen/Plugins/UploadPlugins/NftStorage/Uploaders/NftStorageUploader.cs +++ b/NftFaucetRadzen/Plugins/UploadPlugins/NftStorage/Uploaders/NftStorageUploader.cs @@ -20,4 +20,9 @@ public class NftStorageUploader : IUploader { throw new NotImplementedException(); } + + public Task> Upload(IToken token) + { + throw new NotImplementedException(); + } }