Skip to content

Commit

Permalink
ActionEditor improvements with TokenData and balance data
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Apr 24, 2024
1 parent b3fe20d commit 1eb0800
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 67 deletions.
73 changes: 29 additions & 44 deletions src/aoWebWallet/Pages/ActionPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -49,59 +49,44 @@

</MudStack>

<MudStack>
Target: @AoAction.Target?.Value
</MudStack>

<MudStack>
@foreach (var value in AoAction.Filled)
{
<p>@value.Key = @value.Value | @value.ParamType</p>
@foreach (var arg in value.Args)
{
<b>@arg</b>
}
}

</MudStack>

<MudStack>
@foreach (var param in AoAction.AllInputs)
{
if (param.ParamType == ActionParamType.Input
|| param.ParamType == ActionParamType.Integer
|| param.ParamType == ActionParamType.Process
)
{
<ActionInputComponent ActionParam="param" />
}
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
<ActionQuantityComponent ActionParam="param" Token="token" />
}
else {
<MudText>Loading Token Data...</MudText>
}


@if(readOnly)
{
<MudText>Please review your transaction:</MudText>
}

}
}
<ActionEditor AoAction="@AoAction" ReadOnly="@readOnly" Address="@selectedWallet" />

</MudStack>
@if (!readOnly)
{
<MudButton OnClick="Preview">Preview</MudButton>
}
else
{
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton OnClick="Submit">Submit</MudButton>
}


</MudContainer>

@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()
{
Expand Down
2 changes: 2 additions & 0 deletions src/aoWebWallet/Pages/ActionPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 5 additions & 2 deletions src/aoWebWallet/Services/TokenDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ public async Task<Token> LoadTokenAsync(string tokenId)
return data;
});

token.TokenData = data;
if (data != null)
{

await storageService.SaveTokenList(tokens);
token.TokenData = data;

await storageService.SaveTokenList(tokens);
}

}

Expand Down
54 changes: 54 additions & 0 deletions src/aoWebWallet/Shared/ActionEditor.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
@using aoWebWallet.Models

<MudStack>
Target: @AoAction.Target?.Value
</MudStack>

<MudStack>
@foreach (var value in AoAction.Filled)
{
<p>@value.Key = @value.Value | @value.ParamType</p>
@foreach (var arg in value.Args)
{
<b>@arg</b>
}
}

</MudStack>

<MudStack>
@foreach (var param in AoAction.AllInputs)
{
if (param.ParamType == ActionParamType.Input
|| param.ParamType == ActionParamType.Integer
|| param.ParamType == ActionParamType.Process
)
{
<ActionInputComponent ActionParam="param" ReadOnly="@ReadOnly" />
}
else if (param.ParamType == ActionParamType.Quantity || param.ParamType == ActionParamType.Balance)
{
var tokenId = param.Args.FirstOrDefault();
if (tokenId != null)
{
<ActionQuantityComponent ActionParam="param" TokenId="@tokenId" Address="@Address" ReadOnly="@ReadOnly" />
}



}
}

</MudStack>


@code {
[Parameter]
public required AoAction AoAction { get; set; }

[Parameter]
public bool ReadOnly { get; set; }

[Parameter]
public string? Address { get; set; }
}
53 changes: 45 additions & 8 deletions src/aoWebWallet/Shared/Components/ActionInputComponent.razor
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
@using aoWebWallet.Models
<p>@ActionParam.Key = @ActionParam.Value | @ActionParam.ParamType</p>

