64 lines
1.4 KiB
Rust
64 lines
1.4 KiB
Rust
use std::rc::Rc;
|
|
use yew::prelude::*;
|
|
use yew_router::prelude::*;
|
|
use yewdux::prelude::*;
|
|
|
|
use crate::{routes, stores};
|
|
|
|
pub enum Msg {
|
|
UpdateUser(Rc<stores::User>),
|
|
}
|
|
|
|
#[derive(Properties, PartialEq)]
|
|
pub struct Props {
|
|
#[prop_or_default]
|
|
pub children: Children,
|
|
}
|
|
|
|
pub struct LoggedIn {
|
|
user: Rc<stores::User>,
|
|
_user_dispatch: Dispatch<stores::User>,
|
|
}
|
|
|
|
impl Component for LoggedIn {
|
|
type Message = Msg;
|
|
type Properties = Props;
|
|
|
|
fn create(ctx: &Context<Self>) -> Self {
|
|
let user_dispatch = Dispatch::subscribe(ctx.link().callback(Msg::UpdateUser));
|
|
let user = user_dispatch.get();
|
|
|
|
if !user.logged_in() {
|
|
ctx.link().navigator().unwrap().push(&routes::Route::Login);
|
|
}
|
|
|
|
Self {
|
|
user,
|
|
_user_dispatch: user_dispatch,
|
|
}
|
|
}
|
|
|
|
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
|
|
match msg {
|
|
Msg::UpdateUser(x) => {
|
|
let prev = self.user.logged_in();
|
|
self.user = x;
|
|
|
|
if !self.user.logged_in() {
|
|
ctx.link().navigator().unwrap().push(&routes::Route::Login);
|
|
}
|
|
|
|
prev != self.user.logged_in()
|
|
}
|
|
}
|
|
}
|
|
|
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
|
html! {
|
|
if self.user.logged_in() {
|
|
{ for ctx.props().children.iter() }
|
|
}
|
|
}
|
|
}
|
|
}
|