use super::Tree; use axohtml::{ html, text, types::{Class, SpacedSet}, }; use crate::models::*; use crate::State; pub struct Inventory { doc: Tree, } impl Inventory { pub async fn build(state: State, categories: Vec) -> Result { let doc = html!(
{>::into(InventoryCategoryList::build(&categories).await?)}
{if state.has_active_category { html!(
{>::into(InventoryItemList::build(categories.iter().find(|category| category.active).unwrap().items()).await?)}
)} else { html!(
) }}
); Ok(Self { doc }) } } impl Into for Inventory { fn into(self) -> Tree { self.doc } } pub struct InventoryCategoryList { doc: Tree, } impl InventoryCategoryList { pub async fn build(categories: &Vec) -> Result { let biggest_category_weight: u32 = categories .iter() .map(|category| category.total_weight()) .max() .unwrap_or(1); let cls_td_active: SpacedSet = ["border", "p-0", "m-0", "font-bold"].try_into().unwrap(); let cls_td_inactive: SpacedSet = ["border", "p-0", "m-0"].try_into().unwrap(); let cls_tr_active: SpacedSet = [ "h-10", "hover:bg-purple-100", "m-3", "h-full", "outline", "outline-2", "outline-indigo-600", ] .try_into() .unwrap(); let cls_tr_inactive: SpacedSet = ["h-10", "hover:bg-purple-100", "m-3", "h-full"] .try_into() .unwrap(); let doc = html!(

"Categories"

{categories.iter().map(|category| html!( ))}
"Name" "Weight"
{text!(category.name.clone())}

{text!(category.total_weight().to_string())}

"Sum"

{text!(categories.iter().map(|category| category.total_weight()).sum::().to_string())}

); Ok(Self { doc }) } } impl Into for InventoryCategoryList { fn into(self) -> Tree { self.doc } } pub struct InventoryItemList { doc: Tree, } impl InventoryItemList { pub async fn build(items: &Vec) -> Result { let doc = html!(

"Categories"

{items.iter().map(|item| html!( ))}
"Name" "Weight"
{text!(item.name.clone())}
); Ok(Self { doc }) } } impl Into for InventoryItemList { fn into(self) -> Tree { self.doc } }