kanidm/kanidmd_web_ui/src/manager.rs

107 lines
2.9 KiB
Rust
Raw Normal View History

2021-07-25 02:51:37 +02:00
//! This is the top level router of the web ui for kanidm. It decides based on the incoming
//! request, where to direct this too, and if the requirements for that request have been
//! met before rendering. For example, if you land here with an oauth request, but you are
//! not atuhenticated, this will determine that and send you to authentication first, then
//! will allow you to proceed with the oauth flow.
2021-12-31 00:11:20 +01:00
use gloo::console;
use wasm_bindgen::UnwrapThrowExt;
2021-12-31 00:11:20 +01:00
use yew::functional::*;
2021-07-25 02:51:37 +02:00
use yew::prelude::*;
use yew_router::prelude::*;
use crate::credential::reset::CredentialResetApp;
2021-07-25 02:51:37 +02:00
use crate::login::LoginApp;
use crate::oauth2::Oauth2App;
use crate::views::{ViewRoute, ViewsApp};
use serde::{Deserialize, Serialize};
2021-07-25 02:51:37 +02:00
// router to decide on state.
#[derive(Routable, PartialEq, Clone, Debug, Serialize, Deserialize)]
2021-07-25 02:51:37 +02:00
pub enum Route {
#[at("/")]
Landing,
#[at("/ui/view/:s")]
Views,
2021-07-25 02:51:37 +02:00
#[at("/ui/login")]
Login,
#[at("/ui/oauth2")]
Oauth2,
#[at("/ui/reset")]
CredentialReset,
2021-07-25 02:51:37 +02:00
#[not_found]
#[at("/ui/404")]
2021-07-25 02:51:37 +02:00
NotFound,
}
2021-12-31 00:11:20 +01:00
#[function_component(Landing)]
fn landing() -> Html {
// Do this to allow use_history to work because lol.
use_history()
.expect_throw("Unable to access history")
.push(ViewRoute::Apps);
html! { <main></main> }
2021-12-31 00:11:20 +01:00
}
fn switch(route: &Route) -> Html {
2021-12-31 00:11:20 +01:00
console::log!("manager::switch");
match route {
2021-12-31 00:11:20 +01:00
Route::Landing => html! { <Landing /> },
2021-07-25 02:51:37 +02:00
Route::Login => html! { <LoginApp /> },
Route::Oauth2 => html! { <Oauth2App /> },
Route::Views => html! { <ViewsApp /> },
Route::CredentialReset => html! { <CredentialResetApp /> },
2021-07-25 02:51:37 +02:00
Route::NotFound => {
html! {
<main>
2021-07-25 02:51:37 +02:00
<h1>{ "404" }</h1>
<Link<ViewRoute> to={ ViewRoute::Apps }>
2021-07-25 02:51:37 +02:00
{ "Home" }
</Link<ViewRoute>>
</main>
2021-07-25 02:51:37 +02:00
}
}
}
}
pub struct ManagerApp {}
2021-07-25 02:51:37 +02:00
impl Component for ManagerApp {
type Message = ();
2021-07-25 02:51:37 +02:00
type Properties = ();
2021-12-31 00:11:20 +01:00
fn create(_ctx: &Context<Self>) -> Self {
console::log!("manager::create");
ManagerApp {}
2021-07-25 02:51:37 +02:00
}
2021-12-31 00:11:20 +01:00
fn changed(&mut self, _ctx: &Context<Self>) -> bool {
console::log!("manager::change");
2021-07-25 02:51:37 +02:00
false
}
fn update(&mut self, _ctx: &Context<Self>, _msg: Self::Message) -> bool {
2021-12-31 00:11:20 +01:00
console::log!("manager::update");
2021-07-25 02:51:37 +02:00
true
}
fn rendered(&mut self, _ctx: &Context<Self>, _first_render: bool) {
2021-12-31 00:11:20 +01:00
console::log!("manager::rendered");
// Can only access the current_route AFTER it renders.
// console::log!(format!("{:?}", yew_router::current_route::<Route>()).as_str())
2021-07-25 02:51:37 +02:00
}
2021-12-31 00:11:20 +01:00
fn view(&self, _ctx: &Context<Self>) -> Html {
2021-07-25 02:51:37 +02:00
html! {
<BrowserRouter>
<Switch<Route> render={ Switch::render(switch) } />
</BrowserRouter>
2021-07-25 02:51:37 +02:00
}
}
}