2021-05-18 17:04:06 +00:00
|
|
|
use crate::cookies::get_cookie;
|
2021-05-13 17:33:57 +00:00
|
|
|
use crate::login::LoginForm;
|
2021-05-18 17:04:06 +00:00
|
|
|
use crate::logout::LogoutButton;
|
2021-05-11 07:54:54 +00:00
|
|
|
use crate::user_table::UserTable;
|
2021-05-08 09:34:55 +00:00
|
|
|
use yew::prelude::*;
|
2021-05-18 17:04:06 +00:00
|
|
|
use yew::services::ConsoleService;
|
2021-05-30 15:02:09 +00:00
|
|
|
use yew_router::{
|
|
|
|
agent::{RouteAgentDispatcher, RouteRequest},
|
|
|
|
route::Route,
|
|
|
|
router::Router,
|
|
|
|
service::RouteService,
|
|
|
|
Switch,
|
|
|
|
};
|
2021-05-08 09:34:55 +00:00
|
|
|
|
2021-05-13 17:33:57 +00:00
|
|
|
pub struct App {
|
|
|
|
link: ComponentLink<Self>,
|
|
|
|
user_name: Option<String>,
|
2021-05-30 15:02:09 +00:00
|
|
|
redirect_to: String,
|
|
|
|
route_dispatcher: RouteAgentDispatcher,
|
2021-05-13 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Msg {
|
|
|
|
Login(String),
|
2021-05-18 17:04:06 +00:00
|
|
|
Logout,
|
2021-05-13 17:33:57 +00:00
|
|
|
}
|
2021-05-08 09:34:55 +00:00
|
|
|
|
2021-05-30 15:02:09 +00:00
|
|
|
#[derive(Switch, Debug, Clone)]
|
|
|
|
pub enum AppRoute {
|
|
|
|
#[to = "/login"]
|
|
|
|
Login,
|
|
|
|
#[to = "/users"]
|
|
|
|
ListUsers,
|
|
|
|
#[to = "/"]
|
|
|
|
Index,
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-05-30 15:02:09 +00:00
|
|
|
let mut app = Self {
|
2021-05-13 17:33:57 +00:00
|
|
|
link: link.clone(),
|
2021-05-18 17:04:06 +00:00
|
|
|
user_name: get_cookie("user_id").unwrap_or_else(|e| {
|
|
|
|
ConsoleService::error(&e.to_string());
|
|
|
|
None
|
|
|
|
}),
|
2021-05-30 15:02:09 +00:00
|
|
|
redirect_to: Self::get_redirect_route(),
|
|
|
|
route_dispatcher: RouteAgentDispatcher::new(),
|
|
|
|
};
|
|
|
|
if app.user_name.is_none() {
|
|
|
|
ConsoleService::info("Redirecting to login");
|
|
|
|
app.route_dispatcher
|
|
|
|
.send(RouteRequest::ReplaceRoute(Route::new_no_state("/login")));
|
|
|
|
};
|
|
|
|
app
|
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);
|
2021-05-30 15:02:09 +00:00
|
|
|
self.route_dispatcher
|
|
|
|
.send(RouteRequest::ChangeRoute(Route::new_no_state(
|
|
|
|
&self.redirect_to,
|
|
|
|
)));
|
2021-05-13 17:33:57 +00:00
|
|
|
}
|
2021-05-18 17:04:06 +00:00
|
|
|
Msg::Logout => {
|
|
|
|
self.user_name = None;
|
|
|
|
}
|
2021-05-13 17:33:57 +00:00
|
|
|
}
|
2021-05-30 15:02:09 +00:00
|
|
|
if self.user_name.is_none() {
|
|
|
|
self.route_dispatcher
|
|
|
|
.send(RouteRequest::ReplaceRoute(Route::new_no_state("/login")));
|
|
|
|
}
|
2021-05-13 17:33:57 +00:00
|
|
|
true
|
2021-05-08 09:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
2021-05-30 15:02:09 +00:00
|
|
|
let link = self.link.clone();
|
2021-05-08 09:34:55 +00:00
|
|
|
html! {
|
2021-05-09 11:26:50 +00:00
|
|
|
<div>
|
|
|
|
<h1>{ "LLDAP" }</h1>
|
2021-05-30 15:02:09 +00:00
|
|
|
<Router<AppRoute>
|
|
|
|
render = Router::render(move |switch: AppRoute| {
|
|
|
|
match switch {
|
|
|
|
AppRoute::Login => html! {
|
|
|
|
<LoginForm on_logged_in=link.callback(|u| Msg::Login(u))/>
|
|
|
|
},
|
|
|
|
AppRoute::Index | AppRoute::ListUsers => html! {
|
|
|
|
<div>
|
|
|
|
<LogoutButton on_logged_out=link.callback(|_| Msg::Logout) />
|
|
|
|
<UserTable />
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
/>
|
2021-05-09 11:26:50 +00:00
|
|
|
</div>
|
2021-05-08 09:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 15:02:09 +00:00
|
|
|
|
|
|
|
impl App {
|
|
|
|
fn get_redirect_route() -> String {
|
|
|
|
let route_service = RouteService::<()>::new();
|
|
|
|
let current_route = route_service.get_path();
|
|
|
|
if current_route.is_empty() || current_route.contains("login") {
|
|
|
|
String::from("/")
|
|
|
|
} else {
|
|
|
|
current_route.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|