diff --git a/http-client/src/client.rs b/http-client/src/client.rs index e35b20b..bc88630 100644 --- a/http-client/src/client.rs +++ b/http-client/src/client.rs @@ -60,7 +60,9 @@ pub async fn put(url: &str, data: JsValue) -> Result<(), FetchError> { opts.body(Some(&data)); 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?; Ok(()) diff --git a/http-client/src/components/account.rs b/http-client/src/components/account.rs index 0e8640d..91457e3 100644 --- a/http-client/src/components/account.rs +++ b/http-client/src/components/account.rs @@ -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::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 { AmountChanged, @@ -31,7 +31,9 @@ impl Account { fn amount(&self) -> f64 { if let Some(amount_el) = self.amount_ref.cast::() { f64::from_str(&amount_el.value()).unwrap_or_default() - } else { 0_f64 } + } else { + 0_f64 + } } fn is_amount_valid(&self) -> bool { @@ -41,8 +43,14 @@ impl Account { fn selected_transfer_account(&self) -> Option { if let Some(transfer_account_el) = self.transfer_account_ref.cast::() { let value: String = transfer_account_el.value(); - if value.is_empty() || value == "undefined" { None } else { Some(value) } - } else { None } + if value.is_empty() || value == "undefined" { + None + } else { + Some(value) + } + } else { + None + } } } @@ -70,16 +78,24 @@ impl Component for Account { } Msg::Deposit => { 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 } Msg::Withdraw => { let amount = ctx.props().balance - self.amount(); 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 { - 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 @@ -88,11 +104,18 @@ impl Component for Account { let amount = ctx.props().balance - self.amount(); 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 { let transfer_nr = self.selected_transfer_account(); 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) -> Html { - let onchange = ctx - .link() - .callback(|_| Msg::AmountChanged); + let onchange = ctx.link().callback(|_| Msg::AmountChanged); - let on_deposit = ctx - .link() - .callback(|_| Msg::Deposit); + let on_deposit = ctx.link().callback(|_| Msg::Deposit); - let on_withdraw = ctx - .link() - .callback(|_| Msg::Withdraw); + let on_withdraw = ctx.link().callback(|_| Msg::Withdraw); - let on_transfer = ctx - .link() - .callback(|_| Msg::Transfer); + let on_transfer = ctx.link().callback(|_| Msg::Transfer); html! { <> diff --git a/http-client/src/components/accounts.rs b/http-client/src/components/accounts.rs index 3269c70..c484b93 100644 --- a/http-client/src/components/accounts.rs +++ b/http-client/src/components/accounts.rs @@ -10,7 +10,7 @@ pub enum Msg { #[derive(Properties, PartialEq)] pub struct AccountsProps { pub account_nrs: Vec, - pub selected_nr: String + pub selected_nr: String, } pub struct Accounts { @@ -49,12 +49,7 @@ impl Component for Accounts { } impl Accounts { - fn account_entry( - &self, - nr: &str, - selected_nr: &str, - ctx: &Context, - ) -> Html { + fn account_entry(&self, nr: &str, selected_nr: &str, ctx: &Context) -> Html { let mut class = Classes::from("accounts__item"); if selected_nr == nr { class.push("accounts__item-selected"); diff --git a/http-client/src/components/main.rs b/http-client/src/components/main.rs index 2e0f5be..9cb643f 100644 --- a/http-client/src/components/main.rs +++ b/http-client/src/components/main.rs @@ -1,8 +1,8 @@ +use crate::api; use crate::components::account::Account; use crate::components::accounts::Accounts; use crate::event_bus::EventBus; use crate::events::Event; -use crate::api; use yew::{classes, html, Component, Context, Html}; use yew_agent::{Bridge, Bridged}; @@ -37,7 +37,7 @@ impl Component for Main { account_nrs: vec![], selected_balance: 0_f64, selected_nr: "".into(), - selected_owner: "".into() + selected_owner: "".into(), } } @@ -85,10 +85,7 @@ impl Component for Main { }); false } - Event::Transfer(amount, from, to) => { - - false - } + Event::Transfer(amount, from, to) => false, } } diff --git a/http-client/src/main.rs b/http-client/src/main.rs index 1e99dbe..8ac260a 100644 --- a/http-client/src/main.rs +++ b/http-client/src/main.rs @@ -1,10 +1,10 @@ use crate::components::main::Main; +mod api; mod client; mod components; mod event_bus; mod events; -mod api; fn main() { yew::start_app::
();