60 lines
1.4 KiB
Rust
60 lines
1.4 KiB
Rust
use yew::prelude::*;
|
|
use yew_agent::{Bridge, Bridged};
|
|
use yew_router::prelude::*;
|
|
|
|
use crate::agents;
|
|
use crate::routes;
|
|
|
|
pub enum Msg {
|
|
LoggedIn(bool),
|
|
}
|
|
|
|
#[derive(Properties, PartialEq)]
|
|
pub struct LoggedInProps {
|
|
pub logged_in: bool,
|
|
#[prop_or_default]
|
|
pub children: Children,
|
|
}
|
|
|
|
pub struct LoggedIn {
|
|
logged_in: bool,
|
|
_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 {
|
|
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) => {
|
|
let prev = self.logged_in;
|
|
self.logged_in = x;
|
|
prev != self.logged_in
|
|
}
|
|
}
|
|
}
|
|
|
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
|
if !self.logged_in {
|
|
log::info!("Viewing logged in required site while not logged in!");
|
|
ctx.link().history().unwrap().push(routes::Route::Login);
|
|
}
|
|
|
|
html! {
|
|
if self.logged_in {
|
|
{ for ctx.props().children.iter() }
|
|
}
|
|
}
|
|
}
|
|
}
|