Use ItemPreview for parent breadcrumbs

This commit is contained in:
Simon Bruder 2024-07-21 15:26:39 +02:00
parent 5563cc1c9a
commit bb011cf84a
Signed by: simon
GPG key ID: 347FF8699CDA0776
2 changed files with 25 additions and 13 deletions

View file

@ -127,7 +127,16 @@ async fn show(
}
tr {
th { "Parents" }
td { (templates::helpers::parents_breadcrumb(&item, &item_class, &parents, &item_classes, true)) }
td {
(templates::helpers::parents_breadcrumb(
ItemName::new(
item.name.as_ref(),
&item_class.name
),
&parents.iter().map(|parent| ItemPreview::from_parts(parent.id, parent.name.as_ref(), &item_classes.get(&parent.class).unwrap().name)).collect::<Vec<ItemPreview>>(),
true
))
}
}
tr {
th { "Original Packaging" }
@ -245,12 +254,22 @@ async fn list(pool: web::Data<PgPool>, user: Identity) -> actix_web::Result<impl
tbody {
@for item in item_list {
@let class = item_classes.get(&item.class).unwrap();
@let parents = item_parents.get(&item.id).unwrap();
tr {
td {
(ItemPreview::new(item.id, ItemName::new(item.name.as_ref(), &class.name).terse()))
}
td { a href={ "/item-class/" (class.id) } { (class.name) } }
td { (templates::helpers::parents_breadcrumb(&item, &class, item_parents.get(&item.id).unwrap(), &item_classes, false)) }
td {
(templates::helpers::parents_breadcrumb(
ItemName::new(
item.name.as_ref(),
&class.name
),
&parents.iter().map(|parent| ItemPreview::from_parts(parent.id, parent.name.as_ref(), &item_classes.get(&parent.class).unwrap().name)).collect::<Vec<ItemPreview>>(),
false
))
}
}
}
}

View file

@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::collections::HashMap;
use std::fmt::{self, Display};
use crate::label::LabelPreset;
@ -241,13 +240,7 @@ impl PageActionGroup {
}
}
pub fn parents_breadcrumb(
item: &Item,
item_class: &ItemClass,
parents: &[Item],
parents_item_classes: &HashMap<Uuid, ItemClass>,
full: bool,
) -> Markup {
pub fn parents_breadcrumb(name: ItemName, parents: &[ItemPreview], full: bool) -> Markup {
const LIMIT: usize = 3;
html! {
@ -255,17 +248,17 @@ pub fn parents_breadcrumb(
@if !full && parents.len() > LIMIT {
li .breadcrumb-item { "" }
}
@let parents: Box<dyn Iterator<Item = &Item>> = if full {
@let parents: Box<dyn Iterator<Item = &ItemPreview>> = if full {
Box::new(parents.iter())
} else {
Box::new(parents.iter().rev().take(LIMIT).rev())
};
@for parent in parents {
li .breadcrumb-item {
(ItemPreview::from_parts(parent.id, parent.name.as_ref(), &parents_item_classes.get(&parent.class).unwrap().name))
(parent)
}
}
li .breadcrumb-item.active { (ItemName::new(item.name.as_ref(), &item_class.name)) }
li .breadcrumb-item.active { (name) }
}
}
}