summary refs log tree commit diff stats
path: root/src/pages/associate_barcode.rs
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-03-19 07:45:14 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-03-19 07:45:14 +0100
commitf6a3fb9c4d8dd86f78c9f75a23c1ac35bf35d4eb (patch)
tree5f28fbca03d83921b568f7cb1708374456d9ec42 /src/pages/associate_barcode.rs
parentfeat(treewide): Add further buttons (diff)
downloadweb-client-f6a3fb9c4d8dd86f78c9f75a23c1ac35bf35d4eb.zip
feat(treewide): Commit MVP
Diffstat (limited to '')
-rw-r--r--src/pages/associate_barcode.rs96
1 files changed, 53 insertions, 43 deletions
diff --git a/src/pages/associate_barcode.rs b/src/pages/associate_barcode.rs
index 20714ff..0e1308d 100644
--- a/src/pages/associate_barcode.rs
+++ b/src/pages/associate_barcode.rs
@@ -1,9 +1,10 @@
 use leptos::{
     IntoView, component,
-    prelude::{Get, Show, WriteSignal, signal},
+    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;
@@ -14,55 +15,62 @@ use crate::{
         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, site_header::SiteHeader},
+    components::{
+        async_fetch::AsyncResource, banner::Banner, catch_errors::CatchErrors,
+        login_wall::LoginWall, site_header::SiteHeader,
+    },
 };
 
 #[component]
 pub fn AssociateBarcode() -> impl IntoView {
-    let product_name_signal;
-
     let (errors, errors_set) = signal(None);
 
     let (show_units, show_units_set) = signal(false);
 
     view! {
-        <SiteHeader logo=icondata_io::IoPricetag back_location="/" name="Buy" />
-
-        <Show when=move || errors.get().is_some()>
-            <Banner text=move || errors.get().expect("Was some") />
-        </Show>
-
-        {
-            Form! {
-                on_submit = |barcode_id, product_name, amount, unit_id| {
-                    let config = get_config!();
-
-                    spawn_local(async move {
-                        let output = async {
-                            let product = product_by_name_external_wrapped(&config, &product_name).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(()) => (),
-                            Err(err) => {
-                                errors_set.set(
-                                    Some(
-                                        format!("Could not associate barcode: {err}")
-                                    )
-                                );
-                            },
-                        }
-                    });
+        <CatchErrors>
+            <LoginWall back=move || "/associate-barcode-product".to_owned()>
+                <SiteHeader logo=icondata_io::IoPricetag back_location="/" name="Buy" />
+
+                <Show when=move || errors.get().is_some()>
+                    <Banner text=move || errors.get().expect("Was some") />
+                </Show>
+
+                {
+                    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}")
+                                            )
+                                        );
+                                    },
+                                }
+                        });
                 };
 
                 <Input
@@ -117,8 +125,10 @@ pub fn AssociateBarcode() -> impl IntoView {
                     html_type="number",
                     label="Amount"
                 />
-            }
-        }
+                    }
+                }
+            </LoginWall>
+        </CatchErrors>
     }
 }