From fdfd90a7049198c796641415fdcfb706496f0a30 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Thu, 11 Jul 2024 23:01:27 +0200 Subject: [PATCH] Allow prefilling add forms --- src/frontend/item.rs | 5 ++++- src/frontend/item_class.rs | 4 +++- src/models.rs | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/frontend/item.rs b/src/frontend/item.rs index 950c67b..0bb40c9 100644 --- a/src/frontend/item.rs +++ b/src/frontend/item.rs @@ -173,7 +173,7 @@ async fn list_items(pool: web::Data) -> actix_web::Result actix_web::Result { +async fn add_item(form: web::Query) -> actix_web::Result { Ok(templates::base( TemplateConfig { path: "/items/add", @@ -187,6 +187,7 @@ async fn add_item() -> actix_web::Result { r#type: forms::InputType::Text, name: "name", title: "Name", + value: form.name.clone(), ..Default::default() }) (forms::InputGroup { @@ -194,12 +195,14 @@ async fn add_item() -> actix_web::Result { name: "class", title: "Class", required: true, + value: form.class.map(|id| id.to_string()), ..Default::default() }) (forms::InputGroup { r#type: forms::InputType::Text, name: "parent", title: "Parent", + value: form.parent.map(|id| id.to_string()), ..Default::default() }) diff --git a/src/frontend/item_class.rs b/src/frontend/item_class.rs index d836a71..e73df7e 100644 --- a/src/frontend/item_class.rs +++ b/src/frontend/item_class.rs @@ -147,7 +147,7 @@ async fn list_item_classes(pool: web::Data) -> actix_web::Result actix_web::Result { +async fn add_item_class(form: web::Query) -> actix_web::Result { Ok(templates::base( TemplateConfig { path: "/items-classes/add", @@ -162,6 +162,7 @@ async fn add_item_class() -> actix_web::Result { name: "name", title: "Name", required: true, + value: form.name.clone(), ..Default::default() }) (forms::InputGroup { @@ -169,6 +170,7 @@ async fn add_item_class() -> actix_web::Result { name: "parent", title: "Parent", disabled: true, + value: form.parent.map(|id| id.to_string()), ..Default::default() }) diff --git a/src/models.rs b/src/models.rs index 018b4f9..80a9950 100644 --- a/src/models.rs +++ b/src/models.rs @@ -25,6 +25,19 @@ pub struct NewItem { pub class: Uuid, } +// Its structure is not how it could ideally be +// (doubly nested Options where the original struct already has an Option>) +// because the intended usage (in a GET request parameter) does not allow such fine-grained types. +// TODO: this can be automated from NewItem with derive macro +#[derive(Debug, Deserialize)] +pub struct NewItemForm { + #[serde(default)] + pub name: Option, + #[serde(default)] + pub parent: Option, + pub class: Option, +} + #[derive(Clone, Debug, Serialize)] pub struct ItemClass { pub id: Uuid, @@ -41,3 +54,11 @@ pub struct NewItemClass { #[serde(default)] pub parent: Option, } + +// see NewItemForm +#[derive(Debug, Deserialize)] +pub struct NewItemClassForm { + pub name: Option, + #[serde(default)] + pub parent: Option, +}