From 04b9e97527eda04a5119d45ff1303965fecd00df Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Mon, 5 Jul 2021 09:21:13 +0200 Subject: [PATCH] Implement client view for the user details --- app/src/api.rs | 22 ++++++++-- app/src/app.rs | 5 ++- app/src/lib.rs | 1 + app/src/user_details.rs | 90 +++++++++++++++++++++++++++++++++++++++ src/infra/auth_service.rs | 4 +- 5 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 app/src/user_details.rs diff --git a/app/src/api.rs b/app/src/api.rs index 8709483..5e42fb9 100644 --- a/app/src/api.rs +++ b/app/src/api.rs @@ -69,9 +69,16 @@ where Req: Into, Result: From> + 'static, { - let request = Request::post(url) - .header("Content-Type", "application/json") - .body(request.into().0)?; + let request = { + // If the request type is empty (if the size is 0), it's a get. + if std::mem::size_of::() == 0 { + Request::get(url) + } else { + Request::post(url) + } + } + .header("Content-Type", "application/json") + .body(request.into().0)?; let handler = create_handler(callback, handler); FetchService::fetch_with_options(request, get_default_options(), handler) } @@ -109,6 +116,15 @@ impl HostService { call_server_json_with_error_message("/api/users", &request, callback, "") } + pub fn get_user_details(username: &str, callback: Callback>) -> Result { + call_server_json_with_error_message( + &format!("/api/user/{}", username), + yew::format::Nothing, + callback, + "", + ) + } + pub fn login_start( request: login::ClientLoginStartRequest, callback: Callback>>, diff --git a/app/src/app.rs b/app/src/app.rs index 4e33964..17f61d9 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -1,6 +1,6 @@ use crate::{ cookies::get_cookie, create_user::CreateUserForm, login::LoginForm, logout::LogoutButton, - user_table::UserTable, + user_details::UserDetails, user_table::UserTable, }; use yew::prelude::*; use yew::services::ConsoleService; @@ -125,7 +125,8 @@ impl Component for App { }, AppRoute::UserDetails(username) => html! {
- {"details about "} {&username} + +
}, } diff --git a/app/src/lib.rs b/app/src/lib.rs index 0be0103..b8fee89 100644 --- a/app/src/lib.rs +++ b/app/src/lib.rs @@ -6,6 +6,7 @@ mod cookies; mod create_user; mod login; mod logout; +mod user_details; mod user_table; use wasm_bindgen::prelude::{wasm_bindgen, JsValue}; diff --git a/app/src/user_details.rs b/app/src/user_details.rs new file mode 100644 index 0000000..c8fcf71 --- /dev/null +++ b/app/src/user_details.rs @@ -0,0 +1,90 @@ +use crate::api::HostService; +use anyhow::{anyhow, Result}; +use lldap_model::*; +use yew::prelude::*; +use yew::services::{fetch::FetchTask, ConsoleService}; + +pub struct UserDetails { + link: ComponentLink, + username: String, + user: Option>, + // Used to keep the request alive long enough. + _task: Option, +} + +pub enum Msg { + UserDetailsResponse(Result), +} + +#[derive(yew::Properties, Clone, PartialEq)] +pub struct Props { + pub username: String, +} + +impl UserDetails { + fn get_user_details(&mut self) { + match HostService::get_user_details( + &self.username, + self.link.callback(Msg::UserDetailsResponse), + ) { + Ok(task) => self._task = Some(task), + Err(e) => { + self._task = None; + ConsoleService::log(format!("Error trying to fetch user details: {}", e).as_str()) + } + }; + } +} + +impl Component for UserDetails { + type Message = Msg; + // The username. + type Properties = Props; + + fn create(props: Self::Properties, link: ComponentLink) -> Self { + let mut table = UserDetails { + link, + username: props.username, + _task: None, + user: None, + }; + table.get_user_details(); + table + } + + fn update(&mut self, msg: Self::Message) -> ShouldRender { + match msg { + Msg::UserDetailsResponse(Ok(user)) => { + self.user = Some(Ok(user)); + true + } + Msg::UserDetailsResponse(Err(e)) => { + self.user = Some(Err(anyhow!("Error getting user details: {}", e))); + true + } + } + } + + fn change(&mut self, _: Self::Properties) -> ShouldRender { + false + } + + fn view(&self) -> Html { + match &self.user { + None => html! {{"Loading..."}}, + Some(Err(e)) => html! {
{"Error: "}{e.to_string()}
}, + Some(Ok(u)) => { + html! { +
+
{"User ID: "} {&u.user_id}
+
{"Email: "}{&u.email}
+
{"Display name: "}{&u.display_name.as_ref().unwrap_or(&String::new())}
+
{"First name: "}{&u.first_name.as_ref().unwrap_or(&String::new())}
+
{"Last name: "}{&u.last_name.as_ref().unwrap_or(&String::new())}
+
{"Creation date: "}{&u.creation_date}
+
+ } + } + } + } +} diff --git a/src/infra/auth_service.rs b/src/infra/auth_service.rs index d7e4fbe..514eeb0 100644 --- a/src/infra/auth_service.rs +++ b/src/infra/auth_service.rs @@ -114,7 +114,7 @@ where .unwrap_or_else(error_to_http_response) } -async fn post_logout( +async fn get_logout( data: web::Data>, request: HttpRequest, ) -> HttpResponse @@ -416,5 +416,5 @@ where .route(web::post().to(opaque_register_finish::)), ) .service(web::resource("/refresh").route(web::get().to(get_refresh::))) - .service(web::resource("/logout").route(web::post().to(post_logout::))); + .service(web::resource("/logout").route(web::get().to(get_logout::))); }