lldap/app/src/app.rs

66 lines
1.6 KiB
Rust
Raw Normal View History

use crate::cookies::get_cookie;
2021-05-13 17:33:57 +00:00
use crate::login::LoginForm;
use crate::logout::LogoutButton;
use crate::user_table::UserTable;
use yew::prelude::*;
use yew::services::ConsoleService;
2021-05-13 17:33:57 +00:00
pub struct App {
link: ComponentLink<Self>,
user_name: Option<String>,
}
pub enum Msg {
Login(String),
Logout,
2021-05-13 17:33:57 +00:00
}
impl Component for App {
type Message = Msg;
type Properties = ();
2021-05-13 17:33:57 +00:00
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
App {
link: link.clone(),
user_name: get_cookie("user_id").unwrap_or_else(|e| {
ConsoleService::error(&e.to_string());
None
}),
2021-05-13 17:33:57 +00:00
}
}
2021-05-13 17:33:57 +00:00
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Login(user_name) => {
self.user_name = Some(user_name);
}
Msg::Logout => {
self.user_name = None;
}
2021-05-13 17:33:57 +00:00
}
true
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
html! {
<div>
<h1>{ "LLDAP" }</h1>
2021-05-13 17:33:57 +00:00
{if self.user_name.is_some() {
html! {
<div>
<LogoutButton on_logged_out=self.link.callback(|_| Msg::Logout) />
<UserTable />
</div>
}
2021-05-13 17:33:57 +00:00
} else {
html! {<LoginForm on_logged_in=self.link.callback(|u| Msg::Login(u))/>}
2021-05-13 17:33:57 +00:00
}}
</div>
}
}
}