@if (ActionParam.ParamType == ActionParamType.Input)
@if(ReadOnly)
{
<MudTextField @ref="mudTextField" T="string" Label="@ActionParam.Key" Variant="Variant.Text" ValueChanged="UpdateStringValue"></MudTextField>
<p>@ActionParam.Key = @ActionParam.Value</p>
}
else if (ActionParam.ParamType == ActionParamType.Process)
else
{
<MudTextField @ref="mudProcessField" T="string" MaxLength=43 Label="@ActionParam.Key" Validation="@(new Func<string, IEnumerable<string>>(ValidateProcess))" Variant="Variant.Text" ValueChanged="UpdateStringValue" />
}
else if (ActionParam.ParamType == ActionParamType.Integer)
{
<MudTextField @ref="mudIntField" T="int" Label="@ActionParam.Key" Variant="Variant.Text" ValueChanged="UpdateIntValue"></MudTextField>
if (ActionParam.ParamType == ActionParamType.Input)
{
<MudTextField @ref="mudTextField" T="string" Label="@ActionParam.Key" Variant="Variant.Text" ValueChanged="UpdateStringValue"></MudTextField>
}
else if (ActionParam.ParamType == ActionParamType.Process)
{
<MudTextField @ref="mudProcessField" T="string" MaxLength=43 Label="@ActionParam.Key" Validation="@(new Func<string, IEnumerable<string>>(ValidateProcess))" Variant="Variant.Text" ValueChanged="UpdateStringValue" />
}
else if (ActionParam.ParamType == ActionParamType.Integer)
{
<MudTextField @ref="mudIntField" T="int" Label="@ActionParam.Key" Variant="Variant.Text" ValueChanged="UpdateIntValue"></MudTextField>
}
}


Expand All @@ -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<string>? mudTextField;
MudTextField<string>? mudProcessField;
MudTextField<int>? 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)
Expand Down
101 changes: 94 additions & 7 deletions src/aoWebWallet/Shared/Components/ActionQuantityComponent.razor
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
@using ArweaveAO.Models.Token
@using aoWebWallet.Models
@inject TokenDataService tokenDataService
@inject TokenClient tokenClient
<p>@ActionParam.Key = @ActionParam.Value | @ActionParam.ParamType</p>

@if (ActionParam.ParamType == ActionParamType.Quantity
|| ActionParam.ParamType == ActionParamType.Balance)
@if(Token == null)
{
<MudText>Loading token data...</MudText>
return;
}
@if (ActionParam.ParamType == ActionParamType.Balance && string.IsNullOrEmpty(Address))
{
<MudText>Please select a wallet...</MudText>
return;
}
@if (ActionParam.ParamType == ActionParamType.Balance && BalanceData == null)
{
<MudTextField @ref="mudTextField" T="decimal" Label="@ActionParam.Key" Variant="Variant.Text" ValueChanged="UpdateDecimalValue" Format="@DenominationFormat"></MudTextField>
<MudText>Loading balance...</MudText>
return;
}

@if (ReadOnly)
{
<p>@ActionParam.Key = @BalanceHelper.FormatBalance(long.Parse(ActionParam.Value ?? "0"), Token?.TokenData?.Denomination ?? 0)</p>
}
else
{
if (ActionParam.ParamType == ActionParamType.Quantity
|| ActionParam.ParamType == ActionParamType.Balance)
{
<MudTextField @ref="mudTextField" T="decimal" Label="@ActionParam.Key" Variant="Variant.Text" ValueChanged="UpdateDecimalValue" Format="@DenominationFormat" Validation="@(new Func<decimal, IEnumerable<string>>(ValidateBalance))"></MudTextField>

if (ActionParam.ParamType == ActionParamType.Balance)
{
<MudText>Balance available: @BalanceHelper.FormatBalance(BalanceData?.Balance, Token?.TokenData?.Denomination ?? 1) </MudText>
}
}
}


@code {
Expand All @@ -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<decimal>? 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<string> 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
Expand Down
6 changes: 0 additions & 6 deletions src/aoWebWallet/aoWebWallet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,4 @@
<ProjectReference Include="..\webvNext.DataLoader\webvNext.DataLoader.csproj" />
</ItemGroup>

<ItemGroup>
<Content Update="Shared\Components\ActionQuantityComponent.razor">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>

</Project>

0 comments on commit 1eb0800

Please sign in to comment.