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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
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! {
<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
name=barcode_id,
rust_type=u32,
html_type="number",
label="Barcode number",
/>
<Input
name=product_name,
rust_type=String,
html_type="text",
label="Product Name",
reactive=product_name_signal
auto_complete=generate_suggest_products
/>
<Show
when=move || show_units.get(),
>
<Select
name=unit_id,
rust_type=Uuid,
label="Unit",
options=AsyncResource! {
(
product_name: Option<String> = product_name_signal(),
show_units_set: WriteSignal<bool> = show_units_set
) -> Result<Vec<(String, String)>, leptos::error::Error> {
let units = product_unit_fetcher(product_name).await?;
show_units_set.set(units.is_some());
if let Some(units) = units {
Ok(
units
.into_iter()
.map(|unit| (unit.full_name_singular, unit.id.to_string()))
.collect()
)
} else {
Ok(vec![])
}
}
},
/>
</Show>
<Input
name=amount,
rust_type=u16,
html_type="number",
label="Amount"
/>
}
}
</LoginWall>
</CatchErrors>
}
}
async fn generate_suggest_products(
optional_product_name: Option<String>,
) -> Result<Option<Vec<String>>, leptos::error::Error> {
if let Some(product_name) = optional_product_name
&& !product_name.is_empty()
{
let products = product_suggestion_by_name_wrapped(&product_name).await?;
Ok(Some(products.into_iter().map(|prod| prod.name).collect()))
} else {
Ok(None)
}
}
async fn product_unit_fetcher(
optinal_product_name: Option<String>,
) -> Result<Option<Vec<Unit>>, leptos::error::Error> {
if let Some(product_name) = optinal_product_name
&& !product_name.is_empty()
{
let maybe_product: Option<Product> = product_by_name_404_wrapped(&product_name).await?;
if let Some(product) = maybe_product {
let unit_property =
unit_property_by_id_wrapped(product_by_id_wrapped(product.id).await?.unit_property)
.await?;
let mut units = Vec::with_capacity(unit_property.units.len());
for unit_id in unit_property.units {
units.push(unit_by_id_wrapped(unit_id).await?);
}
Ok(Some(units))
} else {
Ok(None)
}
} else {
Ok(None)
}
}
|