175 lines
7.1 KiB
Text
175 lines
7.1 KiB
Text
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
export module Nostro.App:Forms;
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import Nostro;
|
||
|
|
import Nostro.Ui;
|
||
|
|
|
||
|
|
// What the user fills in, and what it is read as. A form keeps its fields as they were typed and is checked as a
|
||
|
|
// whole: each field's error is a line for under that field, and a form without errors gives an order, the values
|
||
|
|
// nostrod is asked with. Nothing here talks to nostrod; nostrod checks everything again. The forms a payment, a
|
||
|
|
// request and a filter are filled in on are any bank's; what a bank asks for to be connected is its own, and so is
|
||
|
|
// the form for it, named for its bank.
|
||
|
|
|
||
|
|
export namespace Nostro::App {
|
||
|
|
// What a field's text was read as, or why it could not be.
|
||
|
|
template<typename T>
|
||
|
|
struct Read {
|
||
|
|
std::optional<T> value;
|
||
|
|
std::string error;
|
||
|
|
};
|
||
|
|
|
||
|
|
// An amount as a person writes it, "12,50", "12.50" or "12", in the currency's smallest unit; above zero.
|
||
|
|
Read<std::int64_t> ReadAmount(std::string_view text, Currency currency);
|
||
|
|
// Who is paid or asked: an IBAN, an e-mail address or a phone number starting with +.
|
||
|
|
Read<BunqCounterparty> ReadParty(std::string_view text);
|
||
|
|
// What kind of address that is, in words, for beside the field while it is typed; empty for none yet.
|
||
|
|
std::string PartyKind(std::string_view text);
|
||
|
|
// A day "YYYY-MM-DD" in local time, with a time "hh:mm" on it when one is given; the day's last moment when
|
||
|
|
// `end` and no time is given.
|
||
|
|
Read<std::chrono::system_clock::time_point> ReadDay(std::string_view day, std::string_view time = {}, bool end = false);
|
||
|
|
// A whole number above zero.
|
||
|
|
Read<std::uint32_t> ReadCount(std::string_view text);
|
||
|
|
|
||
|
|
// How often a scheduled payment runs, as the form offers it and as nostrod is told it.
|
||
|
|
struct ScheduleUnit {
|
||
|
|
std::string_view label;
|
||
|
|
std::string_view unit;
|
||
|
|
};
|
||
|
|
inline constexpr std::array<ScheduleUnit, 6> scheduleUnits = {{
|
||
|
|
{"Once", "ONCE"}, {"Hourly", "HOURLY"}, {"Daily", "DAILY"}, {"Weekly", "WEEKLY"}, {"Monthly", "MONTHLY"}, {"Yearly", "YEARLY"},
|
||
|
|
}};
|
||
|
|
|
||
|
|
enum class When : std::uint8_t {
|
||
|
|
Now,
|
||
|
|
Scheduled,
|
||
|
|
Draft // made now, paid once someone accepts it
|
||
|
|
};
|
||
|
|
|
||
|
|
// A payment as nostrod is asked for it.
|
||
|
|
struct PaymentOrder {
|
||
|
|
BunqCounterparty to;
|
||
|
|
std::int64_t amount = 0;
|
||
|
|
std::string description;
|
||
|
|
When when = When::Now;
|
||
|
|
BunqSchedule schedule; // for When::Scheduled
|
||
|
|
};
|
||
|
|
|
||
|
|
struct PaymentForm {
|
||
|
|
Ui::EditState to{Ui::FieldKind::Plain};
|
||
|
|
Ui::EditState name{Ui::FieldKind::Text};
|
||
|
|
Ui::EditState amount{Ui::FieldKind::Decimal};
|
||
|
|
Ui::EditState description{Ui::FieldKind::Text};
|
||
|
|
When when = When::Now;
|
||
|
|
Ui::EditState day{Ui::FieldKind::Plain};
|
||
|
|
Ui::EditState time{Ui::FieldKind::Plain};
|
||
|
|
std::size_t unit = 0; // of scheduleUnits
|
||
|
|
Ui::EditState every{Ui::FieldKind::Digits};
|
||
|
|
Ui::EditState until{Ui::FieldKind::Plain};
|
||
|
|
// Errors show once the user has tried to go on: an empty form is not a list of mistakes.
|
||
|
|
bool tried = false;
|
||
|
|
|
||
|
|
struct Errors {
|
||
|
|
std::string to;
|
||
|
|
std::string name;
|
||
|
|
std::string amount;
|
||
|
|
std::string day;
|
||
|
|
std::string time;
|
||
|
|
std::string every;
|
||
|
|
std::string until;
|
||
|
|
bool Any() const noexcept;
|
||
|
|
};
|
||
|
|
// Every field's error, and into `order` what the form asks for when there is none.
|
||
|
|
Errors Check(Currency currency, std::chrono::system_clock::time_point now, PaymentOrder& order) const;
|
||
|
|
void Clear();
|
||
|
|
};
|
||
|
|
|
||
|
|
// A request for money as nostrod is asked for it.
|
||
|
|
struct RequestOrder {
|
||
|
|
BunqCounterparty from;
|
||
|
|
std::int64_t amount = 0;
|
||
|
|
std::string description;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct RequestForm {
|
||
|
|
Ui::EditState from{Ui::FieldKind::Plain};
|
||
|
|
Ui::EditState name{Ui::FieldKind::Text};
|
||
|
|
Ui::EditState amount{Ui::FieldKind::Decimal};
|
||
|
|
Ui::EditState description{Ui::FieldKind::Text};
|
||
|
|
bool tried = false;
|
||
|
|
|
||
|
|
struct Errors {
|
||
|
|
std::string from;
|
||
|
|
std::string name;
|
||
|
|
std::string amount;
|
||
|
|
bool Any() const noexcept;
|
||
|
|
};
|
||
|
|
Errors Check(Currency currency, RequestOrder& order) const;
|
||
|
|
void Clear();
|
||
|
|
};
|
||
|
|
|
||
|
|
// Which of an account's payments are listed.
|
||
|
|
struct FilterForm {
|
||
|
|
Ui::EditState search{Ui::FieldKind::Text};
|
||
|
|
Ui::EditState amount{Ui::FieldKind::Decimal};
|
||
|
|
bool received = false; // the amount came in; it went out otherwise
|
||
|
|
Ui::EditState from{Ui::FieldKind::Plain};
|
||
|
|
Ui::EditState to{Ui::FieldKind::Plain};
|
||
|
|
|
||
|
|
struct Errors {
|
||
|
|
std::string amount;
|
||
|
|
std::string from;
|
||
|
|
std::string to;
|
||
|
|
bool Any() const noexcept;
|
||
|
|
};
|
||
|
|
// The errors, and into `query` what the form asks for when there is none; its limit is left as it is.
|
||
|
|
Errors Check(Currency currency, TransactionQuery& query) const;
|
||
|
|
// What is asked for besides the search, each as a few words: "Paid €12.50", "From 3 Nov".
|
||
|
|
std::vector<std::string> Chips(Currency currency, std::chrono::system_clock::time_point now) const;
|
||
|
|
// Empties everything but the search.
|
||
|
|
void Reset();
|
||
|
|
};
|
||
|
|
|
||
|
|
// A connection to bunq made with an API key. The key says itself which of bunq's environments it is of, by how it
|
||
|
|
// starts, so there is none to choose: nostrod takes the key's own.
|
||
|
|
struct BunqKeyForm {
|
||
|
|
Ui::EditState key{Ui::FieldKind::Secret};
|
||
|
|
Ui::EditState ips{Ui::FieldKind::Plain};
|
||
|
|
bool advanced = false; // the allowed addresses are shown
|
||
|
|
bool tried = false;
|
||
|
|
|
||
|
|
BunqKeyForm();
|
||
|
|
// Why the form cannot be sent; empty when it can.
|
||
|
|
std::string Check() const;
|
||
|
|
void Clear();
|
||
|
|
};
|
||
|
|
|
||
|
|
// A connection to bunq made through OAuth: the client's registration first, then the address the browser ends
|
||
|
|
// up at.
|
||
|
|
struct BunqOAuthForm {
|
||
|
|
Ui::EditState clientId{Ui::FieldKind::Plain};
|
||
|
|
Ui::EditState redirectUri{Ui::FieldKind::Plain};
|
||
|
|
Ui::EditState secret{Ui::FieldKind::Secret};
|
||
|
|
bool sandbox = false;
|
||
|
|
Ui::EditState redirected{Ui::FieldKind::Plain};
|
||
|
|
// Ties the browser's answer to this request; made when the address to open is.
|
||
|
|
std::string state;
|
||
|
|
std::string address; // where the user approves, once the first step is filled in
|
||
|
|
bool tried = false;
|
||
|
|
|
||
|
|
struct Errors {
|
||
|
|
std::string clientId;
|
||
|
|
std::string redirectUri;
|
||
|
|
std::string secret;
|
||
|
|
bool Any() const noexcept;
|
||
|
|
};
|
||
|
|
Errors Check() const;
|
||
|
|
// Makes the address to open, with a new state. The form must be without errors.
|
||
|
|
void Begin(std::string newState);
|
||
|
|
// The code in the address the browser was sent to; throws what BunqConnector::OAuthCodeFromRedirect does.
|
||
|
|
std::string Code() const;
|
||
|
|
void Clear();
|
||
|
|
};
|
||
|
|
}
|