mentorenwahl/frontend/src/layouts/main.rs
Dominic Grimm ff2b884d42
All checks were successful
continuous-integration/drone/push Build is passing
Agent oriented logged in state
2022-11-05 21:27:49 +01:00

80 lines
2 KiB
Rust

use yew::prelude::*;
use yew_agent::{Dispatched, Dispatcher};
use yew_router::prelude::*;
use crate::agents;
use crate::components;
use crate::routes;
pub enum Msg {
LogOut,
}
#[derive(Properties, PartialEq)]
pub struct MainProps {
pub logged_in: bool,
#[prop_or_default]
pub children: Children,
}
pub struct Main {
logged_in: bool,
logged_in_event_bus: Dispatcher<agents::logged_in::EventBus>,
}
impl Component for Main {
type Message = Msg;
type Properties = MainProps;
fn create(ctx: &Context<Self>) -> Self {
Self {
logged_in: ctx.props().logged_in,
logged_in_event_bus: agents::logged_in::EventBus::dispatcher(),
}
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::LogOut => {
self.logged_in_event_bus
.send(agents::logged_in::Request::LoggedIn(false));
false
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let logout_onclick = ctx.link().callback(move |_| Msg::LogOut);
html! {
<>
<nav>
<ul>
<li>
<Link<routes::Route> to={routes::Route::Home}>
<button>{ "Home" }</button>
</Link<routes::Route>>
</li>
<li>
if self.logged_in {
<button onclick={logout_onclick}>{ "Logout" }</button>
} else {
<Link<routes::Route> to={routes::Route::Login}>
<button>{ "Login" }</button>
</Link<routes::Route>>
}
</li>
</ul>
</nav>
<hr />
<components::logged_in_handler::LoggedInHandler logged_in={self.logged_in} />
<main>
{ for ctx.props().children.iter() }
</main>
</>
}
}
}