Add helper for item preview

Its API currently seems not that optimised, but in the future I want to
decrease the depdendencies on Item and ItemClass, so this is a step in
that direction.
This commit is contained in:
Simon Bruder 2024-07-21 15:18:09 +02:00
parent 82c7533b4c
commit 5563cc1c9a
Signed by: simon
GPG key ID: 347FF8699CDA0776
3 changed files with 37 additions and 9 deletions

View file

@ -12,7 +12,9 @@ use serde::Deserialize;
use sqlx::PgPool;
use uuid::Uuid;
use super::templates::helpers::{Colour, ItemName, PageAction, PageActionGroup, PageActionMethod};
use super::templates::helpers::{
Colour, ItemName, ItemPreview, PageAction, PageActionGroup, PageActionMethod,
};
use super::templates::{self, datalist, forms, TemplateConfig};
use crate::manage;
use crate::models::*;
@ -160,7 +162,7 @@ async fn show(
ul {
@for child in children {
li {
a href={ "/item/" (child.id) } { (ItemName::new(child.name.as_ref(), &item_classes.get(&child.class).unwrap().name)) }
(ItemPreview::from_parts(child.id, child.name.as_ref(), &item_classes.get(&child.class).unwrap().name))
}
}
}
@ -172,7 +174,7 @@ async fn show(
ul {
@for item in original_packaging_of {
li {
a href={ "/item/" (item.id) } { (ItemName::new(item.name.as_ref(), &item_classes.get(&item.class).unwrap().name)) }
(ItemPreview::from_parts(item.id, item.name.as_ref(), &item_classes.get(&item.class).unwrap().name))
}
}
}
@ -245,9 +247,7 @@ async fn list(pool: web::Data<PgPool>, user: Identity) -> actix_web::Result<impl
@let class = item_classes.get(&item.class).unwrap();
tr {
td {
a href={ "/item/" (item.id) } {
(ItemName::new(item.name.as_ref(), &class.name).terse())
}
(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)) }

View file

@ -11,7 +11,9 @@ use serde::Deserialize;
use sqlx::PgPool;
use uuid::Uuid;
use super::templates::helpers::{Colour, ItemName, PageAction, PageActionGroup, PageActionMethod};
use super::templates::helpers::{
Colour, ItemName, ItemPreview, PageAction, PageActionGroup, PageActionMethod,
};
use super::templates::{self, datalist, forms, TemplateConfig};
use crate::manage;
use crate::models::*;
@ -153,7 +155,7 @@ async fn show(
ul {
@for item in items {
li {
a href={ "/item/" (item.id) } { (ItemName::new(item.name.as_ref(), &item_class.name).terse()) }
(ItemPreview::new(item.id, ItemName::new(item.name.as_ref(), &item_class.name).terse()))
}
}
}

View file

@ -133,6 +133,32 @@ impl Render for ItemName {
}
}
pub struct ItemPreview {
pub id: Uuid,
pub name: ItemName,
}
impl ItemPreview {
pub fn new(id: Uuid, name: ItemName) -> Self {
Self { id, name }
}
pub fn from_parts(id: Uuid, item_name: Option<&String>, class_name: &String) -> Self {
Self {
id,
name: ItemName::new(item_name, class_name),
}
}
}
impl Render for ItemPreview {
fn render(&self) -> Markup {
html! {
a href={ "/item/" (self.id) } { (self.name) }
}
}
}
pub enum PageActionMethod {
Get,
Post,
@ -236,7 +262,7 @@ pub fn parents_breadcrumb(
};
@for parent in parents {
li .breadcrumb-item {
a href={ "/item/" (parent.id) } { (ItemName::new(parent.name.as_ref(), &parents_item_classes.get(&parent.class).unwrap().name)) }
(ItemPreview::from_parts(parent.id, parent.name.as_ref(), &parents_item_classes.get(&parent.class).unwrap().name))
}
}
li .breadcrumb-item.active { (ItemName::new(item.name.as_ref(), &item_class.name)) }