This commit is contained in:
2024-04-30 09:04:15 +02:00
parent 0949b1452c
commit cd25631863
2 changed files with 119 additions and 65 deletions

View File

@@ -13,17 +13,10 @@ pub struct NumberWithBar {
pub max_value: i64, pub max_value: i64,
} }
pub struct Icon {
pub icon: super::Icon,
pub href: String,
pub hx_config: Option<HxConfig>,
}
pub enum CellType<'a> { pub enum CellType<'a> {
Text(&'a str), Text(&'a str),
Link(Link<'a>), Link(Link<'a>),
NumberWithBar(NumberWithBar), NumberWithBar(NumberWithBar),
Icon(Icon),
} }
pub struct Cell<'a> { pub struct Cell<'a> {
@@ -31,7 +24,7 @@ pub struct Cell<'a> {
} }
impl<'a> Cell<'a> { impl<'a> Cell<'a> {
fn render(self) -> Markup { fn render(self, is_edit: bool) -> Markup {
match self.cell_type { match self.cell_type {
CellType::Text(text) => html!( CellType::Text(text) => html!(
td td
@@ -98,17 +91,31 @@ impl<'a> Cell<'a> {
{} {}
} }
), ),
CellType::Icon(icon) => html!( }
}
}
pub struct Button {
pub icon: super::Icon,
pub action: Action,
pub hx_config: Option<HxConfig>,
}
impl Button {
fn render(self) -> Markup {
html!(
td td
."border-none" ."border-none"
."p-0" ."p-0"
.(icon.icon.background()) .(self.icon.background())
.(icon.icon.background_hover()) .(self.icon.background_hover())
."h-full" ."h-full"
."w-10" ."w-10"
{ {
@match self.action {
Action::Href(href) => {
a a
href=(icon.href) href=(href)
."aspect-square" ."aspect-square"
."flex" ."flex"
{ {
@@ -116,20 +123,47 @@ impl<'a> Cell<'a> {
."m-auto" ."m-auto"
."mdi" ."mdi"
."text-xl" ."text-xl"
.(icon.icon.mdi_class()) .(self.icon.mdi_class())
{} {}
} }
} }
), Action::Submit(form) => {
button
."aspect-square"
."flex"
."w-full"
."h-full"
type="submit"
form=(form)
{
span
."m-auto"
."mdi"
.(self.icon.mdi_class())
."text-xl"
{}
} }
} }
} }
}
)
}
}
pub enum Action {
Href(String),
Submit(&'static str),
}
pub struct EditingConfig { pub struct EditingConfig {
pub edit_href: String, pub edit_action: Action,
pub edit_hx_config: Option<HxConfig>, pub edit_hx_config: Option<HxConfig>,
pub delete_href: String, pub delete_action: Action,
pub delete_hx_config: Option<HxConfig>, pub delete_hx_config: Option<HxConfig>,
pub save_action: Action,
pub save_hx_config: Option<HxConfig>,
pub cancel_action: Action,
pub cancel_hx_config: Option<HxConfig>,
} }
pub trait Row { pub trait Row {
@@ -141,10 +175,6 @@ pub trait Row {
false false
} }
fn editing_config(&self) -> Option<EditingConfig> {
None
}
fn cells(&self) -> Vec<Cell>; fn cells(&self) -> Vec<Cell>;
} }
@@ -203,6 +233,7 @@ where
tbody { tbody {
@for row in self.rows.into_iter() { @for row in self.rows.into_iter() {
@let active = row.is_active(); @let active = row.is_active();
@let is_edit = row.is_edit();
tr tr
."h-10" ."h-10"
."hover:bg-gray-100" ."hover:bg-gray-100"
@@ -213,30 +244,39 @@ where
."font-bold"[active] ."font-bold"[active]
{ {
@for cell in row.cells() { @for cell in row.cells() {
(cell.render()) (cell.render(is_edit))
}
} }
@if let Some(ref edit_config) = self.editing_config { @if let Some(ref edit_config) = self.editing_config {
@let edit_config = (*edit_config)(row); @let edit_config = (*edit_config)(row);
(Cell { @if is_edit {
cell_type: CellType::Icon(Icon { (Button {
icon: super::Icon::Save,
action: edit_config.save_action,
hx_config: edit_config.save_hx_config,
}.render())
(Button {
icon: super::Icon::Cancel,
action: edit_config.cancel_action,
hx_config: edit_config.cancel_hx_config,
}.render())
} @else {
(Button {
icon: super::Icon::Edit, icon: super::Icon::Edit,
href: edit_config.edit_href, action: edit_config.edit_action,
hx_config: edit_config.edit_hx_config, hx_config: edit_config.edit_hx_config,
}),
} .render()) } .render())
(Cell { (Button {
cell_type: CellType::Icon(Icon {
icon: super::Icon::Delete, icon: super::Icon::Delete,
href: edit_config.delete_href, action: edit_config.delete_action,
hx_config: edit_config.delete_hx_config, hx_config: edit_config.delete_hx_config,
}),
}.render()) }.render())
}
} }
} }
} }
} }
}
) )
} }
} }

View File

@@ -5,7 +5,7 @@ use crate::ClientState;
use crate::{ use crate::{
elements::{ elements::{
self, self,
list::{self, List}, list::{self, Action, List},
}, },
models::inventory::Item, models::inventory::Item,
}; };
@@ -142,6 +142,7 @@ impl InventoryItemList {
struct Row<'a> { struct Row<'a> {
item: &'a Item, item: &'a Item,
biggest_item_weight: i64, biggest_item_weight: i64,
edit_item_id: Option<Uuid>,
} }
impl<'a> list::Row for Row<'a> { impl<'a> list::Row for Row<'a> {
@@ -162,21 +163,33 @@ impl InventoryItemList {
}, },
] ]
} }
}
fn editing_config(row: Row) -> list::EditingConfig { fn is_edit(&self) -> bool {
list::EditingConfig { self.edit_item_id.map_or(false, |id| id == self.item.id)
edit_href: format!("?edit_item={id}", id = row.item.id),
edit_hx_config: None,
delete_href: format!("/inventory/item/{id}/delete", id = row.item.id),
delete_hx_config: None,
} }
} }
let table = list::List { let table = list::List {
id: None, id: None,
editing_config: Some(Box::new(editing_config)), editing_config: Some(Box::new(|row: Row| list::EditingConfig {
edit_action: Action::Href(format!("?edit_item={id}", id = row.item.id)),
edit_hx_config: None,
delete_action: Action::Href(format!(
"/inventory/item/{id}/delete",
id = row.item.id
)),
delete_hx_config: None,
save_action: Action::Submit("edit-item"),
save_hx_config: None,
cancel_action: Action::Href(format!(
"/inventory/item/{id}/cancel",
id = row.item.id
)),
cancel_hx_config: None,
})),
header: list::Header { header: list::Header {
cells: vec![ cells: vec![
Some(list::HeaderCell { title: "Name" }), Some(list::HeaderCell { title: "Name" }),
@@ -188,6 +201,7 @@ impl InventoryItemList {
.map(|item| Row { .map(|item| Row {
item, item,
biggest_item_weight, biggest_item_weight,
edit_item_id,
}) })
.collect(), .collect(),
}; };