Introduce a router for the app

This commit is contained in:
Valentin Tolmer 2021-05-30 17:02:09 +02:00
parent 6ebd18d1e0
commit c1cb5792fe
2 changed files with 67 additions and 13 deletions

View File

@ -13,7 +13,8 @@ lldap_model = { path = "../model" }
serde = "1"
serde_json = "1"
wasm-bindgen = "0.2"
yew = "0.17"
yew = "0.18"
yew-router = "0.15"
[dependencies.web-sys]
version = "0.3"

View File

@ -4,10 +4,19 @@ use crate::logout::LogoutButton;
use crate::user_table::UserTable;
use yew::prelude::*;
use yew::services::ConsoleService;
use yew_router::{
agent::{RouteAgentDispatcher, RouteRequest},
route::Route,
router::Router,
service::RouteService,
Switch,
};
pub struct App {
link: ComponentLink<Self>,
user_name: Option<String>,
redirect_to: String,
route_dispatcher: RouteAgentDispatcher,
}
pub enum Msg {
@ -15,29 +24,55 @@ pub enum Msg {
Logout,
}
#[derive(Switch, Debug, Clone)]
pub enum AppRoute {
#[to = "/login"]
Login,
#[to = "/users"]
ListUsers,
#[to = "/"]
Index,
}
impl Component for App {
type Message = Msg;
type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
App {
let mut app = Self {
link: link.clone(),
user_name: get_cookie("user_id").unwrap_or_else(|e| {
ConsoleService::error(&e.to_string());
None
}),
}
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
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Login(user_name) => {
self.user_name = Some(user_name);
self.route_dispatcher
.send(RouteRequest::ChangeRoute(Route::new_no_state(
&self.redirect_to,
)));
}
Msg::Logout => {
self.user_name = None;
}
}
if self.user_name.is_none() {
self.route_dispatcher
.send(RouteRequest::ReplaceRoute(Route::new_no_state("/login")));
}
true
}
@ -46,20 +81,38 @@ impl Component for App {
}
fn view(&self) -> Html {
let link = self.link.clone();
html! {
<div>
<h1>{ "LLDAP" }</h1>
{if self.user_name.is_some() {
html! {
<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=self.link.callback(|_| Msg::Logout) />
<LogoutButton on_logged_out=link.callback(|_| Msg::Logout) />
<UserTable />
</div>
}
} else {
html! {<LoginForm on_logged_in=self.link.callback(|u| Msg::Login(u))/>}
}}
}
})
/>
</div>
}
}
}
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()
}
}
}