Skip to content

Commit

Permalink
Make predefined forms usable statically
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukks committed Oct 30, 2022
1 parent f4441f2 commit 0df7d90
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BTCPayServer.Abstractions.Form;

public class TextField : Field
public class HtmlInputField : Field
{


Expand All @@ -15,15 +15,15 @@ public class TextField : Field
public string HelpText;

public bool Required = false;
public TextField(string label, string name, string value, bool required, string helpText)
public HtmlInputField(string label, string name, string value, bool required, string helpText, string type = "text")
{
this.Label = label;
this.Name = name;
this.Value = value;
this.OriginalValue = value;
this.Required = required;
this.HelpText = helpText;
this.Type = "text";
this.Type = type;
}

// TODO JSON parsing from string to objects again probably won't work out of the box because of the different field types.
Expand Down
24 changes: 22 additions & 2 deletions BTCPayServer/Forms/FormDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<List<FormData>> GetForms(FormQuery query)
await using var context = _applicationDbContextFactory.CreateContext();
return await GetForms(query, context);
}

private async Task<List<FormData>> GetForms(FormQuery query, ApplicationDbContext context)
{
var queryable = context.Forms.AsQueryable();
Expand Down Expand Up @@ -58,8 +58,28 @@ public async Task RemoveForm(string id, string storeId)
public async Task AddOrUpdateForm(FormData data)
{
await using var context = _applicationDbContextFactory.CreateContext();

context.Update(data);
await context.SaveChangesAsync();
}

public static readonly Form StaticFormEmail = new()
{
Fields = new List<Field>() {new HtmlInputField("Enter your email", "buyerEmail", null, true, null)}
};

public static readonly Form StaticFormAddress = new()
{
Fields = new List<Field>()
{
new HtmlInputField("Enter your email", "buyerEmail", null, true, null, "email"),
new HtmlInputField("Name", "buyerName", null, true, null),
new HtmlInputField("Address Line 1", "buyerAddress1", null, true, null),
new HtmlInputField("Address Line 2", "buyerAddress2", null, false, null),
new HtmlInputField("City", "buyerCity", null, true, null),
new HtmlInputField("Postcode", "buyerZip", null, false, null),
new HtmlInputField("State", "buyerState", null, false, null),
new HtmlInputField("Country", "buyerCountry", null, true, null)
}
};
}
4 changes: 2 additions & 2 deletions BTCPayServer/Views/Shared/Forms/InputElement.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
@using Newtonsoft.Json.Linq
@model BTCPayServer.Abstractions.Form.Field
@{
if (Model is not TextField field)
if (Model is not HtmlInputField field)
{
field = JObject.FromObject(Model).ToObject<TextField>();
field = JObject.FromObject(Model).ToObject<HtmlInputField>();
}
}
<div class="form-group">
Expand Down
101 changes: 11 additions & 90 deletions BTCPayServer/Views/UIForms/Modify.cshtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@using BTCPayServer.Forms
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using BTCPayServer.Abstractions.TagHelpers
@using Newtonsoft.Json
@model BTCPayServer.Forms.ModifyForm

@{
Expand All @@ -6,7 +10,7 @@
Layout = "../Shared/_NavLayout.cshtml";
ViewData["NavPartialName"] = "../UIStores/_Nav";
ViewData.SetActivePage(StoreNavPages.Forms, $"{(isNew ? "Create" : "Edit")} Form", Model.Name);

var storeId = Context.GetCurrentStoreId();
}

Expand All @@ -25,98 +29,16 @@
}

<template id="form-template-email">
{
"Fields": [
@Json.Serialize(FormDataService.StaticFormEmail, new JsonSerializerSettings()
{
"Name": "buyerEmail",
"Type": "email",
"Label": "Enter your email",
"Value": null,
"HelpText": null,
"Required": true,
"OriginalValue": null,
"ValidationErrors": []
}
]
}
Formatting = Formatting.Indented
})
</template>
<template id="form-template-address">
{
"Fields": [
{
"HelpText": null,
"Label": "Enter your email",
"Name": "buyerEmail",
"OriginalValue": null,
"Required": true,
"Type": "email",
"Value": null
},
{
"HelpText": null,
"Label": "Name",
"Name": "buyerName",
"OriginalValue": null,
"Required": true,
"Type": "text",
"Value": null
},
{
"HelpText": null,
"Label": "Address Line 1",
"Name": "buyerAddress1",
"OriginalValue": null,
"Required": true,
"Type": "text",
"Value": null
},
{
"HelpText": null,
"Label": "Address Line 2",
"Name": "buyerAddress2",
"OriginalValue": null,
"Required": false,
"Type": "text",
"Value": null
},
@Json.Serialize(FormDataService.StaticFormAddress, new JsonSerializerSettings()
{
"HelpText": null,
"Label": "City",
"Name": "buyerCity",
"OriginalValue": null,
"Required": true,
"Type": "text",
"Value": null
},
{
"HelpText": null,
"Label": "Postcode",
"Name": "buyerZip",
"OriginalValue": null,
"Required": false,
"Type": "text",
"Value": null
},
{
"HelpText": null,
"Label": "State",
"Name": "buyerState",
"OriginalValue": null,
"Required": false,
"Type": "text",
"Value": null
},
{
"HelpText": null,
"Label": "Country",
"Name": "buyerCountry",
"OriginalValue": null,
"Required": true,
"Type": "text",
"Value": null
}
]
}
Formatting = Formatting.Indented
})
</template>

<form method="post" asp-action="Modify" asp-route-id="@formId" asp-route-storeId="@storeId">
Expand Down Expand Up @@ -153,4 +75,3 @@
</div>
</div>
</form>

Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public Task<Form> GetConfigForm(JObject config, string locale, CancellationToken
var fieldset = new Fieldset();

// Maybe a decimal type field would be better?
var fakeBTCBalance = new TextField("BTC Balance", "BTCBalance", fakeConfig?.BTCBalance.ToString(), true,
var fakeBTCBalance = new HtmlInputField("BTC Balance", "BTCBalance", fakeConfig?.BTCBalance.ToString(), true,
"Enter the amount of BTC you want to have.");
var fakeLTCBalance = new TextField("LTC Balance", "LTCBalance", fakeConfig?.LTCBalance.ToString(), true,
var fakeLTCBalance = new HtmlInputField("LTC Balance", "LTCBalance", fakeConfig?.LTCBalance.ToString(), true,
"Enter the amount of LTC you want to have.");
var fakeEURBalance = new TextField("EUR Balance", "EURBalance", fakeConfig?.EURBalance.ToString(), true,
var fakeEURBalance = new HtmlInputField("EUR Balance", "EURBalance", fakeConfig?.EURBalance.ToString(), true,
"Enter the amount of EUR you want to have.");
var fakeUSDBalance = new TextField("USD Balance", "USDBalance", fakeConfig?.USDBalance.ToString(), true,
var fakeUSDBalance = new HtmlInputField("USD Balance", "USDBalance", fakeConfig?.USDBalance.ToString(), true,
"Enter the amount of USD you want to have.");

fieldset.Label = "Your fake balances";
Expand Down

0 comments on commit 0df7d90

Please sign in to comment.