Skip to content

Commit

Permalink
Added ActionQuantityComponent.razor
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Apr 17, 2024
1 parent a2fc7da commit bd116f0
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 12 deletions.
15 changes: 14 additions & 1 deletion src/aoWebWallet/Pages/ActionPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,20 @@
}
else if (param.ParamType == ActionParamType.Quantity || param.ParamType == ActionParamType.Balance)
{
//TODO: Quantity, with token and denomination and enough balance check
var tokenId = param.Args.FirstOrDefault();
var token = BindingContext.TokenList.Data?.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>
}



}
}

Expand Down
12 changes: 11 additions & 1 deletion src/aoWebWallet/Pages/ActionPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void NavigationManager_LocationChanged(object? sender, LocationChangedEv
StateHasChanged();
}

void GetQueryStringValues()
private async void GetQueryStringValues()
{
var uri = new Uri(NavigationManager.Uri);
var query = uri.Query;
Expand Down Expand Up @@ -98,6 +98,16 @@ void GetQueryStringValues()
}
}

//Add and load tokens
var tokens = AoAction
.AllInputs
.Where(x => x.ParamType == ActionParamType.Balance || x.ParamType == ActionParamType.Quantity)
.Select(x => x.Args.FirstOrDefault())
.Distinct()
.ToList();

await BindingContext.TryAddTokenIds(tokens);

StateHasChanged();
}

Expand Down
22 changes: 20 additions & 2 deletions src/aoWebWallet/Services/StorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,25 @@ private void AddSystemToken(List<Token> list, string tokenId)
list.Add(new Token { TokenId = tokenId, IsSystemToken = true });
}

public async ValueTask<Token> AddToken(string tokenId, TokenData data, bool isUserAdded)
public async Task AddTokenId(string tokenId, bool isUserAdded = true, bool isVisible = false)
{
if(tokenId.Length != 43)
return;

var list = await GetTokenIds();

var existing = list.Where(x => x.TokenId == tokenId).FirstOrDefault();
if (existing != null)
return;


existing = new Token { TokenId = tokenId, IsUserAdded = isUserAdded, IsVisible = isVisible };
list.Add(existing);

await SaveTokenList(list);
}

public async ValueTask<Token> AddToken(string tokenId, TokenData data, bool isUserAdded, bool isVisible = true)
{
var list = await GetTokenIds();

Expand All @@ -54,7 +72,7 @@ public async ValueTask<Token> AddToken(string tokenId, TokenData data, bool isUs
}
else
{
existing = new Token { TokenId = tokenId, TokenData = data, IsUserAdded = isUserAdded };
existing = new Token { TokenId = tokenId, TokenData = data, IsUserAdded = isUserAdded, IsVisible = isVisible };
list.Add(existing);
}

Expand Down
9 changes: 3 additions & 6 deletions src/aoWebWallet/Shared/Components/ActionInputComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ else if (ActionParam.ParamType == ActionParamType.Integer)
MudTextField<string>? mudProcessField;
MudTextField<int>? mudIntField;

public string? stringValue = null;
public int? intValue = null;

public async void UpdateStringValue(string e)
public async void UpdateStringValue(string? e)
{
if (mudTextField != null)
await mudTextField.Validate();
Expand All @@ -45,9 +42,9 @@ else if (ActionParam.ParamType == ActionParamType.Integer)
StateHasChanged();
}

public IEnumerable<string> ValidateProcess(string input)
public IEnumerable<string> ValidateProcess(string? input)
{
if (input.Length != 43)
if (input == null || input.Length != 43)
{
Console.WriteLine("Invalid");

Expand Down
51 changes: 51 additions & 0 deletions src/aoWebWallet/Shared/Components/ActionQuantityComponent.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@using ArweaveAO.Models.Token
@using aoWebWallet.Models
<p>@ActionParam.Key = @ActionParam.Value | @ActionParam.ParamType</p>

@if (ActionParam.ParamType == ActionParamType.Quantity
|| ActionParam.ParamType == ActionParamType.Balance)
{
<MudTextField @ref="mudTextField" T="decimal" Label="@ActionParam.Key" Variant="Variant.Text" ValueChanged="UpdateDecimalValue" Format="@DenominationFormat"></MudTextField>
}



@code {

[Parameter]
public required ActionParam ActionParam { get; set; }

[Parameter]
public required Token Token { get; set; }

[Parameter]
public BalanceData? BalanceData { get; set; }

public string DenominationFormat => "F" + (Token.TokenData?.Denomination ?? 1).ToString();

MudTextField<decimal>? mudTextField;

public async void UpdateDecimalValue(decimal e)
{
if (mudTextField != null)
await mudTextField.Validate();

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
ActionParam.Value = null;

StateHasChanged();
}

}
5 changes: 3 additions & 2 deletions src/aoWebWallet/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,11 @@ public async Task ClearUserData()
BalanceDataList.Data = null;
}

private async Task TryAddTokenIds(List<string?> allTokenIds)
public async Task TryAddTokenIds(List<string?> allTokenIds)
{
foreach (var tokenId in allTokenIds)
{
if (string.IsNullOrEmpty(tokenId))
if (string.IsNullOrEmpty(tokenId) || tokenId.Length != 43)
continue;

var exist = TokenList.Data?.Where(x => x.TokenId == tokenId).Any() ?? false;
Expand Down Expand Up @@ -800,5 +800,6 @@ public async Task SetIsDarkMode(bool isDarkMode)
await SaveUserSettings();
}
}

}
}
6 changes: 6 additions & 0 deletions src/aoWebWallet/aoWebWallet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@
<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 bd116f0

Please sign in to comment.