Implement client view for the user details

This commit is contained in:
Valentin Tolmer 2021-07-05 09:21:13 +02:00 committed by nitnelave
parent 5e0b41998f
commit 04b9e97527
5 changed files with 115 additions and 7 deletions

View File

@ -69,7 +69,14 @@ where
Req: Into<yew::format::Text>, Req: Into<yew::format::Text>,
Result<Resp>: From<Result<String>> + 'static, Result<Resp>: From<Result<String>> + 'static,
{ {
let request = Request::post(url) let request = {
// If the request type is empty (if the size is 0), it's a get.
if std::mem::size_of::<RB>() == 0 {
Request::get(url)
} else {
Request::post(url)
}
}
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.body(request.into().0)?; .body(request.into().0)?;
let handler = create_handler(callback, handler); let handler = create_handler(callback, handler);
@ -109,6 +116,15 @@ impl HostService {
call_server_json_with_error_message("/api/users", &request, callback, "") call_server_json_with_error_message("/api/users", &request, callback, "")
} }
pub fn get_user_details(username: &str, callback: Callback<Result<User>>) -> Result<FetchTask> {
call_server_json_with_error_message(
&format!("/api/user/{}", username),
yew::format::Nothing,
callback,
"",
)
}
pub fn login_start( pub fn login_start(
request: login::ClientLoginStartRequest, request: login::ClientLoginStartRequest,
callback: Callback<Result<Box<login::ServerLoginStartResponse>>>, callback: Callback<Result<Box<login::ServerLoginStartResponse>>>,

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
cookies::get_cookie, create_user::CreateUserForm, login::LoginForm, logout::LogoutButton, cookies::get_cookie, create_user::CreateUserForm, login::LoginForm, logout::LogoutButton,
user_table::UserTable, user_details::UserDetails, user_table::UserTable,
}; };
use yew::prelude::*; use yew::prelude::*;
use yew::services::ConsoleService; use yew::services::ConsoleService;
@ -125,7 +125,8 @@ impl Component for App {
}, },
AppRoute::UserDetails(username) => html! { AppRoute::UserDetails(username) => html! {
<div> <div>
{"details about "} {&username} <LogoutButton on_logged_out=link.callback(|_| Msg::Logout) />
<UserDetails username=username.clone() />
</div> </div>
}, },
} }

View File

@ -6,6 +6,7 @@ mod cookies;
mod create_user; mod create_user;
mod login; mod login;
mod logout; mod logout;
mod user_details;
mod user_table; mod user_table;
use wasm_bindgen::prelude::{wasm_bindgen, JsValue}; use wasm_bindgen::prelude::{wasm_bindgen, JsValue};

90
app/src/user_details.rs Normal file
View File

@ -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<Self>,
username: String,
user: Option<Result<User>>,
// Used to keep the request alive long enough.
_task: Option<FetchTask>,
}
pub enum Msg {
UserDetailsResponse(Result<User>),
}
#[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>) -> 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! {<div>{"Error: "}{e.to_string()}</div>},
Some(Ok(u)) => {
html! {
<div>
<div>{"User ID: "} {&u.user_id}</div>
<div>{"Email: "}{&u.email}</div>
<div>{"Display name: "}{&u.display_name.as_ref().unwrap_or(&String::new())}</div>
<div>{"First name: "}{&u.first_name.as_ref().unwrap_or(&String::new())}</div>
<div>{"Last name: "}{&u.last_name.as_ref().unwrap_or(&String::new())}</div>
<div>{"Creation date: "}{&u.creation_date}</div>
</div>
}
}
}
}
}

View File

@ -114,7 +114,7 @@ where
.unwrap_or_else(error_to_http_response) .unwrap_or_else(error_to_http_response)
} }
async fn post_logout<Backend>( async fn get_logout<Backend>(
data: web::Data<AppState<Backend>>, data: web::Data<AppState<Backend>>,
request: HttpRequest, request: HttpRequest,
) -> HttpResponse ) -> HttpResponse
@ -416,5 +416,5 @@ where
.route(web::post().to(opaque_register_finish::<Backend>)), .route(web::post().to(opaque_register_finish::<Backend>)),
) )
.service(web::resource("/refresh").route(web::get().to(get_refresh::<Backend>))) .service(web::resource("/refresh").route(web::get().to(get_refresh::<Backend>)))
.service(web::resource("/logout").route(web::post().to(post_logout::<Backend>))); .service(web::resource("/logout").route(web::get().to(get_logout::<Backend>)));
} }