diff --git a/src/routes/admin.rs b/src/routes/admin.rs index d79e96e..96f75b1 100644 --- a/src/routes/admin.rs +++ b/src/routes/admin.rs @@ -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>> for Contexts<'a, 'b> { - fn from(value: &'a Form>) -> 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()) diff --git a/templates/admin/index.html.tera b/templates/admin/index.html.tera index 010c3b0..e2f1b2e 100644 --- a/templates/admin/index.html.tera +++ b/templates/admin/index.html.tera @@ -1,4 +1,5 @@ {% extends "base" %} +{% import "macros/form" as form %} {% block title %}Admin panel - {% endblock title %} {% block main %}