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}
} } } } }