Add deleting tokens and upload locations (via context menu)

This commit is contained in:
Ivan Yaremenchuk 2022-10-02 23:15:36 -05:00
parent 2663eda2ca
commit 8f396a620b
7 changed files with 80 additions and 1 deletions

View File

@ -18,4 +18,7 @@ public interface IStateRepository
public Task<ITokenUploadLocation[]> LoadUploadLocations();
public Task<UploaderStateDto[]> LoadUploaderStates();
public Task<WalletStateDto[]> LoadWalletStates();
public Task DeleteTokenLocation(Guid uploadLocationId);
public Task DeleteToken(Guid tokenId);
}

View File

@ -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<T> GetFirst<T>(string storeName)
{
var existingRecords = await _dbManager.GetRecords<T>(storeName);

View File

@ -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))">
<div class="d-flex flex-row align-items-center">
@if (SelectedItems != null && SelectedItems.Contains(cardListItem.Id))
{

View File

@ -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();
}
}

View File

@ -10,4 +10,5 @@ public class CardListItem
public CardListItemProperty[] Properties { get; set; } = Array.Empty<CardListItemProperty>();
public CardListItemBadge[] Badges { get; set; } = Array.Empty<CardListItemBadge>();
public CardListItemButton[] Buttons { get; set; } = Array.Empty<CardListItemButton>();
public CardListItemButton[] ContextMenuButtons { get; set; } = Array.Empty<CardListItemButton>();
}

View File

@ -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<Guid>();
await SaveAppState();
}
private async Task DeleteToken(IToken token)
{
var tokenLocations = AppState?.UserStorage?.UploadLocations?.Where(x => x.TokenId == token.Id).ToArray() ?? Array.Empty<ITokenUploadLocation>();
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();
}
}

View File

@ -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();
}
}