From 629e7ae825b3acab7f9e7cbf4afbc49ebc917948 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sat, 13 Jul 2024 15:04:27 +0200 Subject: [PATCH] Make page actions more configurable --- src/frontend/item.rs | 19 ++++++++---- src/frontend/item_class.rs | 25 +++++++++++----- src/frontend/templates/helpers.rs | 49 +++++++++++++++++++++++++++++-- 3 files changed, 77 insertions(+), 16 deletions(-) diff --git a/src/frontend/item.rs b/src/frontend/item.rs index a78026d..003230e 100644 --- a/src/frontend/item.rs +++ b/src/frontend/item.rs @@ -10,6 +10,7 @@ use maud::html; use sqlx::PgPool; use uuid::Uuid; +use super::templates::helpers::{Colour, PageAction, PageActionMethod}; use super::templates::{self, datalist, forms, TemplateConfig}; use crate::manage; use crate::models::*; @@ -59,13 +60,17 @@ async fn show_item( title: Some(&title), page_title: Some(Box::new(item_name.clone())), page_actions: vec![ - (templates::helpers::PageAction { - href: format!("/items/add?parent={}", item.id), + (PageAction { + method: PageActionMethod::Get, + target: format!("/items/add?parent={}", item.id), name: "Add Child".to_string(), + colour: Colour::Success, }), - (templates::helpers::PageAction { - href: format!("/item/{}/edit", item.id), + (PageAction { + method: PageActionMethod::Get, + target: format!("/item/{}/edit", item.id), name: "Edit".to_string(), + colour: Colour::Warning, }), ], user: Some(user), @@ -148,9 +153,11 @@ async fn list_items(pool: web::Data, user: Identity) -> actix_web::Resul title: Some("Item List"), page_title: Some(Box::new("Item List")), page_actions: vec![ - (templates::helpers::PageAction { - href: "/items/add".to_string(), + (PageAction { + method: PageActionMethod::Get, + target: "/items/add".to_string(), name: "Add".to_string(), + colour: Colour::Success, }), ], user: Some(user), diff --git a/src/frontend/item_class.rs b/src/frontend/item_class.rs index 3d8b286..a4342ed 100644 --- a/src/frontend/item_class.rs +++ b/src/frontend/item_class.rs @@ -8,6 +8,7 @@ use maud::html; use sqlx::PgPool; use uuid::Uuid; +use super::templates::helpers::{Colour, PageAction, PageActionMethod}; use super::templates::{self, datalist, forms, TemplateConfig}; use crate::manage; use crate::models::*; @@ -50,20 +51,26 @@ async fn show_item_class( title.push_str(" – Item Details"); let mut page_actions = vec![ - (templates::helpers::PageAction { - href: format!("/items/add?class={}", item_class.id), + (PageAction { + method: PageActionMethod::Get, + target: format!("/items/add?class={}", item_class.id), name: "Add Item".to_string(), + colour: Colour::Success, }), ]; if item_class.parent.is_none() { - page_actions.push(templates::helpers::PageAction { - href: format!("/item-classes/add?parent={}", item_class.id), + page_actions.push(PageAction { + method: PageActionMethod::Get, + target: format!("/item-classes/add?parent={}", item_class.id), name: "Add Child".to_string(), + colour: Colour::Primary, }); } - page_actions.push(templates::helpers::PageAction { - href: format!("/item-class/{}/edit", item_class.id), + page_actions.push(PageAction { + method: PageActionMethod::Get, + target: format!("/item-class/{}/edit", item_class.id), name: "Edit".to_string(), + colour: Colour::Warning, }); Ok(templates::base( @@ -128,9 +135,11 @@ async fn list_item_classes( title: Some("Item Class List"), page_title: Some(Box::new("Item Class List")), page_actions: vec![ - (templates::helpers::PageAction { - href: "/item-classes/add".to_string(), + (PageAction { + method: PageActionMethod::Get, + target: "/item-classes/add".to_string(), name: "Add".to_string(), + colour: Colour::Success, }), ], user: Some(user), diff --git a/src/frontend/templates/helpers.rs b/src/frontend/templates/helpers.rs index 44a7e39..72deafc 100644 --- a/src/frontend/templates/helpers.rs +++ b/src/frontend/templates/helpers.rs @@ -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)] pub enum ItemName { Item(String), @@ -99,15 +132,27 @@ impl Render for ItemName { } } +pub enum PageActionMethod { + Get, + Post, +} + pub struct PageAction { - pub href: String, + pub method: PageActionMethod, + pub target: String, pub name: String, + pub colour: Colour, } impl Render for PageAction { fn render(&self) -> Markup { 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) } + }, + } } } }