mentorenwahl/frontend/src/layouts/logged_in.rs
Dominic Grimm 8055a5e4db
Some checks failed
continuous-integration/drone/push Build is failing
Update
2023-01-17 06:56:19 +01:00

63 lines
1.5 KiB
Rust

use std::rc::Rc;
use yew::prelude::*;
use yew_router::prelude::*;
use yewdux::prelude::*;
use crate::routes;
use crate::stores;
pub enum Msg {
UpdateLoggedIn(Rc<stores::LoggedIn>),
}
#[derive(Properties, PartialEq)]
pub struct LoggedInProps {
#[prop_or_default]
pub children: Children,
}
pub struct LoggedIn {
logged_in: Rc<stores::LoggedIn>,
_logged_in_dispatch: Dispatch<stores::LoggedIn>, // _logged_in_producer: Box<dyn Bridge<agents::logged_in::EventBus>>,
}
impl Component for LoggedIn {
type Message = Msg;
type Properties = LoggedInProps;
fn create(ctx: &Context<Self>) -> Self {
let dispatch = Dispatch::subscribe(ctx.link().callback(Msg::UpdateLoggedIn));
let logged_in = dispatch.get();
if !logged_in.0 {
ctx.link().navigator().unwrap().push(&routes::Route::Login)
}
Self {
logged_in,
_logged_in_dispatch: dispatch,
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::UpdateLoggedIn(x) => {
self.logged_in = x;
if !self.logged_in.0 {
ctx.link().navigator().unwrap().push(&routes::Route::Login);
}
false
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
if self.logged_in.0 {
{ for ctx.props().children.iter() }
}
}
}
}