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

View File

@@ -5,7 +5,7 @@ use crate::ClientState;
use crate::{
elements::{
self,
list::{self, List},
list::{self, Action, List},
},
models::inventory::Item,
};
@@ -142,6 +142,7 @@ impl InventoryItemList {
struct Row<'a> {
item: &'a Item,
biggest_item_weight: i64,
edit_item_id: Option<Uuid>,
}
impl<'a> list::Row for Row<'a> {
@@ -162,21 +163,33 @@ impl InventoryItemList {
},
]
}
}
fn editing_config(row: Row) -> list::EditingConfig {
list::EditingConfig {
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,
fn is_edit(&self) -> bool {
self.edit_item_id.map_or(false, |id| id == self.item.id)
}
}
let table = list::List {
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 {
cells: vec![
Some(list::HeaderCell { title: "Name" }),
@@ -188,6 +201,7 @@ impl InventoryItemList {
.map(|item| Row {
item,
biggest_item_weight,
edit_item_id,
})
.collect(),
};