summary refs log tree commit diff stats
path: root/src/pages/buy.rs
blob: e4cd59944273e371012ed7782194a1f0d4225e98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use leptos::{
    IntoView, component,
    prelude::{Get, Show, signal},
    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, catch_errors::CatchErrors, form::Form, login_wall::LoginWall,
        site_header::SiteHeader,
    },
};

#[component]
pub fn Buy() -> impl IntoView {
    let (on_submit_errored, on_submit_errored_set) = signal(None);

    view! {
        <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>

                {
                    Form! {
                    on_submit = |barcode_number, times| {
                        let config = get_config!();
                        let navigate = use_navigate();

                        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));
                                },
                            }


                            info!("Bought barcode {barcode_number} {times} times");
                        });
                };

                <Input
                    name=barcode_number,
                    rust_type=u32,
                    html_type="number",
                    label="Barcode Number",
                />

                <Input
                    name=times,
                    rust_type=u16,
                    html_type="number",
                    label="Times"
                />
                    }
                }
            </LoginWall>
        </CatchErrors>
    }
}