mentorenwahl/frontend/src/components/logged_in_handler.rs

55 lines
1.3 KiB
Rust

use yew::prelude::*;
use yew_agent::{Bridge, Bridged};
use yew_router::prelude::*;
use crate::agents;
use crate::cookies;
use crate::routes;
pub enum Msg {
LoggedIn(bool),
}
#[derive(Properties, PartialEq)]
pub struct LoggedInHandlerProps {
pub logged_in: bool,
}
pub struct LoggedInHandler {
logged_in: bool,
_logged_in_producer: Box<dyn Bridge<agents::logged_in::EventBus>>,
}
impl Component for LoggedInHandler {
type Message = Msg;
type Properties = LoggedInHandlerProps;
fn create(ctx: &Context<Self>) -> Self {
Self {
logged_in: ctx.props().logged_in,
_logged_in_producer: agents::logged_in::EventBus::bridge(
ctx.link().callback(Msg::LoggedIn),
),
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::LoggedIn(x) => {
if self.logged_in && !x {
log::info!("Global logout!");
cookies::logout_clear();
ctx.link().history().unwrap().push(routes::Route::Login);
}
self.logged_in = x;
false
}
}
}
fn view(&self, _ctx: &Context<Self>) -> Html {
html! {}
}
}