Skip to content

Commit

Permalink
Action improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Apr 26, 2024
1 parent ddac9a8 commit 5e409b0
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 100 deletions.
117 changes: 115 additions & 2 deletions src/aoWebWallet/Models/ActionParam.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

using aoWebWallet.Pages;
using System.Net;
using System.Text;

namespace aoWebWallet.Models
{
Expand All @@ -20,6 +19,12 @@ public class AoAction
if (Target == null)
return "No Target process specified.";

foreach(var input in AllInputs)
{
if (string.IsNullOrEmpty(input.Value))
return $"Please enter a value for {input.Key}";
}

return null;
}

Expand All @@ -32,6 +37,114 @@ public class AoAction
{
return AllWithoutTarget.Select(x => new ArweaveBlazor.Models.Tag { Name = x.Key, Value = x.Value ?? string.Empty }).ToList();
}


public string ToQueryString()
{
if (Target == null)
return string.Empty;

StringBuilder sb = new StringBuilder();

sb.Append($"{Target.Key}={Target.Value}&");

foreach (var param in this.Filled)
{
sb.Append($"{param.Key}={param.Value}&");
}

foreach (var param in this.AllInputs)
{
var args = string.Join(';', param.Args);
if (args.Length > 0)
{
sb.Append($"X-{param.ParamType}={param.Key};{args}&");
}
else
{
sb.Append($"X-{param.ParamType}={param.Key}&");
}
}

return sb.ToString().TrimEnd('&');
}

public static AoAction CreateFromQueryString(string qstring)
{
// Parsing query string
var queryStringValues = System.Web.HttpUtility.ParseQueryString(qstring);

AoAction action = new AoAction();

foreach (var key in queryStringValues.AllKeys)
{
if (key == null)
continue;

var values = queryStringValues.GetValues(key);
if (values == null || !values.Any())
continue;

foreach (var val in values)
{
string actionKey = key;
string? actionValue = val.ToString();
ActionParamType actionParamType = ActionParamType.Filled;

var actionValueSplit = actionValue.Split(';', StringSplitOptions.RemoveEmptyEntries);
actionValue = actionValueSplit.First();
List<string> args = actionValueSplit.Skip(1).ToList();

if (key.Equals("Target", StringComparison.InvariantCultureIgnoreCase))
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-Integer", StringComparison.InvariantCultureIgnoreCase))
actionParamType = ActionParamType.Integer;
else if (key.Equals("X-Input", StringComparison.InvariantCultureIgnoreCase))
actionParamType = ActionParamType.Input;

if (actionParamType != ActionParamType.Filled
&& actionParamType != ActionParamType.Target)
{
actionKey = actionValue;
actionValue = null;
}

Console.WriteLine($"Val: {actionValue} args: {args.Count()}");

action.Params.Add(new ActionParam
{
Key = actionKey,
Value = actionValue,
Args = args,
ParamType = actionParamType
});

}
}

return action;
}

public static AoAction CreateForTokenTransaction(string tokenId)
{
return new AoAction
{
Params = new List<ActionParam>
{
new ActionParam { Key= "Target", ParamType = ActionParamType.Target, Value= tokenId },
new ActionParam { Key= "Action", ParamType = ActionParamType.Filled, Value= "Transfer" },
new ActionParam { Key= "Recipient", ParamType = ActionParamType.Process },
new ActionParam { Key= "Quantity", ParamType = ActionParamType.Balance, Args = new List<string> { tokenId } }
}

};
}
}

public class ActionParam
Expand Down
11 changes: 7 additions & 4 deletions src/aoWebWallet/Pages/ActionPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
@inject ISnackbar Snackbar
@inject NavigationManager NavigationManager
@inject TokenDataService dataService
@inject TransactionService transactionService;
@inject WalletDetailViewModel WalletDetailViewModel

<PageTitle>@Program.PageTitlePostFix</PageTitle>


<MudContainer Class="mt-16 px-8" MaxWidth="MaxWidth.False">
<MudText Typo="Typo.h5">Action Page</MudText>
<MudText Typo="Typo.h5">New transaction</MudText>

<MudStack>
TODO: Select a wallet, or create a new wallet

