use leptos::{
IntoView, component,
prelude::{ElementExt, Get, Show, WriteSignal, signal},
task::spawn_local,
view,
};
use leptos_router::{NavigateOptions, hooks::use_navigate};
use rocie_client::models::{Barcode, BarcodeId, Product, Unit, UnitAmount, UnitId};
use rocie_macros::Form;
use uuid::Uuid;
use crate::{
api::{
associate_barcode_external_wrapped, get_config, product_by_id_wrapped,
product_by_name_404_wrapped, product_by_name_external_wrapped,
product_suggestion_by_name_wrapped, unit_by_id_wrapped, unit_property_by_id_wrapped,
},
components::{
async_fetch::AsyncResource, banner::Banner, catch_errors::CatchErrors,
login_wall::LoginWall, site_header::SiteHeader,
},
};
#[component]
pub fn AssociateBarcode() -> impl IntoView {
let (errors, errors_set) = signal(None);
let (show_units, show_units_set) = signal(false);
view! {
{
let product_name_signal;
Form! {
on_submit = |barcode_id, product_name, amount, unit_id| {
let config = get_config!();
let navigate = use_navigate();
spawn_local(async move {
let output = async {
let product = product_by_name_external_wrapped(&config, product_name.trim()).await?;
associate_barcode_external_wrapped(&config, product.id, Barcode {
amount:UnitAmount {
unit: UnitId { value: unit_id },
value: u32::from(amount),
},
id: BarcodeId { value: barcode_id },
}).await?;
Ok::<_, leptos::error::Error>(())
};
match output.await {
Ok(()) => {
navigate("/associate-barcode-product", NavigateOptions::default());
},
Err(err) => {
errors_set.set(
Some(
format!("Could not associate barcode: {err}")
)
);
},
}
});
};
}
}
}
}
async fn generate_suggest_products(
optional_product_name: Option,
) -> Result