2021-05-13 17:33:57 +00:00
|
|
|
use crate::login::LoginForm;
|
2021-05-11 07:54:54 +00:00
|
|
|
use crate::user_table::UserTable;
|
2021-05-13 17:33:57 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use wasm_bindgen::JsCast;
|
2021-05-08 09:34:55 +00:00
|
|
|
use yew::prelude::*;
|
|
|
|
|
2021-05-13 17:33:57 +00:00
|
|
|
pub struct App {
|
|
|
|
link: ComponentLink<Self>,
|
|
|
|
user_name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Msg {
|
|
|
|
Login(String),
|
|
|
|
}
|
2021-05-08 09:34:55 +00:00
|
|
|
|
2021-05-13 17:33:57 +00:00
|
|
|
fn extract_user_id_cookie() -> Result<String> {
|
|
|
|
let document = web_sys::window()
|
|
|
|
.unwrap()
|
|
|
|
.document()
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into::<web_sys::HtmlDocument>()
|
|
|
|
.unwrap();
|
|
|
|
let cookies = document.cookie().unwrap();
|
|
|
|
yew::services::ConsoleService::info(&cookies);
|
|
|
|
cookies
|
|
|
|
.split(";")
|
|
|
|
.filter_map(|c| c.split_once('='))
|
|
|
|
.map(|(name, value)| {
|
|
|
|
if name == "user_id" {
|
|
|
|
Ok(value.into())
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("Wrong cookie"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(Result::is_ok)
|
|
|
|
.next()
|
|
|
|
.unwrap_or(Err(anyhow!("User ID cookie not found in response")))
|
|
|
|
}
|
2021-05-08 09:34:55 +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: extract_user_id_cookie().ok(),
|
|
|
|
}
|
2021-05-08 09:34:55 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
2021-05-08 09:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
|
|
|
html! {
|
2021-05-09 11:26:50 +00:00
|
|
|
<div>
|
|
|
|
<h1>{ "LLDAP" }</h1>
|
2021-05-13 17:33:57 +00:00
|
|
|
{if self.user_name.is_some() {
|
|
|
|
html! {<UserTable />}
|
|
|
|
} else {
|
|
|
|
html! {<LoginForm on_logged_in=self.link.callback(|u| { Msg::Login(u) })/>}
|
|
|
|
}}
|
2021-05-09 11:26:50 +00:00
|
|
|
</div>
|
2021-05-08 09:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|