@if (BindingContext.WalletList.Data != null)
{
if (!BindingContext.WalletList.Data.Any())
Expand Down Expand Up @@ -59,6 +59,7 @@
@if (!readOnly)
{
<MudButton OnClick="Preview">Preview</MudButton>
<MudText Color="Color.Error">@validation</MudText>
}
else
{
Expand All @@ -71,12 +72,14 @@

@code
{
private string? validation;
private string? selectedWallet;
private bool readOnly = false;

private void Preview()
{
readOnly = true;
validation = AoAction.IsValid();
readOnly = string.IsNullOrEmpty(validation);
}
private void Cancel()
{
Expand Down
68 changes: 5 additions & 63 deletions src/aoWebWallet/Pages/ActionPage.razor.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
using aoWebWallet.Models;
using aoWebWallet.Services;
using aoWebWallet.ViewModels;
using Microsoft.AspNetCore.Components.Routing;
using System.Net;

namespace aoWebWallet.Pages
{
public partial class ActionPage : MvvmComponentBase<MainViewModel>
{
private readonly TransactionService transactionService;

public AoAction AoAction { get; set; } = new();

public ActionPage(TransactionService transactionService)
{
this.transactionService = transactionService;
}

protected override void OnInitialized()
{
GetQueryStringValues();
//WatchDataLoaderVM(BindingContext.TokenList);
WatchDataLoaderVM(BindingContext.WalletList);

//Auto select wallet
if(!string.IsNullOrEmpty(WalletDetailViewModel.SelectedWallet?.Wallet.Address))
selectedWallet = WalletDetailViewModel.SelectedWallet?.Wallet.Address;

NavigationManager.LocationChanged += NavigationManager_LocationChanged;

base.OnInitialized();
Expand Down Expand Up @@ -52,60 +47,7 @@ private async void GetQueryStringValues()
var uri = new Uri(NavigationManager.Uri);
var query = uri.Query;

// Parsing query string
var queryStringValues = System.Web.HttpUtility.ParseQueryString(query);

AoAction = new AoAction();

foreach (var key in queryStringValues.AllKeys)
{
if (key == null)
continue;

var values = queryStringValues.GetValues(key);
if (values == null || !values.Any())
continue;

foreach(var val in values)
{
string actionKey = key;
string? actionValue = val.ToString();
ActionParamType actionParamType = ActionParamType.Filled;

var actionValueSplit = actionValue.Split(';', StringSplitOptions.RemoveEmptyEntries);
actionValue = actionValueSplit.FirstOrDefault();
List<string> args = actionValueSplit.Skip(1).ToList();

if (key.Equals("Target", StringComparison.InvariantCultureIgnoreCase))
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))
actionParamType = ActionParamType.Integer;
else if (key.Equals("X-Input", StringComparison.InvariantCultureIgnoreCase))
actionParamType = ActionParamType.Input;

if (actionParamType != ActionParamType.Filled
&& actionParamType != ActionParamType.Target)
{
actionKey = val;
actionValue = null;
}

AoAction.Params.Add(new ActionParam
{
Key = actionKey,
Value = actionValue,
Args = args,
ParamType = actionParamType
});

}
}
AoAction = AoAction.CreateFromQueryString(query);

//Add and load tokens
var tokens = AoAction
Expand Down
19 changes: 13 additions & 6 deletions src/aoWebWallet/Pages/WalletDetail.razor
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,19 @@

private void Send(BalanceDataViewModel? balanceDataVM)
{
var parameters = new DialogParameters<SendTokenDialog> {
{ x => x.SelectedBalanceDataVM, balanceDataVM },
{ x => x.SelectedWallet, BindingContext.SelectedWallet }
};
var options = new DialogOptions { CloseOnEscapeKey = true };
DialogService.Show<SendTokenDialog>("Transfer Token", parameters, options);
if(balanceDataVM?.Token == null)
return;

var aoAction = AoAction.CreateForTokenTransaction(balanceDataVM.Token.TokenId);

NavigationManager.NavigateTo($"/action?{aoAction.ToQueryString()}");

// var parameters = new DialogParameters<SendTokenDialog> {
// { x => x.SelectedBalanceDataVM, balanceDataVM },
// { x => x.SelectedWallet, BindingContext.SelectedWallet }
// };
// var options = new DialogOptions { CloseOnEscapeKey = true };
// DialogService.Show<SendTokenDialog>("Transfer Token", parameters, options);
}

private async Task RefreshBalances()
Expand Down
16 changes: 7 additions & 9 deletions src/aoWebWallet/Shared/ActionEditor.razor
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
@using aoWebWallet.Models

<MudStack>
Target: @AoAction.Target?.Value
</MudStack>
<MudItem>
<MudChip>Target</MudChip> <MudChip Color="Color.Primary">@AoAction.Target?.Value</MudChip>
</MudItem>

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

</MudStack>
Expand Down
7 changes: 4 additions & 3 deletions src/aoWebWallet/Shared/Components/ActionInputComponent.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
@using aoWebWallet.Models
<p>@ActionParam.Key = @ActionParam.Value | @ActionParam.ParamType</p>
@* <p>@ActionParam.Key = @ActionParam.Value | @ActionParam.ParamType</p> *@

<MudItem>
@if(ReadOnly)
{
<p>@ActionParam.Key = @ActionParam.Value</p>
<MudChip>@ActionParam.Key</MudChip> <MudChip Color="Color.Primary">@ActionParam.Value</MudChip>
}
else
{
Expand All @@ -20,7 +21,7 @@ else
<MudTextField @ref="mudIntField" T="int" Label="@ActionParam.Key" Variant="Variant.Text" ValueChanged="UpdateIntValue"></MudTextField>
}
}

</MudItem>

@code {

Expand Down
Loading

0 comments on commit 5e409b0

Please sign in to comment.