diff --git a/src/main.rs b/src/main.rs index 876853c..a194c4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,7 +84,7 @@ fn rocket() -> _ { account::export_data, ], ) - .mount("/admin", routes![admin::index,]) + .mount("/admin", routes![admin::index, admin::handle_in_page_forms]) .mount( "/", routes![ diff --git a/src/pages.rs b/src/pages.rs index 8f6c8e7..11e89e3 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -71,7 +71,7 @@ impl<'r> FromRequest<'r> for CommonTemplateState { /// FakeContext exists to be used as a replacement for Context when reusing a form for creation and edition, to avoid repeating lots of code. /// /// Note: i made this custom context thingy because i couldn't find a way to create a real context with the right populated data. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Default)] #[serde(crate = "rocket::serde")] pub struct FakeContext { /// Values are made to simulate the same structure as the real Context diff --git a/src/routes/admin.rs b/src/routes/admin.rs index 33c3679..a79e4b3 100644 --- a/src/routes/admin.rs +++ b/src/routes/admin.rs @@ -1,3 +1,7 @@ +use rocket::{ + form::{Context, Contextual, Form}, + serde::Serialize, +}; use rocket_dyn_templates::{context, Template}; use sqlx::Acquire; @@ -9,14 +13,6 @@ use crate::{ use super::error_handlers::PageResult; -#[derive(FromForm)] -pub struct TagHandover<'a> { - #[field(validate=crate::ids::validate_id())] - pub tag_id: &'a str, - #[field(validate=len(..=256))] - pub dest_account: &'a str, -} - #[get("/")] pub async fn index(meta: CommonTemplateState, mut db: DollTagsDb, user: Admin) -> PageResult { let mut trx = db.begin().await?; @@ -32,3 +28,76 @@ pub async fn index(meta: CommonTemplateState, mut db: DollTagsDb, user: Admin) - ) .into()) } + +#[derive(Debug, FromFormField, Serialize)] +#[serde(crate = "rocket::serde")] +pub enum SelectedForm { + TagHandover, +} + +#[derive(Debug, FromForm)] +pub struct Forms<'a> { + pub form: SelectedForm, + 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, + } + } +} + +#[derive(Debug, FromForm)] +pub struct TagHandover<'a> { + #[field(validate=crate::ids::validate_id())] + pub tag_id: &'a str, + #[field(validate=len(..=256))] + pub dest_account: &'a str, +} + +#[post("/", data = "
")] +pub async fn handle_in_page_forms( + meta: CommonTemplateState, + mut db: DollTagsDb, + user: Admin, + form: Form>, +) -> PageResult { + println!("{:?}", form); + + match form.form { + SelectedForm::TagHandover => { + if let Some(ref values) = form.tag_handover.value { + // work + println!( + "woof handover of {} to {}", + values.tag_id, values.dest_account + ); + } + } + }; + + let mut trx = db.begin().await?; + let service_status = admin::get_service_status(&mut trx).await?; + trx.commit().await?; + + let previous = Contexts::from(&form); + Ok(Template::render( + "admin/index", + context! { + meta: meta.for_user(&user.0), + service_status, + previous, + }, + ) + .into()) +} diff --git a/templates/admin/index.html.tera b/templates/admin/index.html.tera index 6b23f5d..38e9d81 100644 --- a/templates/admin/index.html.tera +++ b/templates/admin/index.html.tera @@ -31,17 +31,18 @@

+

ID of the tag to hand over

- +

Destination account username

- +
@@ -52,4 +53,4 @@ -{% endblock main %} \ No newline at end of file +{% endblock main %}