Refactor a bit
This commit is contained in:
parent
f18a360982
commit
185fd938c1
7 changed files with 194 additions and 188 deletions
184
src/leptos/cafe.rs
Normal file
184
src/leptos/cafe.rs
Normal file
|
@ -0,0 +1,184 @@
|
|||
use leptos::prelude::*;
|
||||
use crate::commands::*;
|
||||
use leptos::task::spawn_local;
|
||||
use leptos::web_sys::FormData;
|
||||
use leptos::form::FromFormData;
|
||||
use std::collections::HashMap;
|
||||
use crate::types::*;
|
||||
|
||||
#[component]
|
||||
pub fn Cafe() -> impl IntoView {
|
||||
view! {
|
||||
<SwapButton
|
||||
store=Store::Aldi
|
||||
/>
|
||||
<div
|
||||
id="cafe-inventory"
|
||||
>
|
||||
<InvForm />
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn StoreLogo(store: Store) -> impl IntoView {
|
||||
view! {
|
||||
<img
|
||||
src=format!("assets/{}.svg", Into::<String>::into(&store))
|
||||
class="logo"
|
||||
// FIXME: Implement fmt trait for Store
|
||||
alt=format!("{:?}", store)
|
||||
/>
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn SwapButton(store: Store) -> impl IntoView {
|
||||
view! {
|
||||
<button
|
||||
on:click=move |_| {
|
||||
spawn_local(async move {
|
||||
swap(store, 0).await.unwrap();
|
||||
});
|
||||
}
|
||||
class="column"
|
||||
>
|
||||
<StoreLogo store=store />
|
||||
</button>
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn StoreInput(store: Store) -> impl IntoView {
|
||||
let txt = format!("{}", Into::<String>::into(&store));
|
||||
view! {
|
||||
<div
|
||||
class="labelled-input"
|
||||
>
|
||||
<label
|
||||
for=txt.clone()
|
||||
>
|
||||
<StoreLogo store=store />
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
name=txt.clone()
|
||||
id=txt.clone()
|
||||
min=0
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
struct Cash(i64);
|
||||
|
||||
impl std::str::FromStr for Cash {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let split: Vec<&str> = s.split(".").collect();
|
||||
let i = i64::from_str(split.get(0).ok_or(())?)
|
||||
.map_err(|e| println!("{:?}", e))?;
|
||||
let f = match split.get(1) {
|
||||
None => 0,
|
||||
Some(fs) => i64::from_str(
|
||||
&format!("00{}", fs)[0..2]
|
||||
)
|
||||
.map_err(|e| println!("{:?}", e))?,
|
||||
};
|
||||
Ok(Cash(i*100+f))
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn CashInput(value: RwSignal<String>) -> impl IntoView {
|
||||
let txt = "cash";
|
||||
view! {
|
||||
<div
|
||||
class="labelled-input"
|
||||
>
|
||||
<label
|
||||
for=txt.clone()
|
||||
>
|
||||
<img
|
||||
src="assets/cash.svg"
|
||||
class="logo"
|
||||
alt="Bargeld"
|
||||
/>
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
name=txt.clone()
|
||||
id=txt.clone()
|
||||
min=0
|
||||
step=0.01
|
||||
bind:value=value
|
||||
on:change=move |_| {
|
||||
println!("{:?}", value.get());
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn AccRadio(acc: Account) -> impl IntoView {
|
||||
let txt = format!("{}", Into::<String>::into(&acc));
|
||||
let id = format!("acc-{}", txt);
|
||||
view! {
|
||||
<div
|
||||
class="labelled-input"
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
name="acc"
|
||||
id=id.clone()
|
||||
value=txt
|
||||
required
|
||||
/>
|
||||
<label
|
||||
for=id.clone()
|
||||
>
|
||||
// FIXME: Implement fmt trait for Account
|
||||
{format!("{:?}", acc)}
|
||||
</label>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn SubmitButton() -> impl IntoView {
|
||||
view! {
|
||||
<button
|
||||
type="submit"
|
||||
class="shout"
|
||||
>
|
||||
"Senden"
|
||||
</button>
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn InvForm() -> impl IntoView {
|
||||
let cash = RwSignal::new(String::from("0.00"));
|
||||
view! {
|
||||
<form
|
||||
on:submit=move |ev| {
|
||||
ev.prevent_default();
|
||||
let data = FromFormData::from_event(ev.as_ref()).unwrap();
|
||||
spawn_local(async move {inventory(data).await;});
|
||||
}
|
||||
>
|
||||
<AccRadio acc=Account::Sumpf />
|
||||
<AccRadio acc=Account::Heinersyndikat />
|
||||
<StoreInput store=Store::Aldi />
|
||||
<StoreInput store=Store::Dm />
|
||||
<StoreInput store=Store::Lidl />
|
||||
<StoreInput store=Store::Rewe />
|
||||
<StoreInput store=Store::Tegut />
|
||||
<CashInput value=cash />
|
||||
<SubmitButton />
|
||||
</form>
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue