From c1cb5792fee3ac557cc6455ebad76b2586c9d284 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sun, 30 May 2021 17:02:09 +0200 Subject: [PATCH] Introduce a router for the app --- app/Cargo.toml | 3 +- app/src/app.rs | 77 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 67 insertions(+), 13 deletions(-) diff --git a/app/Cargo.toml b/app/Cargo.toml index d2f0e77..0d7059a 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -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" diff --git a/app/src/app.rs b/app/src/app.rs index 12722f1..354ab71 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -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, user_name: Option, + 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 { - 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! {

{ "LLDAP" }

- {if self.user_name.is_some() { - html! { -
- - -
- } - } else { - html! {} - }} + + render = Router::render(move |switch: AppRoute| { + match switch { + AppRoute::Login => html! { + + }, + AppRoute::Index | AppRoute::ListUsers => html! { +
+ + +
+ } + } + }) + />
} } } + +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() + } + } +}