nft-faucet/NftFaucetRadzen/Pages/CreateUploadPage.razor.cs

96 lines
3.4 KiB
C#
Raw Normal View History

2022-08-30 21:09:30 -05:00
using CSharpFunctionalExtensions;
2022-08-28 19:05:50 -05:00
using Microsoft.AspNetCore.Components;
using NftFaucetRadzen.Components;
using NftFaucetRadzen.Components.CardList;
2022-08-28 21:17:40 -05:00
using NftFaucetRadzen.Plugins;
2022-08-28 19:05:50 -05:00
using NftFaucetRadzen.Plugins.UploadPlugins;
using Radzen;
namespace NftFaucetRadzen.Pages;
public partial class CreateUploadPage : BasicComponent
{
2022-09-16 13:51:34 -05:00
[Parameter] public IToken Token { get; set; }
2022-08-30 21:09:30 -05:00
2022-08-28 19:05:50 -05:00
protected override void OnInitialized()
{
2022-09-17 20:41:56 -05:00
RefreshCards();
base.OnInitialized();
2022-08-28 19:05:50 -05:00
}
2022-08-31 11:32:23 -05:00
private CardListItem[] UploaderCards { get; set; }
2022-08-28 21:17:40 -05:00
private Guid[] SelectedUploaderIds { get; set; }
private IUploader SelectedUploader => AppState?.Storage?.Uploaders?.FirstOrDefault(x => x.Id == SelectedUploaderIds?.FirstOrDefault());
2022-08-30 21:09:30 -05:00
private Result<Uri>? FileLocation { get; set; }
2022-09-18 19:38:34 -05:00
private bool IsUploading { get; set; }
2022-08-28 19:05:50 -05:00
2022-09-17 20:41:56 -05:00
private void RefreshCards()
{
UploaderCards = AppState.Storage.Uploaders.Select(MapCardListItem).ToArray();
}
2022-08-28 19:05:50 -05:00
private CardListItem MapCardListItem(IUploader uploader)
2022-09-17 20:41:56 -05:00
{
var configuration = uploader.GetConfiguration();
return new CardListItem
2022-08-28 19:05:50 -05:00
{
Id = uploader.Id,
ImageLocation = uploader.ImageName != null ? "./images/" + uploader.ImageName : null,
Header = uploader.Name,
2022-09-18 19:54:37 -05:00
Properties = uploader.GetProperties(),
2022-08-28 19:05:50 -05:00
IsDisabled = !uploader.IsSupported,
2022-09-18 00:02:38 -05:00
SelectionIcon = uploader.IsConfigured ? CardListItemSelectionIcon.Checkmark : CardListItemSelectionIcon.Warning,
2022-08-28 19:05:50 -05:00
Badges = new[]
{
(Settings?.RecommendedUploaders?.Contains(uploader.Id) ?? false)
? new CardListItemBadge {Style = BadgeStyle.Success, Text = "Recommended"}
: null,
!uploader.IsSupported
? new CardListItemBadge {Style = BadgeStyle.Light, Text = "Not Supported"}
: null,
}.Where(x => x != null).ToArray(),
2022-09-17 20:41:56 -05:00
Configuration = configuration == null
? null
: new CardListItemConfiguration
{
Objects = configuration.Objects,
ConfigureAction = x =>
{
var result = configuration.ConfigureAction(x);
RefreshCards();
return result;
},
},
2022-08-28 19:05:50 -05:00
};
2022-09-17 20:41:56 -05:00
}
2022-08-28 19:05:50 -05:00
private async Task OnSavePressed()
2022-08-30 21:09:30 -05:00
{
2022-09-18 19:38:34 -05:00
IsUploading = true;
RefreshMediator.NotifyStateHasChangedSafe();
2022-08-30 21:09:30 -05:00
FileLocation = await SelectedUploader.Upload(Token);
2022-09-18 19:38:34 -05:00
IsUploading = false;
RefreshMediator.NotifyStateHasChangedSafe();
if (FileLocation.Value.IsSuccess)
2022-08-30 21:09:30 -05:00
{
NotificationService.Notify(NotificationSeverity.Success, "Upload succeeded", FileLocation.Value.Value.OriginalString);
var uploadLocation = new TokenUploadLocation
2022-08-30 21:09:30 -05:00
{
Id = Guid.NewGuid(),
Name = SelectedUploader.ShortName,
Location = FileLocation!.Value!.Value!.OriginalString,
CreatedAt = DateTime.Now,
UploaderId = SelectedUploader.Id,
};
DialogService.Close(uploadLocation);
2022-08-30 21:09:30 -05:00
}
else
2022-08-30 21:09:30 -05:00
{
NotificationService.Notify(NotificationSeverity.Error, "Upload failed", FileLocation.Value.Error);
}
2022-08-28 19:05:50 -05:00
}
}