run cargo fmt

This commit is contained in:
Sebastian Hugentobler 2022-03-26 07:57:57 +01:00
parent 8cf6fbce57
commit 368e3b2b30
5 changed files with 48 additions and 39 deletions

View File

@ -60,7 +60,9 @@ pub async fn put(url: &str, data: JsValue) -> Result<(), FetchError> {
opts.body(Some(&data)); opts.body(Some(&data));
let request = Request::new_with_str_and_init(url, &opts)?; let request = Request::new_with_str_and_init(url, &opts)?;
request.headers().set("Content-Type", "application/x-www-form-urlencoded")?; request
.headers()
.set("Content-Type", "application/x-www-form-urlencoded")?;
self::send_request(request).await?; self::send_request(request).await?;
Ok(()) Ok(())

View File

@ -1,9 +1,9 @@
use std::str::FromStr;
use web_sys::{HtmlInputElement, HtmlSelectElement};
use yew::{classes, html, Component, Context, Html, Properties, NodeRef};
use yew_agent::{Dispatched, Dispatcher};
use crate::event_bus::{EventBus, Request}; use crate::event_bus::{EventBus, Request};
use crate::events::Event; use crate::events::Event;
use std::str::FromStr;
use web_sys::{HtmlInputElement, HtmlSelectElement};
use yew::{classes, html, Component, Context, Html, NodeRef, Properties};
use yew_agent::{Dispatched, Dispatcher};
pub enum Msg { pub enum Msg {
AmountChanged, AmountChanged,
@ -31,7 +31,9 @@ impl Account {
fn amount(&self) -> f64 { fn amount(&self) -> f64 {
if let Some(amount_el) = self.amount_ref.cast::<HtmlInputElement>() { if let Some(amount_el) = self.amount_ref.cast::<HtmlInputElement>() {
f64::from_str(&amount_el.value()).unwrap_or_default() f64::from_str(&amount_el.value()).unwrap_or_default()
} else { 0_f64 } } else {
0_f64
}
} }
fn is_amount_valid(&self) -> bool { fn is_amount_valid(&self) -> bool {
@ -41,8 +43,14 @@ impl Account {
fn selected_transfer_account(&self) -> Option<String> { fn selected_transfer_account(&self) -> Option<String> {
if let Some(transfer_account_el) = self.transfer_account_ref.cast::<HtmlSelectElement>() { if let Some(transfer_account_el) = self.transfer_account_ref.cast::<HtmlSelectElement>() {
let value: String = transfer_account_el.value(); let value: String = transfer_account_el.value();
if value.is_empty() || value == "undefined" { None } else { Some(value) } if value.is_empty() || value == "undefined" {
} else { None } None
} else {
Some(value)
}
} else {
None
}
} }
} }
@ -70,16 +78,24 @@ impl Component for Account {
} }
Msg::Deposit => { Msg::Deposit => {
let amount = self.amount() + ctx.props().balance; let amount = self.amount() + ctx.props().balance;
self.event_bus.send(Request::EventBusMsg(Event::SetBalance(amount, ctx.props().nr.clone()))); self.event_bus.send(Request::EventBusMsg(Event::SetBalance(
amount,
ctx.props().nr.clone(),
)));
false false
} }
Msg::Withdraw => { Msg::Withdraw => {
let amount = ctx.props().balance - self.amount(); let amount = ctx.props().balance - self.amount();
if amount > 0_f64 { if amount > 0_f64 {
self.event_bus.send(Request::EventBusMsg(Event::SetBalance(amount, ctx.props().nr.clone()))); self.event_bus.send(Request::EventBusMsg(Event::SetBalance(
amount,
ctx.props().nr.clone(),
)));
} else { } else {
self.event_bus.send(Request::EventBusMsg(Event::ShowError("Balance can not be overdrawn".into()))); self.event_bus.send(Request::EventBusMsg(Event::ShowError(
"Balance can not be overdrawn".into(),
)));
} }
false false
@ -88,11 +104,18 @@ impl Component for Account {
let amount = ctx.props().balance - self.amount(); let amount = ctx.props().balance - self.amount();
if amount > 0_f64 { if amount > 0_f64 {
self.event_bus.send(Request::EventBusMsg(Event::SetBalance(amount, ctx.props().nr.clone()))); self.event_bus.send(Request::EventBusMsg(Event::SetBalance(
amount,
ctx.props().nr.clone(),
)));
} else { } else {
let transfer_nr = self.selected_transfer_account(); let transfer_nr = self.selected_transfer_account();
if let Some(transfer_nr) = transfer_nr { if let Some(transfer_nr) = transfer_nr {
self.event_bus.send(Request::EventBusMsg(Event::Transfer(amount, ctx.props().nr.clone(), transfer_nr))); self.event_bus.send(Request::EventBusMsg(Event::Transfer(
amount,
ctx.props().nr.clone(),
transfer_nr,
)));
} }
} }
@ -102,21 +125,13 @@ impl Component for Account {
} }
fn view(&self, ctx: &Context<Self>) -> Html { fn view(&self, ctx: &Context<Self>) -> Html {
let onchange = ctx let onchange = ctx.link().callback(|_| Msg::AmountChanged);
.link()
.callback(|_| Msg::AmountChanged);
let on_deposit = ctx let on_deposit = ctx.link().callback(|_| Msg::Deposit);
.link()
.callback(|_| Msg::Deposit);
let on_withdraw = ctx let on_withdraw = ctx.link().callback(|_| Msg::Withdraw);
.link()
.callback(|_| Msg::Withdraw);
let on_transfer = ctx let on_transfer = ctx.link().callback(|_| Msg::Transfer);
.link()
.callback(|_| Msg::Transfer);
html! { html! {
<> <>

View File

@ -10,7 +10,7 @@ pub enum Msg {
#[derive(Properties, PartialEq)] #[derive(Properties, PartialEq)]
pub struct AccountsProps { pub struct AccountsProps {
pub account_nrs: Vec<String>, pub account_nrs: Vec<String>,
pub selected_nr: String pub selected_nr: String,
} }
pub struct Accounts { pub struct Accounts {
@ -49,12 +49,7 @@ impl Component for Accounts {
} }
impl Accounts { impl Accounts {
fn account_entry( fn account_entry(&self, nr: &str, selected_nr: &str, ctx: &Context<Self>) -> Html {
&self,
nr: &str,
selected_nr: &str,
ctx: &Context<Self>,
) -> Html {
let mut class = Classes::from("accounts__item"); let mut class = Classes::from("accounts__item");
if selected_nr == nr { if selected_nr == nr {
class.push("accounts__item-selected"); class.push("accounts__item-selected");

View File

@ -1,8 +1,8 @@
use crate::api;
use crate::components::account::Account; use crate::components::account::Account;
use crate::components::accounts::Accounts; use crate::components::accounts::Accounts;
use crate::event_bus::EventBus; use crate::event_bus::EventBus;
use crate::events::Event; use crate::events::Event;
use crate::api;
use yew::{classes, html, Component, Context, Html}; use yew::{classes, html, Component, Context, Html};
use yew_agent::{Bridge, Bridged}; use yew_agent::{Bridge, Bridged};
@ -37,7 +37,7 @@ impl Component for Main {
account_nrs: vec![], account_nrs: vec![],
selected_balance: 0_f64, selected_balance: 0_f64,
selected_nr: "".into(), selected_nr: "".into(),
selected_owner: "".into() selected_owner: "".into(),
} }
} }
@ -85,10 +85,7 @@ impl Component for Main {
}); });
false false
} }
Event::Transfer(amount, from, to) => { Event::Transfer(amount, from, to) => false,
false
}
} }
} }

View File

@ -1,10 +1,10 @@
use crate::components::main::Main; use crate::components::main::Main;
mod api;
mod client; mod client;
mod components; mod components;
mod event_bus; mod event_bus;
mod events; mod events;
mod api;
fn main() { fn main() {
yew::start_app::<Main>(); yew::start_app::<Main>();