73 lines
1.9 KiB
Rust
73 lines
1.9 KiB
Rust
use crate::event_bus::{EventBus, Request};
|
|
use crate::events::Event;
|
|
use yew::{classes, html, Classes, Component, Context, Html, Properties};
|
|
use yew_agent::{Dispatched, Dispatcher};
|
|
|
|
pub enum Msg {
|
|
SelectAccountNr(String),
|
|
}
|
|
|
|
#[derive(Properties, PartialEq)]
|
|
pub struct AccountsProps {
|
|
pub account_nrs: Vec<String>,
|
|
pub selected_nr: String,
|
|
}
|
|
|
|
pub struct Accounts {
|
|
event_bus: Dispatcher<EventBus>,
|
|
}
|
|
|
|
impl Component for Accounts {
|
|
type Message = Msg;
|
|
type Properties = AccountsProps;
|
|
|
|
fn create(_: &Context<Self>) -> Self {
|
|
Self {
|
|
event_bus: EventBus::dispatcher(),
|
|
}
|
|
}
|
|
|
|
fn update(&mut self, _: &Context<Self>, msg: Self::Message) -> bool {
|
|
match msg {
|
|
Msg::SelectAccountNr(nr) => {
|
|
self.event_bus
|
|
.send(Request::EventBusMsg(Event::SelectAccountNr(nr)));
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
|
html! {
|
|
<>
|
|
if ctx.props().account_nrs.is_empty() {
|
|
<h2>{"No accounts"}</h2>
|
|
} else {
|
|
<ul class={classes!("accounts")}>
|
|
{ for ctx.props().account_nrs.iter().map(|e| self.account_entry(e, &ctx.props().selected_nr, ctx)) }
|
|
</ul>
|
|
}
|
|
</>
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Accounts {
|
|
fn account_entry(&self, nr: &str, selected_nr: &str, ctx: &Context<Self>) -> Html {
|
|
let mut class = Classes::from("accounts__item");
|
|
if selected_nr == nr {
|
|
class.push("accounts__item-selected");
|
|
}
|
|
|
|
let nr = nr.to_string();
|
|
let account_nr = nr.clone();
|
|
let onclick = ctx
|
|
.link()
|
|
.callback(move |_| Msg::SelectAccountNr(nr.clone()));
|
|
|
|
html! {
|
|
<li {onclick} class={class}>{account_nr}</li>
|
|
}
|
|
}
|
|
}
|