Files
btcpayserver/BTCPayServer/TagHelpers/CurrenciesSuggestionsTagHelper.cs
d11n d5d0be5824 Code formatting updates (#4502)
* Editorconfig: Add space_before_self_closing setting

This was a difference between the way dotnet-format and Rider format code. See https://www.jetbrains.com/help/rider/EditorConfig_Index.html

* Editorconfig: Keep 4 spaces indentation for Swagger JSON files

They are all formatted that way, let's keep it like that.

* Apply dotnet-format, mostly white-space related changes
2023-01-06 22:18:07 +09:00

52 lines
2.0 KiB
C#

using System.Collections.Generic;
using System.Linq;
using BTCPayServer.Services.Rates;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace BTCPayServer.TagHelpers
{
[HtmlTargetElement("input", Attributes = "currency-selection")]
public class CurrenciesSuggestionsTagHelper : TagHelper
{
private readonly CurrencyNameTable _currencies;
public CurrenciesSuggestionsTagHelper(CurrencyNameTable currencies)
{
_currencies = currencies;
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.Attributes.RemoveAll("currency-selection");
output.PostElement.AppendHtml("<datalist id=\"currency-selection-suggestion\">");
var currencies = _currencies.Currencies
.Where(c => !c.Crypto)
.OrderBy(c => c.Code).ToList();
// insert btc at the front
output.PostElement.AppendHtml("<option value=\"BTC\">BTC - Bitcoin</option>");
output.PostElement.AppendHtml("<option value=\"SATS\">SATS - Satoshi</option>");
// move most often used currencies up
int pos = 0;
InsertAt(currencies, "USD", pos++);
InsertAt(currencies, "EUR", pos++);
InsertAt(currencies, "JPY", pos++);
InsertAt(currencies, "CNY", pos++);
// add options
foreach (var c in currencies)
{
output.PostElement.AppendHtml($"<option value=\"{c.Code}\">{c.Code} - {c.Name}</option>");
}
output.PostElement.AppendHtml("</datalist>");
output.Attributes.Add("list", "currency-selection-suggestion");
base.Process(context, output);
}
private void InsertAt(List<CurrencyData> currencies, string code, int idx)
{
var curr = currencies.FirstOrDefault(c => c.Code == code);
currencies.Remove(curr);
currencies.Insert(idx, curr);
}
}
}