// rocie - An enterprise grocery management system - Web app // // Copyright (C) 2026 Benedikt Peetz // SPDX-License-Identifier: GPL-3.0-or-later // // This file is part of Rocie. // // You should have received a copy of the License along with this program. // If not, see . use leptos::{ IntoView, component, prelude::{ClassAttribute, ElementChild}, view, }; use rocie_client::models::{Product, ProductAmount, ProductId, Unit}; use crate::{ api::{ amount_by_id_404_wrapped, product_by_id_wrapped, products_in_storage_wrapped, unit_by_id_wrapped, }, components::{ async_fetch::AsyncFetch, catch_errors::CatchErrors, login_wall::LoginWall, site_header::SiteHeader, }, }; #[component] pub fn Inventory() -> impl IntoView { view! {
    { AsyncFetch! { @map_error_in_producer fetcher = products_in_storage_wrapped(), producer = render_products, } }
} } fn render_products(products: Vec) -> impl IntoView { products .into_iter() .map(|product| { AsyncFetch! { @map_error_in_producer fetcher = get_full_product_by_id(product.id), producer = format_full_product, } }) .collect::>() } async fn get_full_product_by_id( id: ProductId, ) -> Result, leptos::error::Error> { let product = product_by_id_wrapped(id).await?; let amount = amount_by_id_404_wrapped(id).await?; if let Some(amount) = amount { let unit = unit_by_id_wrapped(amount.amount.unit).await?; Ok(Some((product, amount, unit))) } else { Ok(None) } } fn format_full_product(maybe_product: Option<(Product, ProductAmount, Unit)>) -> impl IntoView { maybe_product.map(|(product, amount, unit)| { view! {
  • {product.name}
  • {format!("{} {}", amount.amount.value, unit.short_name)}
} }) }