diff options
Diffstat (limited to 'src/pages/buy.rs')
| -rw-r--r-- | src/pages/buy.rs | 63 |
1 files changed, 42 insertions, 21 deletions
diff --git a/src/pages/buy.rs b/src/pages/buy.rs index f3335f6..e4cd599 100644 --- a/src/pages/buy.rs +++ b/src/pages/buy.rs @@ -4,12 +4,16 @@ use leptos::{ task::spawn_local, view, }; +use leptos_router::{NavigateOptions, hooks::use_navigate}; use log::info; use rocie_client::models::BarcodeId; use crate::{ api::{buy_barcode_external_wrapped, get_config}, - components::{banner::Banner, form::Form, site_header::SiteHeader}, + components::{ + banner::Banner, catch_errors::CatchErrors, form::Form, login_wall::LoginWall, + site_header::SiteHeader, + }, }; #[component] @@ -17,29 +21,44 @@ pub fn Buy() -> impl IntoView { let (on_submit_errored, on_submit_errored_set) = signal(None); view! { - <SiteHeader logo=icondata_io::IoPricetag back_location="/" name="Buy" /> + <CatchErrors> + <LoginWall back=move || "/buy".to_owned()> + <SiteHeader logo=icondata_io::IoPricetag back_location="/" name="Buy" /> - <Show when=move || on_submit_errored.get().is_some()> - <Banner text=move || on_submit_errored.get().expect("Should be some") /> - </Show> + <Show when=move || on_submit_errored.get().is_some()> + <Banner text=move || on_submit_errored.get().expect("Should be some") /> + </Show> - { - Form! { - on_submit = |barcode_number, times| { - let config = get_config!(); + { + Form! { + on_submit = |barcode_number, times| { + let config = get_config!(); + let navigate = use_navigate(); - spawn_local(async move { - if let Err(err) = buy_barcode_external_wrapped(&config, BarcodeId { value: barcode_number }, u32::from(times)).await { - let error = format!("Error in form on-submit for barcode `{barcode_number}`: {err}"); + spawn_local(async move { + match buy_barcode_external_wrapped( + &config, + BarcodeId { value: barcode_number }, + u32::from(times) + ).await { + Ok(()) => { + navigate("/buy", NavigateOptions::default()); + on_submit_errored_set.set(None); + }, + Err(err) => { + let error = + format!( + "Error in form \ + on-submit for barcode \ + `{barcode_number}`: {err}" + ); + on_submit_errored_set.set(Some(error)); + }, + } - on_submit_errored_set.set(Some(error)); - } else { - on_submit_errored_set.set(None); - } - - info!("Bought barcode {barcode_number} {times} times"); - }); + info!("Bought barcode {barcode_number} {times} times"); + }); }; <Input @@ -55,7 +74,9 @@ pub fn Buy() -> impl IntoView { html_type="number", label="Times" /> - } - } + } + } + </LoginWall> + </CatchErrors> } } |
