cleaned up multi-form context/error mgmt and did errors on the form

This commit is contained in:
Artemis 2025-02-13 17:52:14 +01:00
parent 1ebe51124a
commit 08aa16f36b
2 changed files with 39 additions and 18 deletions

View file

@ -37,7 +37,6 @@ pub async fn index(
meta: meta.for_user(&user.0),
service_status,
previous: context! {
form: "",
tag_handover: Context::default(),
},
toast,
@ -58,20 +57,16 @@ pub struct Forms<'a> {
pub tag_handover: Contextual<'a, TagHandover<'a>>,
}
#[derive(Debug, Serialize)]
#[serde(crate = "rocket::serde")]
pub struct Contexts<'a, 'b> {
pub form: &'a SelectedForm,
pub tag_handover: &'a Context<'b>,
}
impl<'a, 'b> From<&'a Form<Forms<'b>>> for Contexts<'a, 'b> {
fn from(value: &'a Form<Forms<'b>>) -> Self {
Contexts {
form: &value.form,
tag_handover: &value.tag_handover.context,
/// extracts one of the forms' contexts from the form group,
/// otherwise uses the $def value (usually you'll give [`&Context::default()`][`Context::default()`])
macro_rules! get_for_form {
($def:expr, $curr:expr, $target:pat, $value:expr) => {
if matches!($curr, $target) {
$value
} else {
$def
}
}
};
}
#[derive(Debug, FromForm, Clone)]
@ -154,13 +149,20 @@ pub async fn handle_in_page_forms(
let service_status = admin::get_service_status(&mut trx).await?;
trx.commit().await?;
let previous = Contexts::from(&form);
let def = Context::default();
Ok(Template::render(
"admin/index",
context! {
meta: meta.for_user(&user.0),
service_status,
previous,
previous: context! {
tag_handover: get_for_form!(
&def,
&form.form,
SelectedForm::TagHandover,
&form.tag_handover.context
),
},
},
)
.into())

View file

@ -1,4 +1,5 @@
{% extends "base" %}
{% import "macros/form" as form %}
{% block title %}Admin panel - {% endblock title %}
{% block main %}
<aside>
@ -30,12 +31,25 @@
</section>
<section>
{% set ctx = previous.tag_handover %}
<h2>Tag handover</h2>
<p>
To begin handover of a tag, enter the tag's ID and destination account nickname below.<br />
It will let you confirm the tag's account holder and the destination account before executing the handover.
</p>
{% if ctx.form_errors | length > 0 %}
<div class="form-error">
<h3>Some errors were encountered...</h3>
<ul>
{% for err in ctx.form_errors %}
<li>{{err}}</li>
{% endfor %}
</ul>
</div>
{% endif %}
<form method="post">
<input type="hidden" name="form" value="TagHandover" />
<div class="fields raised">
@ -43,12 +57,17 @@
<div>
<p class="ident center">ID of the tag to hand over</p>
<input type="text" name="tag_handover.tag_id" id="tag_id" placeholder="000000" minlength="6"
maxlength="6" required />
maxlength="6" required {{form::value(ctx=ctx, name="tag_handover.tag_id" )}} />
{{form::error(ctx=ctx, name="tag_handover.tag_id")}}
</div>
<div>
<p class="ident center">Destination account username</p>
<input type="text" name="tag_handover.dest_account" id="dest_account" autocomplete="off" required />
<input type="text" name="tag_handover.dest_account" id="dest_account" autocomplete="off" required
{{form::value(ctx=ctx, name="tag_handover.dest_account" )}} />
{{form::error(ctx=ctx, name="tag_handover.dest_account")}}
</div>
</div>