Make page actions more configurable

This commit is contained in:
Simon Bruder 2024-07-13 15:04:27 +02:00
parent 7563905e66
commit 629e7ae825
Signed by: simon
GPG key ID: 347FF8699CDA0776
3 changed files with 77 additions and 16 deletions

View file

@ -10,6 +10,7 @@ use maud::html;
use sqlx::PgPool; use sqlx::PgPool;
use uuid::Uuid; use uuid::Uuid;
use super::templates::helpers::{Colour, PageAction, PageActionMethod};
use super::templates::{self, datalist, forms, TemplateConfig}; use super::templates::{self, datalist, forms, TemplateConfig};
use crate::manage; use crate::manage;
use crate::models::*; use crate::models::*;
@ -59,13 +60,17 @@ async fn show_item(
title: Some(&title), title: Some(&title),
page_title: Some(Box::new(item_name.clone())), page_title: Some(Box::new(item_name.clone())),
page_actions: vec![ page_actions: vec![
(templates::helpers::PageAction { (PageAction {
href: format!("/items/add?parent={}", item.id), method: PageActionMethod::Get,
target: format!("/items/add?parent={}", item.id),
name: "Add Child".to_string(), name: "Add Child".to_string(),
colour: Colour::Success,
}), }),
(templates::helpers::PageAction { (PageAction {
href: format!("/item/{}/edit", item.id), method: PageActionMethod::Get,
target: format!("/item/{}/edit", item.id),
name: "Edit".to_string(), name: "Edit".to_string(),
colour: Colour::Warning,
}), }),
], ],
user: Some(user), user: Some(user),
@ -148,9 +153,11 @@ async fn list_items(pool: web::Data<PgPool>, user: Identity) -> actix_web::Resul
title: Some("Item List"), title: Some("Item List"),
page_title: Some(Box::new("Item List")), page_title: Some(Box::new("Item List")),
page_actions: vec![ page_actions: vec![
(templates::helpers::PageAction { (PageAction {
href: "/items/add".to_string(), method: PageActionMethod::Get,
target: "/items/add".to_string(),
name: "Add".to_string(), name: "Add".to_string(),
colour: Colour::Success,
}), }),
], ],
user: Some(user), user: Some(user),

View file

@ -8,6 +8,7 @@ use maud::html;
use sqlx::PgPool; use sqlx::PgPool;
use uuid::Uuid; use uuid::Uuid;
use super::templates::helpers::{Colour, PageAction, PageActionMethod};
use super::templates::{self, datalist, forms, TemplateConfig}; use super::templates::{self, datalist, forms, TemplateConfig};
use crate::manage; use crate::manage;
use crate::models::*; use crate::models::*;
@ -50,20 +51,26 @@ async fn show_item_class(
title.push_str(" Item Details"); title.push_str(" Item Details");
let mut page_actions = vec![ let mut page_actions = vec![
(templates::helpers::PageAction { (PageAction {
href: format!("/items/add?class={}", item_class.id), method: PageActionMethod::Get,
target: format!("/items/add?class={}", item_class.id),
name: "Add Item".to_string(), name: "Add Item".to_string(),
colour: Colour::Success,
}), }),
]; ];
if item_class.parent.is_none() { if item_class.parent.is_none() {
page_actions.push(templates::helpers::PageAction { page_actions.push(PageAction {
href: format!("/item-classes/add?parent={}", item_class.id), method: PageActionMethod::Get,
target: format!("/item-classes/add?parent={}", item_class.id),
name: "Add Child".to_string(), name: "Add Child".to_string(),
colour: Colour::Primary,
}); });
} }
page_actions.push(templates::helpers::PageAction { page_actions.push(PageAction {
href: format!("/item-class/{}/edit", item_class.id), method: PageActionMethod::Get,
target: format!("/item-class/{}/edit", item_class.id),
name: "Edit".to_string(), name: "Edit".to_string(),
colour: Colour::Warning,
}); });
Ok(templates::base( Ok(templates::base(
@ -128,9 +135,11 @@ async fn list_item_classes(
title: Some("Item Class List"), title: Some("Item Class List"),
page_title: Some(Box::new("Item Class List")), page_title: Some(Box::new("Item Class List")),
page_actions: vec![ page_actions: vec![
(templates::helpers::PageAction { (PageAction {
href: "/item-classes/add".to_string(), method: PageActionMethod::Get,
target: "/item-classes/add".to_string(),
name: "Add".to_string(), name: "Add".to_string(),
colour: Colour::Success,
}), }),
], ],
user: Some(user), user: Some(user),

View file

@ -51,6 +51,39 @@ impl Render for Js<'_> {
} }
} }
#[allow(dead_code)]
pub enum Colour {
Primary,
Secondary,
Success,
Danger,
Warning,
Info,
}
impl fmt::Display for Colour {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Colour::Primary => "primary",
Colour::Secondary => "secondary",
Colour::Success => "success",
Colour::Danger => "danger",
Colour::Warning => "warning",
Colour::Info => "info",
}
)
}
}
impl Colour {
fn button(&self) -> String {
format!("btn-{self}")
}
}
#[derive(Clone)] #[derive(Clone)]
pub enum ItemName { pub enum ItemName {
Item(String), Item(String),
@ -99,15 +132,27 @@ impl Render for ItemName {
} }
} }
pub enum PageActionMethod {
Get,
Post,
}
pub struct PageAction { pub struct PageAction {
pub href: String, pub method: PageActionMethod,
pub target: String,
pub name: String, pub name: String,
pub colour: Colour,
} }
impl Render for PageAction { impl Render for PageAction {
fn render(&self) -> Markup { fn render(&self) -> Markup {
html! { html! {
a .btn.btn-primary href=(self.href) { (self.name) } @match self.method {
PageActionMethod::Get => a .btn.btn-primary.(self.colour.button()) href=(self.target) { (self.name) },
PageActionMethod::Post => form action=(self.target) method="POST" {
button .btn.btn-primary.(self.colour.button()) type="submit" { (self.name) }
},
}
} }
} }
} }