From 21f02960cb4a04257b878098f9c3fe0c7aa4664a Mon Sep 17 00:00:00 2001 From: ilior <78041027+itailiors@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:39:40 +0300 Subject: [PATCH] Refactor UI and State Management for Checkbox and Button Handling (#130) * Refactor UI and State Management for Checkbox and Button Handling * Update Spend.razor * fix for dan msg * fix overlay * overlay fix * Fix code formating * rename StagePinner to StageSpinner --------- Co-authored-by: dangershony --- src/Angor/Client/Pages/Investor.razor | 116 +++++++++++++------------- src/Angor/Client/Pages/Spend.razor | 106 ++++++++++++++++++----- 2 files changed, 143 insertions(+), 79 deletions(-) diff --git a/src/Angor/Client/Pages/Investor.razor b/src/Angor/Client/Pages/Investor.razor index 0f93f74e..d020d52d 100644 --- a/src/Angor/Client/Pages/Investor.razor +++ b/src/Angor/Client/Pages/Investor.razor @@ -139,70 +139,74 @@
-
- +
+
- - - - - - - - - - - - + + + + + + + + + + + + + + @foreach (var project in projects) + { + Stats.TryGetValue(project.ProjectInfo.ProjectIdentifier, out var stats); + var nostrPubKey = project.ProjectInfo.NostrPubKey; + investmentRequestsMap.TryGetValue(nostrPubKey, out bool hasInvestmentRequests); - @foreach (var project in projects) - { - Stats.TryGetValue(project.ProjectInfo.ProjectIdentifier, out var stats); - var nostrPubKey = project.ProjectInfo.NostrPubKey; - investmentRequestsMap.TryGetValue(nostrPubKey, out bool hasInvestmentRequests); - - - - - - - - - - - - - - } + + + + + + + + + + + + + }
NameFunding Target (@network.CoinTicker)Raised (@network.CoinTicker)Raised (% Target)Project StatusMy Investment (@network.CoinTicker)Spent by FounderAvailable to FounderIn RecoveryFounder Approval
NameFunding Target (@network.CoinTicker)Raised (@network.CoinTicker)Raised (% Target)Project StatusMy Investment (@network.CoinTicker)Spent by FounderAvailable to FounderIn RecoveryFounder Approval
- @project.Metadata?.Name - @project.ProjectInfo.TargetAmount @network.CoinTicker@Money.Satoshis(stats?.AmountInvested ?? 0).ToUnit(MoneyUnit.BTC) @network.CoinTicker @((stats?.AmountInvested ?? 0) * 100 / Money.Coins(project.ProjectInfo.TargetAmount).Satoshi) % - @if (project.ProjectInfo.StartDate < DateTime.UtcNow) - { - Funding - } - else - { - Live - } - - @Money.Satoshis(project.AmountInvested ?? 0).ToUnit(MoneyUnit.BTC) @network.CoinTicker - @if (!project.SignaturesInfo?.Signatures.Any() ?? false) - { - - } - --@Money.Satoshis(project.AmountInRecovery ?? 0).ToUnit(MoneyUnit.BTC) @network.CoinTicker - @if (hasInvestmentRequests) - { - Approved - } - -
+ @project.Metadata?.Name + @project.ProjectInfo.TargetAmount @network.CoinTicker@Money.Satoshis(stats?.AmountInvested ?? 0).ToUnit(MoneyUnit.BTC) @network.CoinTicker@((stats?.AmountInvested ?? 0) * 100 / Money.Coins(project.ProjectInfo.TargetAmount).Satoshi) % + @if (project.ProjectInfo.StartDate < DateTime.UtcNow) + { + Funding + } + else + { + Live + } + + @Money.Satoshis(project.AmountInvested ?? 0).ToUnit(MoneyUnit.BTC) @network.CoinTicker + @if (!project.SignaturesInfo?.Signatures.Any() ?? false) + { + + } + --@Money.Satoshis(project.AmountInRecovery ?? 0).ToUnit(MoneyUnit.BTC) @network.CoinTicker + @if (hasInvestmentRequests) + { + Approved + } + else + { + Pending + } +
+ diff --git a/src/Angor/Client/Pages/Spend.razor b/src/Angor/Client/Pages/Spend.razor index 54e9634f..5e240dc2 100644 --- a/src/Angor/Client/Pages/Spend.razor +++ b/src/Angor/Client/Pages/Spend.razor @@ -75,7 +75,7 @@ bool stageisActive = stage.Stage.ReleaseDate < DateTime.UtcNow; var investedCount = stage.Items.Count(c => c.IsSpent == false); var investedAmount = stage.Items.Where(c => c.IsSpent == false).Sum(c => c.Amount); - bool noCoinsToClaim = investedCount == 0 || stage.StagePinner == true; + bool noCoinsToClaim = investedCount == 0 || stage.StageSpinner == true;
@@ -85,8 +85,8 @@ @if (stageisActive) { -
@@ -174,11 +174,19 @@
Stages
- @foreach (var item in selected) + @foreach (var utxo in selectedUtxos) { -

@item.Key

+ if (utxo.Value) // check if the UTXO is selected + { + var utxoKey = utxo.Key; + + var stageIndex = GetStageIndexForUtxo(utxoKey); + +

Stage: @stageIndex, Transaction: @utxoKey.Trxid, Output Index: @utxoKey.Outputindex

+ } } +

Are you sure you want to continue?

@@ -255,14 +263,14 @@ List<(Transaction Transaction, string TrxId)> transactions = new(); - Dictionary selected = new(); + Dictionary selectedUtxos = new Dictionary(); public class StageData { public int StageIndex; public Stage Stage; public List Items = new(); - public bool StagePinner = false; + public bool StageSpinner = false; } public class StageDataTrx @@ -425,8 +433,6 @@ } } - - private async Task CheckSpentFund() { List trxs = new(); @@ -480,7 +486,7 @@ { passwordComponent.ShowPassword(async () => { - await ClaimCoins(stageId); ; + await ClaimCoins(stageId); }); } } @@ -488,8 +494,7 @@ private async Task ClaimCoins(int stageId) { var stage = stageDatas.First(s => s.StageIndex == stageId); - - stage.StagePinner = true; + stage.StageSpinner = true; StateHasChanged(); await Task.Delay(10); @@ -507,13 +512,15 @@ founderContext = new FounderContext { ProjectInfo = project, ProjectSeeders = new ProjectSeeders() }; - foreach (var item in selected) + foreach (var utxo in selectedUtxos.Where(kv => GetStageIndexForUtxo(kv.Key) == stageId && kv.Value)) { - var trx = transactions.First(f => f.TrxId == item.Key); + var trxId = utxo.Key.Trxid; + var trx = transactions.First(f => f.TrxId == trxId); founderContext.InvestmentTrasnactionsHex.Add(trx.Transaction.ToHex(network.Consensus.ConsensusFactory)); } + var accountInfo = storage.GetAccountInfo(network.Name); var address = accountInfo.GetNextReceiveAddress(); var addressScript = BitcoinWitPubKeyAddress.Create(address, network).ScriptPubKey; @@ -533,12 +540,19 @@ } finally { - stage.StagePinner = false; + CalculateTotalValues(); + + if (selectedUtxos.Keys.Any(utxoKey => GetStageIndexForUtxo(utxoKey) == selectedStageId)) + { + selectedUtxos.Clear(); + } + stage.StageSpinner = false; } - + StateHasChanged(); } + private async Task FeeRangeChanged(ChangeEventArgs e) { var selectedItem = e.Value?.ToString(); @@ -628,20 +642,39 @@ private void Expand(int stageId) { expandedStageId = expandedStageId == stageId ? null : stageId; + selectedUtxos.Clear(); } - private void HandleCheckboxChange(string trxId) + private void HandleCheckboxChange(string trxId, int outputIndex) { + var key = new UtxoKey(trxId, outputIndex); - if (selected.ContainsKey(trxId)) + if (selectedUtxos.ContainsKey(key)) { - selected.Remove(trxId); + selectedUtxos.Remove(key); } else { - selected.Add(trxId, null); + selectedUtxos[key] = true; } } + + private bool IsUtxoSelected(string trxId, int outputIndex) + { + return selectedUtxos.ContainsKey(new UtxoKey(trxId, outputIndex)); + } + + private int GetStageIndexForUtxo(UtxoKey utxoKey) + { + return stageDatas.First(stage => stage.Items.Any(item => item.Trxid == utxoKey.Trxid && item.Outputindex == utxoKey.Outputindex)).StageIndex; + } + + + private bool IsCheckboxSelectedForStage(int stageIndex) + { + return selectedUtxos.Any(utxo => GetStageIndexForUtxo(utxo.Key) == stageIndex && utxo.Value); + } + private async Task CopyTRXToClipboard(string trxData) { @@ -653,4 +686,31 @@ await _clipboardService.WriteTextAsync(trxData); StateHasChanged(); } + + public struct UtxoKey + { + public string Trxid { get; set; } + public int Outputindex { get; set; } + + public UtxoKey(string trxid, int outputindex) + { + Trxid = trxid; + Outputindex = outputindex; + } + + public override bool Equals(object obj) + { + if (!(obj is UtxoKey)) + return false; + + var key = (UtxoKey)obj; + return Trxid == key.Trxid && Outputindex == key.Outputindex; + } + + public override int GetHashCode() + { + return HashCode.Combine(Trxid, Outputindex); + } + } + } \ No newline at end of file