2021-07-05 07:21:13 +00:00
|
|
|
use crate::api::HostService;
|
|
|
|
use anyhow::{anyhow, Result};
|
2021-08-30 07:08:42 +00:00
|
|
|
use graphql_client::GraphQLQuery;
|
2021-07-05 07:21:13 +00:00
|
|
|
use yew::prelude::*;
|
|
|
|
use yew::services::{fetch::FetchTask, ConsoleService};
|
|
|
|
|
2021-08-30 07:08:42 +00:00
|
|
|
#[derive(GraphQLQuery)]
|
|
|
|
#[graphql(
|
|
|
|
schema_path = "../schema.graphql",
|
|
|
|
query_path = "queries/get_user_details.graphql",
|
|
|
|
response_derives = "Debug",
|
|
|
|
custom_scalars_module = "crate::graphql"
|
|
|
|
)]
|
|
|
|
pub struct GetUserDetails;
|
|
|
|
|
|
|
|
type User = get_user_details::GetUserDetailsUser;
|
|
|
|
|
2021-07-05 07:21:13 +00:00
|
|
|
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 {
|
2021-08-30 07:08:42 +00:00
|
|
|
UserDetailsResponse(Result<get_user_details::ResponseData>),
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(yew::Properties, Clone, PartialEq)]
|
|
|
|
pub struct Props {
|
|
|
|
pub username: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserDetails {
|
|
|
|
fn get_user_details(&mut self) {
|
2021-08-30 07:08:42 +00:00
|
|
|
self._task = HostService::graphql_query::<GetUserDetails>(
|
|
|
|
get_user_details::Variables {
|
2021-08-30 07:49:13 +00:00
|
|
|
id: self.username.clone(),
|
2021-08-30 07:08:42 +00:00
|
|
|
},
|
2021-07-05 07:21:13 +00:00
|
|
|
self.link.callback(Msg::UserDetailsResponse),
|
2021-08-30 07:08:42 +00:00
|
|
|
"Error trying to fetch user details",
|
|
|
|
)
|
|
|
|
.map_err(|e| {
|
|
|
|
ConsoleService::log(&e.to_string());
|
|
|
|
e
|
|
|
|
})
|
|
|
|
.ok();
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) => {
|
2021-08-30 07:08:42 +00:00
|
|
|
self.user = Some(Ok(user.user));
|
2021-07-05 07:21:13 +00:00
|
|
|
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>
|
2021-08-30 07:08:42 +00:00
|
|
|
<div>{"User ID: "} {&u.id}</div>
|
2021-07-05 07:21:13 +00:00
|
|
|
<div>{"Email: "}{&u.email}</div>
|
2021-09-01 07:59:01 +00:00
|
|
|
<div>{"Display name: "}{&u.display_name}</div>
|
|
|
|
<div>{"First name: "}{&u.first_name}</div>
|
|
|
|
<div>{"Last name: "}{&u.last_name}</div>
|
2021-08-31 18:18:19 +00:00
|
|
|
<div>{"Creation date: "}{&u.creation_date.with_timezone(&chrono::Local)}</div>
|
2021-07-05 07:21:13 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|