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}; pub struct Main { _subscriber: Box>, error: Option, account_nrs: Vec, selected_balance: f64, selected_owner: String, selected_nr: String, } impl Main { fn set_selected_account(&self, ctx: &Context, nr: String) { ctx.link().send_future(async move { match api::fetch_account("http://localhost:8000", &nr).await { Err(e) => Event::ShowError(e.to_string()), Ok(account) => Event::SetSelectedAccount(account), } }); } } impl Component for Main { type Message = Event; type Properties = (); fn create(ctx: &Context) -> Self { Self { error: None, _subscriber: EventBus::bridge(ctx.link().callback(|x| x)), account_nrs: vec![], selected_balance: 0_f64, selected_nr: "".into(), selected_owner: "".into() } } fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { match msg { Event::GetAccountNrs => { ctx.link().send_future(async { match api::fetch_account_nrs("http://localhost:8000").await { Err(e) => Event::ShowError(e.to_string()), Ok(nrs) => Event::SetAccountNrs(nrs), } }); false } Event::SetAccountNrs(nrs) => { if self.account_nrs.is_empty() && !nrs.is_empty() { let nr = nrs[0].clone(); self.set_selected_account(ctx, nr); } self.account_nrs = nrs; true } Event::ShowError(error) => { self.error = Some(error); true } Event::SetSelectedAccount(account) => { self.selected_balance = account.balance; self.selected_owner = account.owner; self.selected_nr = account.number; true } Event::SelectAccountNr(nr) => { self.set_selected_account(ctx, nr); true } Event::SetBalance(balance, nr) => { ctx.link().send_future(async move { match api::set_balance("http://localhost:8000", &nr, balance).await { Err(e) => Event::ShowError(e.to_string()), Ok(_) => Event::SelectAccountNr(nr), } }); false } Event::Transfer(amount, from, to) => { false } } } fn view(&self, _: &Context) -> Html { html! { <> if let Some(error_msg) = &self.error {
{ error_msg }
}

{"welcome to your vaults"}

} } fn rendered(&mut self, ctx: &Context, first_render: bool) { if first_render { ctx.link().send_message(Event::GetAccountNrs); } } }