use std::{convert::Infallible, str::FromStr}; use leptos::{ IntoView, component, prelude::{Get, Show, signal}, task::spawn_local, view, }; use rocie_client::models::{ProductStub, UnitPropertyId}; use rocie_macros::Form; use uuid::Uuid; use crate::{ api::{get_config, register_product_external_wrapped, unit_properties_wrapped}, components::{async_fetch::AsyncResource, banner::Banner, site_header::SiteHeader}, }; struct OptionalString(Option); impl FromStr for OptionalString { type Err = Infallible; fn from_str(s: &str) -> Result { if s.is_empty() { Ok(Self(None)) } else { Ok(Self(Some(s.to_owned()))) } } } #[component] pub fn CreateProduct() -> impl IntoView { let (error_message, error_message_set) = signal(None); view! { { Form! { on_submit = |product_name, product_description, unit_property_id| { let config = get_config!(); spawn_local(async move { match register_product_external_wrapped(&config, ProductStub { description: Some(product_description.0), name: product_name, parent: None, // TODO: Add this <2025-10-25> unit_property: UnitPropertyId { value: unit_property_id }, } ).await { Ok(_id) => {} Err(err) => error_message_set.set(Some(format!("Failed to create product: {err}"))), } }); };