From 5615ef8e1f72d9eefda04108be67143cc7643b41 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Tue, 11 May 2021 09:54:54 +0200 Subject: [PATCH] Split the user table into a sub-component --- app/src/app.rs | 57 +++++-------------------------- app/src/lib.rs | 1 + app/src/user_table.rs | 79 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 49 deletions(-) create mode 100644 app/src/user_table.rs diff --git a/app/src/app.rs b/app/src/app.rs index 225b201..0cb046a 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -1,60 +1,20 @@ -use crate::api::HostService; -use anyhow::Error; -use lldap_model::*; -use yew::format::Json; +use crate::user_table::UserTable; use yew::prelude::*; -use yew::services::{fetch::FetchTask, ConsoleService}; -pub struct App { - link: ComponentLink, - ipservice: HostService, - task: Option, - users: Option>, -} +pub struct App {} -pub enum Msg { - //BindRequest(BindRequest), - ListUsersRequest(ListUsersRequest), - ListUsersResponse(Result, Error>), -} +pub enum Msg {} impl Component for App { type Message = Msg; type Properties = (); - fn create(_: Self::Properties, link: ComponentLink) -> Self { - App { - link: link.clone(), - ipservice: HostService::default(), - task: None, - users: None, - } + fn create(_: Self::Properties, _link: ComponentLink) -> Self { + App {} } - fn update(&mut self, msg: Self::Message) -> ShouldRender { - match msg { - Msg::ListUsersRequest(req) => { - match self - .ipservice - .list_users(req, self.link.callback(Msg::ListUsersResponse)) - { - Ok(task) => self.task = Some(task), - Err(e) => { - self.task = None; - ConsoleService::log(format!("Error trying to fetch users: {}", e).as_str()) - } - } - } - Msg::ListUsersResponse(Ok(users)) => { - self.users = Some(users); - ConsoleService::log(format!("Response: {:?}", Json(&self.users)).as_str()); - } - Msg::ListUsersResponse(Err(e)) => { - self.task = None; - ConsoleService::log(format!("Error listing users: {}", e).as_str()) - } - } - true + fn update(&mut self, _msg: Self::Message) -> ShouldRender { + false } fn change(&mut self, _: Self::Properties) -> ShouldRender { @@ -65,8 +25,7 @@ impl Component for App { html! {

{ "LLDAP" }

- -

{ format!("Users: {:?}", &self.users) }

+
} } diff --git a/app/src/lib.rs b/app/src/lib.rs index 4b33a41..c7192b4 100644 --- a/app/src/lib.rs +++ b/app/src/lib.rs @@ -1,6 +1,7 @@ #![recursion_limit = "256"] mod api; mod app; +mod user_table; use wasm_bindgen::prelude::{wasm_bindgen, JsValue}; diff --git a/app/src/user_table.rs b/app/src/user_table.rs new file mode 100644 index 0000000..7fac30a --- /dev/null +++ b/app/src/user_table.rs @@ -0,0 +1,79 @@ +use crate::api::HostService; +use anyhow::{anyhow, Result}; +use lldap_model::*; +use yew::format::Json; +use yew::prelude::*; +use yew::services::{fetch::FetchTask, ConsoleService}; + +pub struct UserTable { + link: ComponentLink, + ipservice: HostService, + task: Option, + users: Option>>, +} + +pub enum Msg { + ListUsersResponse(Result>), +} + +impl UserTable { + fn get_users(&mut self, req: ListUsersRequest) { + match self + .ipservice + .list_users(req, self.link.callback(Msg::ListUsersResponse)) + { + Ok(task) => self.task = Some(task), + Err(e) => { + self.task = None; + ConsoleService::log(format!("Error trying to fetch users: {}", e).as_str()) + } + }; + } +} + +impl Component for UserTable { + type Message = Msg; + type Properties = (); + + fn create(_: Self::Properties, link: ComponentLink) -> Self { + let mut table = UserTable { + link: link.clone(), + ipservice: HostService::default(), + task: None, + users: None, + }; + table.get_users(ListUsersRequest { filters: None }); + table + } + + fn update(&mut self, msg: Self::Message) -> ShouldRender { + match msg { + Msg::ListUsersResponse(Ok(users)) => { + self.users = Some(Ok(users)); + ConsoleService::log(format!("Response: {:?}", Json(&self.users)).as_str()); + true + } + Msg::ListUsersResponse(Err(e)) => { + self.task = None; + self.users = Some(Err(anyhow!("Error listing users: {}", e))); + true + } + } + } + + fn change(&mut self, _: Self::Properties) -> ShouldRender { + false + } + + fn view(&self) -> Html { + html! { +

{ + match &self.users { + None => "Loading...".to_string(), + Some(Ok(users)) => format!("Users: {:?}", &users), + Some(Err(e)) => e.to_string(), + } + }

+ } + } +}