diff --git a/NftFaucet.Infrastructure/Repositories/IStateRepository.cs b/NftFaucet.Infrastructure/Repositories/IStateRepository.cs index 4a6119c..41274c4 100644 --- a/NftFaucet.Infrastructure/Repositories/IStateRepository.cs +++ b/NftFaucet.Infrastructure/Repositories/IStateRepository.cs @@ -18,4 +18,7 @@ public interface IStateRepository public Task LoadUploadLocations(); public Task LoadUploaderStates(); public Task LoadWalletStates(); + + public Task DeleteTokenLocation(Guid uploadLocationId); + public Task DeleteToken(Guid tokenId); } diff --git a/NftFaucet.Infrastructure/Repositories/StateRepository.cs b/NftFaucet.Infrastructure/Repositories/StateRepository.cs index ca5dc07..500682b 100644 --- a/NftFaucet.Infrastructure/Repositories/StateRepository.cs +++ b/NftFaucet.Infrastructure/Repositories/StateRepository.cs @@ -176,6 +176,16 @@ public class StateRepository : IStateRepository return existingWalletStates.ToArray(); } + public async Task DeleteTokenLocation(Guid uploadLocationId) + { + await _dbManager.DeleteRecord(UploadLocationsStoreName, uploadLocationId); + } + + public async Task DeleteToken(Guid tokenId) + { + await _dbManager.DeleteRecord(TokensStoreName, tokenId); + } + private async Task GetFirst(string storeName) { var existingRecords = await _dbManager.GetRecords(storeName); diff --git a/NftFaucet/Components/CardList/CardList.razor b/NftFaucet/Components/CardList/CardList.razor index 06007c1..3d5ba9e 100644 --- a/NftFaucet/Components/CardList/CardList.razor +++ b/NftFaucet/Components/CardList/CardList.razor @@ -7,7 +7,8 @@ + (SelectedItems != null && SelectedItems.Contains(cardListItem.Id) ? (cardListItem.SelectionIcon == CardListItemSelectionIcon.Checkmark ? " box-checkmark" : " box-warning") : string.Empty)) - Style="width: 250px; position: relative;" onclick="@(async () => await ToggleSelection(cardListItem))"> + Style="width: 250px; position: relative;" onclick="@(async () => await ToggleSelection(cardListItem))" + ContextMenu="@(args => ShowContextMenu(args, cardListItem.ContextMenuButtons))">
@if (SelectedItems != null && SelectedItems.Contains(cardListItem.Id)) { diff --git a/NftFaucet/Components/CardList/CardList.razor.cs b/NftFaucet/Components/CardList/CardList.razor.cs index a8aae7e..3afe29d 100644 --- a/NftFaucet/Components/CardList/CardList.razor.cs +++ b/NftFaucet/Components/CardList/CardList.razor.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Web; using NftFaucet.Pages; using Radzen; @@ -39,4 +40,24 @@ public partial class CardList : BasicComponent await OnSelectedChange.InvokeAsync(SelectedItems); RefreshMediator.NotifyStateHasChangedSafe(); } + + private void ShowContextMenu(MouseEventArgs args, CardListItemButton[] contextMenuButtons) + { + if (contextMenuButtons == null || contextMenuButtons.Length == 0) + return; + + ContextMenuService.Open(args, + contextMenuButtons.Select(x => new ContextMenuItem + { + Text = x.Name, + Value = x.Action, + }).ToList(), OnMenuItemClick); + } + + private void OnMenuItemClick(MenuItemEventArgs obj) + { + ContextMenuService.Close(); + var action = (Action) obj.Value; + action(); + } } diff --git a/NftFaucet/Components/CardList/CardListItem.cs b/NftFaucet/Components/CardList/CardListItem.cs index c9b79b2..7eec027 100644 --- a/NftFaucet/Components/CardList/CardListItem.cs +++ b/NftFaucet/Components/CardList/CardListItem.cs @@ -10,4 +10,5 @@ public class CardListItem public CardListItemProperty[] Properties { get; set; } = Array.Empty(); public CardListItemBadge[] Badges { get; set; } = Array.Empty(); public CardListItemButton[] Buttons { get; set; } = Array.Empty(); + public CardListItemButton[] ContextMenuButtons { get; set; } = Array.Empty(); } diff --git a/NftFaucet/Pages/TokensPage.razor.cs b/NftFaucet/Pages/TokensPage.razor.cs index 5c7edc9..336960c 100644 --- a/NftFaucet/Pages/TokensPage.razor.cs +++ b/NftFaucet/Pages/TokensPage.razor.cs @@ -11,6 +11,7 @@ public partial class TokensPage : BasicComponent protected override void OnInitialized() { RefreshCards(); + base.OnInitialized(); } private CardListItem[] TokenCards { get; set; } @@ -49,6 +50,14 @@ public partial class TokensPage : BasicComponent Header = token.Name, ImageLocation = token.CoverFile?.FileData ?? token.MainFile.FileData, Properties = properties.ToArray(), + ContextMenuButtons = new [] + { + new CardListItemButton + { + Name = "Delete", + Action = async () => await DeleteToken(token), + }, + }, }; } @@ -78,4 +87,20 @@ public partial class TokensPage : BasicComponent AppState.UserStorage.SelectedUploadLocations = Array.Empty(); await SaveAppState(); } + + private async Task DeleteToken(IToken token) + { + var tokenLocations = AppState?.UserStorage?.UploadLocations?.Where(x => x.TokenId == token.Id).ToArray() ?? Array.Empty(); + foreach (var tokenUploadLocation in tokenLocations) + { + await StateRepository.DeleteTokenLocation(tokenUploadLocation.Id); + } + + await StateRepository.DeleteToken(token.Id); + + AppState!.UserStorage!.UploadLocations = AppState.UserStorage.UploadLocations!.Except(tokenLocations).ToList(); + AppState!.UserStorage!.Tokens = AppState.UserStorage.Tokens!.Where(x => x.Id != token.Id).ToList(); + RefreshCards(); + RefreshMediator.NotifyStateHasChangedSafe(); + } } diff --git a/NftFaucet/Pages/UploadLocationsPage.razor.cs b/NftFaucet/Pages/UploadLocationsPage.razor.cs index 313cea9..65fb9a5 100644 --- a/NftFaucet/Pages/UploadLocationsPage.razor.cs +++ b/NftFaucet/Pages/UploadLocationsPage.razor.cs @@ -11,6 +11,7 @@ public partial class UploadLocationsPage : BasicComponent protected override void OnInitialized() { RefreshCards(); + base.OnInitialized(); } private CardListItem[] UploadCards { get; set; } @@ -46,6 +47,14 @@ public partial class UploadLocationsPage : BasicComponent Value = uploadLocation.CreatedAt.ToString(CultureInfo.InvariantCulture), }, }, + ContextMenuButtons = new [] + { + new CardListItemButton + { + Name = "Delete", + Action = async () => await DeleteUploadLocation(uploadLocation), + }, + }, }; private string GetUploaderImageLocation(Guid uploaderId) @@ -86,4 +95,13 @@ public partial class UploadLocationsPage : BasicComponent { await SaveAppState(); } + + private async Task DeleteUploadLocation(ITokenUploadLocation uploadLocation) + { + await StateRepository.DeleteTokenLocation(uploadLocation.Id); + + AppState!.UserStorage!.UploadLocations = AppState.UserStorage.UploadLocations!.Where(x => x.Id != uploadLocation.Id).ToList(); + RefreshCards(); + RefreshMediator.NotifyStateHasChangedSafe(); + } }