From 693fe687979cd28f083ec4a664d3d084b95c25e6 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Tue, 16 Jul 2024 12:46:30 +0200 Subject: [PATCH] Extract label generation page action dropdown --- src/frontend/item.rs | 17 +---------------- src/frontend/item_class.rs | 19 +++---------------- src/frontend/templates/helpers.rs | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/frontend/item.rs b/src/frontend/item.rs index 80fc81f..613279f 100644 --- a/src/frontend/item.rs +++ b/src/frontend/item.rs @@ -13,7 +13,6 @@ use uuid::Uuid; use super::templates::helpers::{Colour, ItemName, PageAction, PageActionGroup, PageActionMethod}; use super::templates::{self, datalist, forms, TemplateConfig}; -use crate::label::LabelPreset; use crate::manage; use crate::models::*; @@ -84,21 +83,7 @@ async fn show( }, colour: Colour::Success, }), - (PageActionGroup::Dropdown { - name: "Generate Label".to_string(), - actions: enum_iterator::all::() - .map(|preset| PageAction { - method: PageActionMethod::Get, - target: format!( - "/labels/generate?preset={}&ids={}", - &serde_variant::to_variant_name(&preset).unwrap(), - item.id - ), - name: preset.to_string(), - }) - .collect(), - colour: Colour::Primary, - }), + PageActionGroup::generate_labels(&[&item]), (PageActionGroup::Button { action: PageAction { method: PageActionMethod::Get, diff --git a/src/frontend/item_class.rs b/src/frontend/item_class.rs index f497deb..2302abc 100644 --- a/src/frontend/item_class.rs +++ b/src/frontend/item_class.rs @@ -11,7 +11,6 @@ use uuid::Uuid; use super::templates::helpers::{Colour, ItemName, PageAction, PageActionGroup, PageActionMethod}; use super::templates::{self, datalist, forms, TemplateConfig}; -use crate::label::LabelPreset; use crate::manage; use crate::models::*; @@ -143,21 +142,9 @@ async fn show( h3 { "Items" } } div { - (PageActionGroup::Dropdown { - name: "Generate Labels".to_string(), - actions: enum_iterator::all::() - .map(|preset| PageAction { - method: PageActionMethod::Get, - target: format!( - "/labels/generate?preset={}&ids={}", - &serde_variant::to_variant_name(&preset).unwrap(), - items.iter().map(|i| i.id.to_string()).collect::>().join(",") - ), - name: preset.to_string(), - }) - .collect(), - colour: Colour::Primary, - }) + (PageActionGroup::generate_labels( + &items.iter().collect::>(), + )) } } diff --git a/src/frontend/templates/helpers.rs b/src/frontend/templates/helpers.rs index f82337f..610112f 100644 --- a/src/frontend/templates/helpers.rs +++ b/src/frontend/templates/helpers.rs @@ -5,6 +5,7 @@ use std::collections::HashMap; use std::fmt::{self, Display}; +use crate::label::LabelPreset; use crate::models::*; use maud::{html, Markup, PreEscaped, Render}; use uuid::Uuid; @@ -190,6 +191,30 @@ impl Render for PageActionGroup { } } +impl PageActionGroup { + pub fn generate_labels(items: &[&Item]) -> PageActionGroup { + PageActionGroup::Dropdown { + name: "Generate Label".to_string(), + actions: enum_iterator::all::() + .map(|preset| PageAction { + method: PageActionMethod::Get, + target: format!( + "/labels/generate?preset={}&ids={}", + &serde_variant::to_variant_name(&preset).unwrap(), + items + .iter() + .map(|item| item.id.to_string()) + .collect::>() + .join(",") + ), + name: preset.to_string(), + }) + .collect(), + colour: Colour::Primary, + } + } +} + pub fn parents_breadcrumb( item: &Item, item_class: &ItemClass,