use crate::infra::api::HostService; use anyhow::{anyhow, Result}; use graphql_client::GraphQLQuery; use yew::format::Json; use yew::prelude::*; use yew::services::{fetch::FetchTask, ConsoleService}; #[derive(GraphQLQuery)] #[graphql( schema_path = "../schema.graphql", query_path = "queries/list_users.graphql", response_derives = "Debug", custom_scalars_module = "crate::infra::graphql" )] pub struct ListUsersQuery; use list_users_query::{RequestFilter, ResponseData}; type User = list_users_query::ListUsersQueryUsers; pub struct UserTable { link: ComponentLink, users: Option>>, // Used to keep the request alive long enough. _task: Option, } pub enum Msg { ListUsersResponse(Result), } impl UserTable { fn get_users(&mut self, req: Option) { self._task = HostService::graphql_query::( list_users_query::Variables { filters: req }, self.link.callback(Msg::ListUsersResponse), "Error trying to fetch users", ) .map_err(|e| { ConsoleService::log(&e.to_string()); e }) .ok(); } } impl Component for UserTable { type Message = Msg; type Properties = (); fn create(_: Self::Properties, link: ComponentLink) -> Self { let mut table = UserTable { link, _task: None, users: None, }; table.get_users(None); table } fn update(&mut self, msg: Self::Message) -> ShouldRender { match msg { Msg::ListUsersResponse(Ok(users)) => { self.users = Some(Ok(users.users.into_iter().collect())); ConsoleService::log(format!("Response: {:?}", Json(&self.users)).as_str()); true } Msg::ListUsersResponse(Err(e)) => { self.users = Some(Err(anyhow!("Error listing users: {}", e))); true } } } fn change(&mut self, _: Self::Properties) -> ShouldRender { false } fn view(&self) -> Html { let make_user_row = |user: &User| { html! { {&user.id} {&user.email} {&user.display_name} {&user.first_name} {&user.last_name} {&user.creation_date.with_timezone(&chrono::Local)} } }; let make_table = |users: &Vec| { html! { {users.iter().map(make_user_row).collect::>()}
{"User ID"} {"Email"} {"Display name"} {"First name"} {"Last name"} {"Creation date"}
} }; match &self.users { None => html! {{"Loading..."}}, Some(Err(e)) => html! {
{"Error: "}{e.to_string()}
}, Some(Ok(users)) => make_table(users), } } }