mirror of
https://github.com/nitnelave/lldap.git
synced 2023-04-12 14:25:13 +00:00
Introduce a router for the app
This commit is contained in:
parent
6ebd18d1e0
commit
c1cb5792fe
@ -13,7 +13,8 @@ lldap_model = { path = "../model" }
|
|||||||
serde = "1"
|
serde = "1"
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
wasm-bindgen = "0.2"
|
wasm-bindgen = "0.2"
|
||||||
yew = "0.17"
|
yew = "0.18"
|
||||||
|
yew-router = "0.15"
|
||||||
|
|
||||||
[dependencies.web-sys]
|
[dependencies.web-sys]
|
||||||
version = "0.3"
|
version = "0.3"
|
||||||
|
@ -4,10 +4,19 @@ use crate::logout::LogoutButton;
|
|||||||
use crate::user_table::UserTable;
|
use crate::user_table::UserTable;
|
||||||
use yew::prelude::*;
|
use yew::prelude::*;
|
||||||
use yew::services::ConsoleService;
|
use yew::services::ConsoleService;
|
||||||
|
use yew_router::{
|
||||||
|
agent::{RouteAgentDispatcher, RouteRequest},
|
||||||
|
route::Route,
|
||||||
|
router::Router,
|
||||||
|
service::RouteService,
|
||||||
|
Switch,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
link: ComponentLink<Self>,
|
link: ComponentLink<Self>,
|
||||||
user_name: Option<String>,
|
user_name: Option<String>,
|
||||||
|
redirect_to: String,
|
||||||
|
route_dispatcher: RouteAgentDispatcher,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
@ -15,29 +24,55 @@ pub enum Msg {
|
|||||||
Logout,
|
Logout,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Switch, Debug, Clone)]
|
||||||
|
pub enum AppRoute {
|
||||||
|
#[to = "/login"]
|
||||||
|
Login,
|
||||||
|
#[to = "/users"]
|
||||||
|
ListUsers,
|
||||||
|
#[to = "/"]
|
||||||
|
Index,
|
||||||
|
}
|
||||||
|
|
||||||
impl Component for App {
|
impl Component for App {
|
||||||
type Message = Msg;
|
type Message = Msg;
|
||||||
type Properties = ();
|
type Properties = ();
|
||||||
|
|
||||||
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||||
App {
|
let mut app = Self {
|
||||||
link: link.clone(),
|
link: link.clone(),
|
||||||
user_name: get_cookie("user_id").unwrap_or_else(|e| {
|
user_name: get_cookie("user_id").unwrap_or_else(|e| {
|
||||||
ConsoleService::error(&e.to_string());
|
ConsoleService::error(&e.to_string());
|
||||||
None
|
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 {
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
||||||
match msg {
|
match msg {
|
||||||
Msg::Login(user_name) => {
|
Msg::Login(user_name) => {
|
||||||
self.user_name = Some(user_name);
|
self.user_name = Some(user_name);
|
||||||
|
self.route_dispatcher
|
||||||
|
.send(RouteRequest::ChangeRoute(Route::new_no_state(
|
||||||
|
&self.redirect_to,
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
Msg::Logout => {
|
Msg::Logout => {
|
||||||
self.user_name = None;
|
self.user_name = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if self.user_name.is_none() {
|
||||||
|
self.route_dispatcher
|
||||||
|
.send(RouteRequest::ReplaceRoute(Route::new_no_state("/login")));
|
||||||
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,20 +81,38 @@ impl Component for App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Html {
|
fn view(&self) -> Html {
|
||||||
|
let link = self.link.clone();
|
||||||
html! {
|
html! {
|
||||||
<div>
|
<div>
|
||||||
<h1>{ "LLDAP" }</h1>
|
<h1>{ "LLDAP" }</h1>
|
||||||
{if self.user_name.is_some() {
|
<Router<AppRoute>
|
||||||
html! {
|
render = Router::render(move |switch: AppRoute| {
|
||||||
<div>
|
match switch {
|
||||||
<LogoutButton on_logged_out=self.link.callback(|_| Msg::Logout) />
|
AppRoute::Login => html! {
|
||||||
<UserTable />
|
<LoginForm on_logged_in=link.callback(|u| Msg::Login(u))/>
|
||||||
</div>
|
},
|
||||||
}
|
AppRoute::Index | AppRoute::ListUsers => html! {
|
||||||
} else {
|
<div>
|
||||||
html! {<LoginForm on_logged_in=self.link.callback(|u| Msg::Login(u))/>}
|
<LogoutButton on_logged_out=link.callback(|_| Msg::Logout) />
|
||||||
}}
|
<UserTable />
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
/>
|
||||||
</div>
|
</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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user