From 762d1fcad97dbbafe2030d5272a6ba9262e85655 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sun, 14 Jul 2024 16:37:07 +0200 Subject: [PATCH] Generalize PageAction to allow dropdown --- src/frontend/item.rs | 42 +++++++++++++++---------- src/frontend/item_class.rs | 52 ++++++++++++++++++------------- src/frontend/templates/helpers.rs | 47 +++++++++++++++++++++++----- src/frontend/templates/mod.rs | 2 +- 4 files changed, 97 insertions(+), 46 deletions(-) diff --git a/src/frontend/item.rs b/src/frontend/item.rs index 8378376..a10af3b 100644 --- a/src/frontend/item.rs +++ b/src/frontend/item.rs @@ -10,7 +10,7 @@ use maud::html; use sqlx::PgPool; use uuid::Uuid; -use super::templates::helpers::{Colour, ItemName, PageAction, PageActionMethod}; +use super::templates::helpers::{Colour, ItemName, PageAction, PageActionGroup, PageActionMethod}; use super::templates::{self, datalist, forms, TemplateConfig}; use crate::manage; use crate::models::*; @@ -74,22 +74,28 @@ async fn show( title: Some(&title), page_title: Some(Box::new(item_name.clone())), page_actions: vec![ - (PageAction { - method: PageActionMethod::Get, - target: format!("/items/add?parent={}", item.id), - name: "Add Child".to_string(), + (PageActionGroup::Button { + action: PageAction { + method: PageActionMethod::Get, + target: format!("/items/add?parent={}", item.id), + name: "Add Child".to_string(), + }, colour: Colour::Success, }), - (PageAction { - method: PageActionMethod::Get, - target: format!("/item/{}/edit", item.id), - name: "Edit".to_string(), + (PageActionGroup::Button { + action: PageAction { + method: PageActionMethod::Get, + target: format!("/item/{}/edit", item.id), + name: "Edit".to_string(), + }, colour: Colour::Warning, }), - (PageAction { - method: PageActionMethod::Post, - target: format!("/item/{}/delete", item.id), - name: "Delete".to_string(), + (PageActionGroup::Button { + action: PageAction { + method: PageActionMethod::Post, + target: format!("/item/{}/delete", item.id), + name: "Delete".to_string(), + }, colour: Colour::Danger, }), ], @@ -201,10 +207,12 @@ async fn list(pool: web::Data, user: Identity) -> actix_web::Result, user: Identity) -> actix_web::Result, + colour: Colour, + }, +} + pub struct PageAction { pub method: PageActionMethod, pub target: String, pub name: String, - pub colour: Colour, } -impl Render for PageAction { +impl Render for PageActionGroup { fn render(&self) -> Markup { html! { - @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) } - }, + @match self { + Self::Button { action, colour } => { + @match action.method { + PageActionMethod::Get => a .btn.btn-primary.(colour.button()) href=(action.target) { (action.name) }, + PageActionMethod::Post => form action=(action.target) method="POST" { + button .btn.btn-primary.(colour.button()) type="submit" { (action.name) } + }, + } + } + Self::Dropdown { name, actions, colour } => { + div .btn-group { + button .btn.(colour.button()).dropdown-toggle type="button" data-bs-toggle="dropdown" { (name) } + + ul .dropdown-menu { + @for action in actions { + li { + @match action.method { + PageActionMethod::Get => a .dropdown-item href=(action.target) { (action.name) }, + PageActionMethod::Post => form action=(action.target) method="POST" { + button .dropdown-item type="submit" { (action.name) } + }, + } + } + } + } + } + } } } } diff --git a/src/frontend/templates/mod.rs b/src/frontend/templates/mod.rs index 1e44c11..c83ef4f 100644 --- a/src/frontend/templates/mod.rs +++ b/src/frontend/templates/mod.rs @@ -69,7 +69,7 @@ pub struct TemplateConfig<'a> { pub path: &'a str, pub title: Option<&'a str>, pub page_title: Option>, - pub page_actions: Vec, + pub page_actions: Vec, pub extra_css: Vec>, pub extra_js: Vec>, pub datalists: Vec<&'a Datalist>,