app: migrate user_details to graphql endpoint

This commit is contained in:
Valentin Tolmer 2021-08-30 09:08:42 +02:00 committed by nitnelave
parent 267ce51973
commit dd7e9c9283
3 changed files with 36 additions and 22 deletions

View File

@ -0,0 +1,10 @@
query GetUserDetails($id: String) {
user(id: $id) {
id
email
displayName
firstName
lastName
creationDate
}
}

View File

@ -162,15 +162,6 @@ impl HostService {
) )
} }
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,9 +1,20 @@
use crate::api::HostService; use crate::api::HostService;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use lldap_model::*; use graphql_client::GraphQLQuery;
use yew::prelude::*; use yew::prelude::*;
use yew::services::{fetch::FetchTask, ConsoleService}; use yew::services::{fetch::FetchTask, ConsoleService};
#[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;
pub struct UserDetails { pub struct UserDetails {
link: ComponentLink<Self>, link: ComponentLink<Self>,
username: String, username: String,
@ -13,7 +24,7 @@ pub struct UserDetails {
} }
pub enum Msg { pub enum Msg {
UserDetailsResponse(Result<User>), UserDetailsResponse(Result<get_user_details::ResponseData>),
} }
#[derive(yew::Properties, Clone, PartialEq)] #[derive(yew::Properties, Clone, PartialEq)]
@ -23,16 +34,18 @@ pub struct Props {
impl UserDetails { impl UserDetails {
fn get_user_details(&mut self) { fn get_user_details(&mut self) {
match HostService::get_user_details( self._task = HostService::graphql_query::<GetUserDetails>(
&self.username, get_user_details::Variables {
id: Some(self.username.clone()),
},
self.link.callback(Msg::UserDetailsResponse), self.link.callback(Msg::UserDetailsResponse),
) { "Error trying to fetch user details",
Ok(task) => self._task = Some(task), )
Err(e) => { .map_err(|e| {
self._task = None; ConsoleService::log(&e.to_string());
ConsoleService::log(format!("Error trying to fetch user details: {}", e).as_str()) e
} })
}; .ok();
} }
} }
@ -55,7 +68,7 @@ impl Component for UserDetails {
fn update(&mut self, msg: Self::Message) -> ShouldRender { fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg { match msg {
Msg::UserDetailsResponse(Ok(user)) => { Msg::UserDetailsResponse(Ok(user)) => {
self.user = Some(Ok(user)); self.user = Some(Ok(user.user));
true true
} }
Msg::UserDetailsResponse(Err(e)) => { Msg::UserDetailsResponse(Err(e)) => {
@ -76,7 +89,7 @@ impl Component for UserDetails {
Some(Ok(u)) => { Some(Ok(u)) => {
html! { html! {
<div> <div>
<div>{"User ID: "} {&u.user_id}</div> <div>{"User ID: "} {&u.id}</div>
<div>{"Email: "}{&u.email}</div> <div>{"Email: "}{&u.email}</div>
<div>{"Display name: "}{&u.display_name.as_ref().unwrap_or(&String::new())}</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>{"First name: "}{&u.first_name.as_ref().unwrap_or(&String::new())}</div>