Allow prefilling add forms

This commit is contained in:
Simon Bruder 2024-07-11 23:01:27 +02:00
parent 6d1fa56759
commit 06a1137377
Signed by: simon
GPG key ID: 347FF8699CDA0776
3 changed files with 29 additions and 3 deletions

View file

@ -173,7 +173,7 @@ async fn list_items(pool: web::Data<PgPool>) -> actix_web::Result<impl Responder
} }
#[get("/items/add")] #[get("/items/add")]
async fn add_item() -> actix_web::Result<impl Responder> { async fn add_item(form: web::Query<NewItemForm>) -> actix_web::Result<impl Responder> {
Ok(templates::base( Ok(templates::base(
TemplateConfig { TemplateConfig {
path: "/items/add", path: "/items/add",
@ -187,6 +187,7 @@ async fn add_item() -> actix_web::Result<impl Responder> {
r#type: forms::InputType::Text, r#type: forms::InputType::Text,
name: "name", name: "name",
title: "Name", title: "Name",
value: form.name.clone(),
..Default::default() ..Default::default()
}) })
(forms::InputGroup { (forms::InputGroup {
@ -194,12 +195,14 @@ async fn add_item() -> actix_web::Result<impl Responder> {
name: "class", name: "class",
title: "Class", title: "Class",
required: true, required: true,
value: form.class.map(|id| id.to_string()),
..Default::default() ..Default::default()
}) })
(forms::InputGroup { (forms::InputGroup {
r#type: forms::InputType::Text, r#type: forms::InputType::Text,
name: "parent", name: "parent",
title: "Parent", title: "Parent",
value: form.parent.map(|id| id.to_string()),
..Default::default() ..Default::default()
}) })

View file

@ -147,7 +147,7 @@ async fn list_item_classes(pool: web::Data<PgPool>) -> actix_web::Result<impl Re
} }
#[get("/item-classes/add")] #[get("/item-classes/add")]
async fn add_item_class() -> actix_web::Result<impl Responder> { async fn add_item_class(form: web::Query<NewItemClassForm>) -> actix_web::Result<impl Responder> {
Ok(templates::base( Ok(templates::base(
TemplateConfig { TemplateConfig {
path: "/items-classes/add", path: "/items-classes/add",
@ -162,13 +162,15 @@ async fn add_item_class() -> actix_web::Result<impl Responder> {
name: "name", name: "name",
title: "Name", title: "Name",
required: true, required: true,
value: form.name.clone(),
..Default::default() ..Default::default()
}) })
(forms::InputGroup { (forms::InputGroup {
r#type: forms::InputType::Text, r#type: forms::InputType::Text,
name: "parent", name: "parent",
title: "Parent", title: "Parent",
disabled: true, disabled: form.parent.is_none(),
value: form.parent.map(|id| id.to_string()),
..Default::default() ..Default::default()
}) })

View file

@ -25,6 +25,19 @@ pub struct NewItem {
pub class: Uuid, 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<String>,
#[serde(default)]
pub parent: Option<Uuid>,
pub class: Option<Uuid>,
}
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
pub struct ItemClass { pub struct ItemClass {
pub id: Uuid, pub id: Uuid,
@ -41,3 +54,11 @@ pub struct NewItemClass {
#[serde(default)] #[serde(default)]
pub parent: Option<Uuid>, pub parent: Option<Uuid>,
} }
// see NewItemForm
#[derive(Debug, Deserialize)]
pub struct NewItemClassForm {
pub name: Option<String>,
#[serde(default)]
pub parent: Option<Uuid>,
}