From 1eb0800eb13ea00014a9ce07970b1491501a8fef Mon Sep 17 00:00:00 2001 From: Michiel Post Date: Wed, 24 Apr 2024 16:45:03 +0200 Subject: [PATCH] ActionEditor improvements with TokenData and balance data --- src/aoWebWallet/Pages/ActionPage.razor | 73 +++++-------- src/aoWebWallet/Pages/ActionPage.razor.cs | 2 + src/aoWebWallet/Services/TokenDataService.cs | 7 +- src/aoWebWallet/Shared/ActionEditor.razor | 54 ++++++++++ .../Components/ActionInputComponent.razor | 53 +++++++-- .../Components/ActionQuantityComponent.razor | 101 ++++++++++++++++-- src/aoWebWallet/aoWebWallet.csproj | 6 -- 7 files changed, 229 insertions(+), 67 deletions(-) create mode 100644 src/aoWebWallet/Shared/ActionEditor.razor diff --git a/src/aoWebWallet/Pages/ActionPage.razor b/src/aoWebWallet/Pages/ActionPage.razor index db72f28..85711a1 100644 --- a/src/aoWebWallet/Pages/ActionPage.razor +++ b/src/aoWebWallet/Pages/ActionPage.razor @@ -49,52 +49,22 @@ - - Target: @AoAction.Target?.Value - - - - @foreach (var value in AoAction.Filled) - { -

@value.Key = @value.Value | @value.ParamType

- @foreach (var arg in value.Args) - { - @arg - } - } - -
- - - @foreach (var param in AoAction.AllInputs) - { - if (param.ParamType == ActionParamType.Input - || param.ParamType == ActionParamType.Integer - || param.ParamType == ActionParamType.Process - ) - { - - } - else if (param.ParamType == ActionParamType.Quantity || param.ParamType == ActionParamType.Balance) - { - var tokenId = param.Args.FirstOrDefault(); - var token = dataService.TokenList.Where(x => x.TokenId == tokenId && x.TokenData != null).FirstOrDefault(); - - if(token != null) - { - //TODO: Quantity, with token and denomination and enough balance check - - } - else { - Loading Token Data... - } - - + @if(readOnly) + { + Please review your transaction: + } - } - } + - + @if (!readOnly) + { + Preview + } + else + { + Cancel + Submit + } @@ -102,6 +72,21 @@ @code { private string? selectedWallet; + private bool readOnly = false; + + private void Preview() + { + readOnly = true; + } + private void Cancel() + { + readOnly = false; + } + + private void Submit() + { + readOnly = false; + } private void OpenDialog() { diff --git a/src/aoWebWallet/Pages/ActionPage.razor.cs b/src/aoWebWallet/Pages/ActionPage.razor.cs index d829305..bacd88d 100644 --- a/src/aoWebWallet/Pages/ActionPage.razor.cs +++ b/src/aoWebWallet/Pages/ActionPage.razor.cs @@ -73,6 +73,8 @@ private async void GetQueryStringValues() actionParamType = ActionParamType.Target; if (key.Equals("X-Quantity", StringComparison.InvariantCultureIgnoreCase)) actionParamType = ActionParamType.Quantity; + if (key.Equals("X-Balance", StringComparison.InvariantCultureIgnoreCase)) + actionParamType = ActionParamType.Balance; else if (key.Equals("X-Process", StringComparison.InvariantCultureIgnoreCase)) actionParamType = ActionParamType.Process; else if (key.Equals("X-Int", StringComparison.InvariantCultureIgnoreCase)) diff --git a/src/aoWebWallet/Services/TokenDataService.cs b/src/aoWebWallet/Services/TokenDataService.cs index 225a428..b64770a 100644 --- a/src/aoWebWallet/Services/TokenDataService.cs +++ b/src/aoWebWallet/Services/TokenDataService.cs @@ -72,10 +72,13 @@ public async Task LoadTokenAsync(string tokenId) return data; }); - token.TokenData = data; + if (data != null) + { - await storageService.SaveTokenList(tokens); + token.TokenData = data; + await storageService.SaveTokenList(tokens); + } } diff --git a/src/aoWebWallet/Shared/ActionEditor.razor b/src/aoWebWallet/Shared/ActionEditor.razor new file mode 100644 index 0000000..fa19e30 --- /dev/null +++ b/src/aoWebWallet/Shared/ActionEditor.razor @@ -0,0 +1,54 @@ +@using aoWebWallet.Models + + + Target: @AoAction.Target?.Value + + + + @foreach (var value in AoAction.Filled) + { +

@value.Key = @value.Value | @value.ParamType

+ @foreach (var arg in value.Args) + { + @arg + } + } + +
+ + + @foreach (var param in AoAction.AllInputs) + { + if (param.ParamType == ActionParamType.Input + || param.ParamType == ActionParamType.Integer + || param.ParamType == ActionParamType.Process + ) + { + + } + else if (param.ParamType == ActionParamType.Quantity || param.ParamType == ActionParamType.Balance) + { + var tokenId = param.Args.FirstOrDefault(); + if (tokenId != null) + { + + } + + + + } + } + + + + +@code { + [Parameter] + public required AoAction AoAction { get; set; } + + [Parameter] + public bool ReadOnly { get; set; } + + [Parameter] + public string? Address { get; set; } +} diff --git a/src/aoWebWallet/Shared/Components/ActionInputComponent.razor b/src/aoWebWallet/Shared/Components/ActionInputComponent.razor index f707bbf..5390175 100644 --- a/src/aoWebWallet/Shared/Components/ActionInputComponent.razor +++ b/src/aoWebWallet/Shared/Components/ActionInputComponent.razor @@ -1,17 +1,24 @@ @using aoWebWallet.Models

@ActionParam.Key = @ActionParam.Value | @ActionParam.ParamType

-@if (ActionParam.ParamType == ActionParamType.Input) +@if(ReadOnly) { - +

@ActionParam.Key = @ActionParam.Value

} -else if (ActionParam.ParamType == ActionParamType.Process) +else { - -} -else if (ActionParam.ParamType == ActionParamType.Integer) -{ - + if (ActionParam.ParamType == ActionParamType.Input) + { + + } + else if (ActionParam.ParamType == ActionParamType.Process) + { + + } + else if (ActionParam.ParamType == ActionParamType.Integer) + { + + } } @@ -20,10 +27,40 @@ else if (ActionParam.ParamType == ActionParamType.Integer) [Parameter] public required ActionParam ActionParam { get; set; } + [Parameter] + public bool ReadOnly { get; set; } + + private string? textValue; + MudTextField? mudTextField; MudTextField? mudProcessField; MudTextField? mudIntField; + // protected override void OnParametersSet() + // { + // mudTextField?.SetText("TESTAAA"); + // Console.WriteLine("Param Set"); + + // base.OnParametersSet(); + // } + + // protected override void OnInitialized() + // { + // mudTextField?.SetText("TESTAAA"); + // Console.WriteLine("Init"); + + // base.OnInitialized(); + // } + + protected override void OnAfterRender(bool firstRender) + { + mudTextField?.SetText(ActionParam.Value); + mudProcessField?.SetText(ActionParam.Value); + mudIntField?.SetText(ActionParam.Value); + + base.OnAfterRender(firstRender); + } + public async void UpdateStringValue(string? e) { if (mudTextField != null) diff --git a/src/aoWebWallet/Shared/Components/ActionQuantityComponent.razor b/src/aoWebWallet/Shared/Components/ActionQuantityComponent.razor index d6c1a9f..3425241 100644 --- a/src/aoWebWallet/Shared/Components/ActionQuantityComponent.razor +++ b/src/aoWebWallet/Shared/Components/ActionQuantityComponent.razor @@ -1,13 +1,42 @@ @using ArweaveAO.Models.Token @using aoWebWallet.Models +@inject TokenDataService tokenDataService +@inject TokenClient tokenClient

@ActionParam.Key = @ActionParam.Value | @ActionParam.ParamType

-@if (ActionParam.ParamType == ActionParamType.Quantity -|| ActionParam.ParamType == ActionParamType.Balance) +@if(Token == null) +{ + Loading token data... + return; +} +@if (ActionParam.ParamType == ActionParamType.Balance && string.IsNullOrEmpty(Address)) +{ + Please select a wallet... + return; +} +@if (ActionParam.ParamType == ActionParamType.Balance && BalanceData == null) { - + Loading balance... + return; } +@if (ReadOnly) +{ +

@ActionParam.Key = @BalanceHelper.FormatBalance(long.Parse(ActionParam.Value ?? "0"), Token?.TokenData?.Denomination ?? 0)

+} +else +{ + if (ActionParam.ParamType == ActionParamType.Quantity +|| ActionParam.ParamType == ActionParamType.Balance) + { + + + if (ActionParam.ParamType == ActionParamType.Balance) + { + Balance available: @BalanceHelper.FormatBalance(BalanceData?.Balance, Token?.TokenData?.Denomination ?? 1) + } + } +} @code { @@ -16,30 +45,88 @@ public required ActionParam ActionParam { get; set; } [Parameter] - public required Token Token { get; set; } + public string? Address { get; set; } [Parameter] + public bool ReadOnly { get; set; } + + [Parameter] + public required string TokenId { get; set; } + + public Token? Token { get; set; } + public BalanceData? BalanceData { get; set; } - public string DenominationFormat => "F" + (Token.TokenData?.Denomination ?? 1).ToString(); + public string DenominationFormat => "F" + (Token?.TokenData?.Denomination ?? 1).ToString(); MudTextField? mudTextField; + protected override void OnAfterRender(bool firstRender) + { + mudTextField?.SetText(ActionParam.Value); + + base.OnAfterRender(firstRender); + } + + protected override async Task OnParametersSetAsync() + { + var token = await tokenDataService.LoadTokenAsync(TokenId); + if (token.TokenData?.Denomination != null) + Token = token; + + if (ActionParam.ParamType == ActionParamType.Balance && !string.IsNullOrEmpty(Address)) + { + BalanceData = await tokenClient.GetBalance(token.TokenId, Address); + } + + base.OnParametersSetAsync(); + } + + public IEnumerable ValidateBalance(decimal e) + { + // if (e == null) + // { + // yield return "Please enter a value."; + // } + + if(e > 0) + { + + if (ActionParam.ParamType == ActionParamType.Balance) + { + if (Token?.TokenData?.Denomination.HasValue ?? false) + { + long amountLong = BalanceHelper.DecimalToTokenAmount(e, Token.TokenData.Denomination.Value); + + if (BalanceData?.Balance < amountLong) + { + yield return "Not enough balance available."; + } + } + else + { + yield return "Token data is not available."; + } + } + } + } + public async void UpdateDecimalValue(decimal e) { if (mudTextField != null) await mudTextField.Validate(); - if (Token.TokenData?.Denomination == null) + if (Token?.TokenData?.Denomination == null) { ActionParam.Value = null; return; } + if (!(mudTextField?.ValidationErrors.Any() ?? false)) { long amountLong = BalanceHelper.DecimalToTokenAmount(e, Token.TokenData.Denomination.Value); - + ActionParam.Value = amountLong.ToString(); } else diff --git a/src/aoWebWallet/aoWebWallet.csproj b/src/aoWebWallet/aoWebWallet.csproj index dc8a86d..a1937b7 100644 --- a/src/aoWebWallet/aoWebWallet.csproj +++ b/src/aoWebWallet/aoWebWallet.csproj @@ -27,10 +27,4 @@ - - - true - - -