Add ConfigurationItems to IUploader

This commit is contained in:
Ivan Yaremenchuk 2022-08-28 21:17:40 -05:00
parent aebcfc7cfa
commit 0a5a4a2f42
7 changed files with 111 additions and 7 deletions

View File

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Cryptography.ECDSA.Secp256K1" Version="1.1.3" />
<PackageReference Include="CSharpFunctionalExtensions" Version="2.33.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.2" PrivateAssets="all" />
<PackageReference Include="Nethereum.ABI" Version="4.8.0" />

View File

@ -3,16 +3,24 @@
<PageTitle>Create upload</PageTitle>
<RadzenContent Container="main">
<RadzenSteps>
<RadzenSteps Change="@OnChange" ShowStepsButtons="false">
<Steps>
<RadzenStepsItem Text="Select uploader">
<CardList Data="@Data" @bind-SelectedItems="@SelectedUploaders"/>
<CardList Data="@Data" @bind-SelectedItems="@SelectedUploaderIds"/>
</RadzenStepsItem>
<RadzenStepsItem Text="Configure uploader" Disabled="@(SelectedUploaders == null || SelectedUploaders.Length == 0)">
<h3 class="mt-4">Specify dedicated domain</h3>
<RadzenTextBox Placeholder="Domain..." MaxLength="50" Class="w-100" />
<RadzenStepsItem Text="Configure uploader" Disabled="@(SelectedUploader == null)">
@if (ConfigurationItems != null)
{
foreach (var configurationItem in ConfigurationItems)
{
<h3 class="mt-4">@configurationItem.Name</h3>
<p>@configurationItem.Tooltip</p>
<RadzenTextBox @bind-Value="@configurationItem.Value" Class="w-100"/>
}
<RadzenButton Text="Verify configuration" Click="@(async () => await VerifyConfiguration())" />
}
</RadzenStepsItem>
<RadzenStepsItem Text="Upload" Disabled="@(SelectedUploaders == null || SelectedUploaders.Length == 0)">
<RadzenStepsItem Text="Upload" Disabled="@(SelectedUploader == null || !SelectedUploader.IsInitialized)">
<h3 class="mt-4">Uploading... Wait!</h3>
</RadzenStepsItem>
</Steps>

View File

@ -2,6 +2,7 @@ using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components;
using NftFaucetRadzen.Components;
using NftFaucetRadzen.Components.CardList;
using NftFaucetRadzen.Plugins;
using NftFaucetRadzen.Plugins.UploadPlugins;
using Radzen;
@ -33,7 +34,9 @@ public partial class CreateUploadPage : BasicComponent
}
private CardListItem[] Data { get; set; }
private Guid[] SelectedUploaders { get; set; }
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 bool ModelIsValid => IsValid();
@ -55,6 +58,29 @@ public partial class CreateUploadPage : BasicComponent
}.Where(x => x != null).ToArray(),
};
private void OnChange(int pageNumber)
{
if (pageNumber == 1)
{
ConfigurationItems = SelectedUploader.GetConfigurationItems();
StateHasChangedSafe();
}
}
private async Task VerifyConfiguration()
{
var result = await SelectedUploader.TryInitialize(ConfigurationItems);
if (result.IsSuccess)
{
NotificationService.Notify(NotificationSeverity.Success, "Init succeeded", "Configuration is valid");
}
else
{
NotificationService.Notify(NotificationSeverity.Error, "Init failed", result.Error);
}
StateHasChangedSafe();
}
private void OnSavePressed()
{
if (!IsValid())

View File

@ -0,0 +1,8 @@
namespace NftFaucetRadzen.Plugins;
public class ConfigurationItem
{
public string Name { get; set; }
public string Tooltip { get; set; }
public string Value { get; set; }
}

View File

@ -1,3 +1,5 @@
using CSharpFunctionalExtensions;
namespace NftFaucetRadzen.Plugins.UploadPlugins;
public interface IUploader
@ -8,4 +10,6 @@ public interface IUploader
public string ImageName { get; }
public bool IsSupported { get; }
public bool IsInitialized { get; }
public IReadOnlyCollection<ConfigurationItem> GetConfigurationItems();
public Task<Result> TryInitialize(IReadOnlyCollection<ConfigurationItem> configurationItems);
}

View File

@ -1,3 +1,5 @@
using CSharpFunctionalExtensions;
namespace NftFaucetRadzen.Plugins.UploadPlugins.InfuraDedicatedGateway.Uploaders;
public class InfuraDedicatedGatewayUploader : IUploader
@ -8,4 +10,47 @@ public class InfuraDedicatedGatewayUploader : IUploader
public string ImageName { get; } = "infura_black.svg";
public bool IsSupported { get; } = true;
public bool IsInitialized { get; private set; } = false;
public IReadOnlyCollection<ConfigurationItem> GetConfigurationItems()
=> new[]
{
new ConfigurationItem
{
Name = "Dedicated gateway URL",
Tooltip = "Specify full URL with 'https://' prefix and '.infura-ipfs.io' postfix",
Value = "https://<your-subdomain>.infura-ipfs.io",
},
};
public async Task<Result> TryInitialize(IReadOnlyCollection<ConfigurationItem> configurationItems)
{
if (configurationItems == null || configurationItems.Count != 1)
{
return Result.Failure("Invalid configuration items count");
}
var urlString = configurationItems.First().Value;
if (string.IsNullOrEmpty(urlString))
{
return Result.Failure("Url string is null or empty");
}
if (!Uri.TryCreate(urlString, UriKind.Absolute, out var url))
{
return Result.Failure("Url string is invalid");
}
if (url.Scheme != "https")
{
return Result.Failure("Invalid url scheme. Expected 'https'");
}
if (!url.Host.EndsWith("infura-ipfs.io"))
{
return Result.Failure("Invalid url host. Expected host ending with '.infura-ipfs.io'");
}
IsInitialized = true;
return Result.Success();
}
}

View File

@ -1,3 +1,5 @@
using CSharpFunctionalExtensions;
namespace NftFaucetRadzen.Plugins.UploadPlugins.NftStorage.Uploaders;
public class NftStorageUploader : IUploader
@ -8,4 +10,14 @@ public class NftStorageUploader : IUploader
public string ImageName { get; } = "nft-storage.svg";
public bool IsSupported { get; } = false;
public bool IsInitialized { get; } = false;
public IReadOnlyCollection<ConfigurationItem> GetConfigurationItems()
{
throw new NotImplementedException();
}
public Task<Result> TryInitialize(IReadOnlyCollection<ConfigurationItem> configurationItems)
{
throw new NotImplementedException();
}
}