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.JSInterop;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using NftFaucetRadzen.Models; using NftFaucetRadzen.Models;
using NftFaucetRadzen.Plugins;
using Radzen; using Radzen;
namespace NftFaucetRadzen.Pages; namespace NftFaucetRadzen.Pages;
@ -33,7 +34,20 @@ public partial class CreateTokenPage
if (!IsValid()) if (!IsValid())
return; 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() private bool IsValid()

View File

@ -3,7 +3,7 @@
<PageTitle>Create upload</PageTitle> <PageTitle>Create upload</PageTitle>
<RadzenContent Container="main"> <RadzenContent Container="main">
<RadzenSteps Change="@OnChange" ShowStepsButtons="false"> <RadzenSteps Change="@(async x => await OnChange(x))" ShowStepsButtons="false">
<Steps> <Steps>
<RadzenStepsItem Text="Select uploader"> <RadzenStepsItem Text="Select uploader">
<CardList Data="@Data" @bind-SelectedItems="@SelectedUploaderIds"/> <CardList Data="@Data" @bind-SelectedItems="@SelectedUploaderIds"/>
@ -21,7 +21,21 @@
} }
</RadzenStepsItem> </RadzenStepsItem>
<RadzenStepsItem Text="Upload" Disabled="@(SelectedUploader == null || !SelectedUploader.IsInitialized)"> <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> </RadzenStepsItem>
</Steps> </Steps>
</RadzenSteps> </RadzenSteps>

View File

@ -1,3 +1,4 @@
using CSharpFunctionalExtensions;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using NftFaucetRadzen.Components; using NftFaucetRadzen.Components;
@ -28,6 +29,9 @@ public partial class CreateUploadPage : BasicComponent
[Inject] [Inject]
protected NotificationService NotificationService { get; set; } protected NotificationService NotificationService { get; set; }
[Parameter]
public IToken Token { get; set; }
protected override void OnInitialized() protected override void OnInitialized()
{ {
Data = AppState.Storage.Uploaders.Select(MapCardListItem).ToArray(); Data = AppState.Storage.Uploaders.Select(MapCardListItem).ToArray();
@ -37,7 +41,7 @@ public partial class CreateUploadPage : BasicComponent
private Guid[] SelectedUploaderIds { get; set; } private Guid[] SelectedUploaderIds { get; set; }
private IUploader SelectedUploader => AppState?.Storage?.Uploaders?.FirstOrDefault(x => x.Id == SelectedUploaderIds?.FirstOrDefault()); private IUploader SelectedUploader => AppState?.Storage?.Uploaders?.FirstOrDefault(x => x.Id == SelectedUploaderIds?.FirstOrDefault());
private IReadOnlyCollection<ConfigurationItem> ConfigurationItems { get; set; } = Array.Empty<ConfigurationItem>(); 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 bool ModelIsValid => IsValid();
private CardListItem MapCardListItem(IUploader uploader) private CardListItem MapCardListItem(IUploader uploader)
@ -58,13 +62,35 @@ public partial class CreateUploadPage : BasicComponent
}.Where(x => x != null).ToArray(), }.Where(x => x != null).ToArray(),
}; };
private void OnChange(int pageNumber) private async Task OnChange(int pageNumber)
{ {
if (pageNumber == 1) if (pageNumber == 1)
{ {
ConfigurationItems = SelectedUploader.GetConfigurationItems(); ConfigurationItems = SelectedUploader.GetConfigurationItems();
StateHasChangedSafe(); 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() private async Task VerifyConfiguration()
@ -86,8 +112,16 @@ public partial class CreateUploadPage : BasicComponent
if (!IsValid()) if (!IsValid())
return; 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() 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 Dictionary<string, object>(),
new DialogOptions() { Width = "700px", Height = "570px", Resizable = true, Draggable = true }); new DialogOptions() { Width = "700px", Height = "570px", Resizable = true, Draggable = true });
var token = new Token if (token == null)
{ {
Id = Guid.NewGuid(), return;
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 ??= new List<IToken>();
AppState.Storage.Tokens.Add(token); AppState.Storage.Tokens.Add(token);
RefreshData(); RefreshData();

View File

@ -1,3 +1,4 @@
using System.Globalization;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using NftFaucetRadzen.Components; using NftFaucetRadzen.Components;
@ -41,9 +42,8 @@ public partial class UploadLocationsPage : BasicComponent
=> new CardListItem => new CardListItem
{ {
Id = uploadLocation.Id, Id = uploadLocation.Id,
Header = uploadLocation.Name,// $"{uploadLocation.StorageType} ({uploadLocation.UploadProvider})", Header = uploadLocation.Name,
// ToDo: Add image of upload location type ImageLocation = GetUploaderImageLocation(uploadLocation.UploaderId),
// ImageLocation = uploadLocation.Image.FileData,
Properties = new[] Properties = new[]
{ {
new CardListItemProperty new CardListItemProperty
@ -51,31 +51,47 @@ public partial class UploadLocationsPage : BasicComponent
Name = "Id", Name = "Id",
Value = uploadLocation.Id.ToString(), 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() private async Task OpenCreateUploadDialog()
{ {
await DialogService.OpenAsync<CreateUploadPage>("Create new upload", var uploadLocation = (ITokenUploadLocation) await DialogService.OpenAsync<CreateUploadPage>("Create new upload",
new Dictionary<string, object>(), new Dictionary<string, object>
{
{ "Token", AppState.SelectedToken },
},
new DialogOptions() { Width = "1000px", Height = "700px", Resizable = true, Draggable = true }); new DialogOptions() { Width = "1000px", Height = "700px", Resizable = true, Draggable = true });
//
// var token = new Token if (uploadLocation == null)
// { {
// Id = Guid.NewGuid(), return;
// Name = newFileModel.Name, }
// Description = newFileModel.Description,
// CreatedAt = DateTime.Now, AppState.Storage.UploadLocations ??= new List<ITokenUploadLocation>();
// Image = new TokenMedia AppState.Storage.UploadLocations.Add(uploadLocation);
// { RefreshData();
// FileName = newFileModel.FileName, StateHasChangedSafe();
// FileSize = newFileModel.FileSize!.Value,
// FileData = newFileModel.FileData,
// },
// };
// AppState.Storage.Tokens ??= new List<IToken>();
// AppState.Storage.Tokens.Add(token);
// 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 bool IsInitialized { get; }
public IReadOnlyCollection<ConfigurationItem> GetConfigurationItems(); public IReadOnlyCollection<ConfigurationItem> GetConfigurationItems();
public Task<Result> TryInitialize(IReadOnlyCollection<ConfigurationItem> configurationItems); 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; IsInitialized = true;
return Result.Success(); 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(); throw new NotImplementedException();
} }
public Task<Result<Uri>> Upload(IToken token)
{
throw new NotImplementedException();
}
} }