Split the user table into a sub-component

This commit is contained in:
Valentin Tolmer 2021-05-11 09:54:54 +02:00
parent e1047124fa
commit 5615ef8e1f
3 changed files with 88 additions and 49 deletions

View File

@ -1,60 +1,20 @@
use crate::api::HostService; use crate::user_table::UserTable;
use anyhow::Error;
use lldap_model::*;
use yew::format::Json;
use yew::prelude::*; use yew::prelude::*;
use yew::services::{fetch::FetchTask, ConsoleService};
pub struct App { pub struct App {}
link: ComponentLink<Self>,
ipservice: HostService,
task: Option<FetchTask>,
users: Option<Vec<User>>,
}
pub enum Msg { pub enum Msg {}
//BindRequest(BindRequest),
ListUsersRequest(ListUsersRequest),
ListUsersResponse(Result<Vec<User>, Error>),
}
impl Component for App { impl Component for App {
type Message = Msg; type Message = Msg;
type Properties = (); type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self { fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
App { App {}
link: link.clone(),
ipservice: HostService::default(),
task: None,
users: None,
}
} }
fn update(&mut self, msg: Self::Message) -> ShouldRender { fn update(&mut self, _msg: Self::Message) -> ShouldRender {
match msg { false
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 change(&mut self, _: Self::Properties) -> ShouldRender { fn change(&mut self, _: Self::Properties) -> ShouldRender {
@ -65,8 +25,7 @@ impl Component for App {
html! { html! {
<div> <div>
<h1>{ "LLDAP" }</h1> <h1>{ "LLDAP" }</h1>
<button onclick=self.link.callback(|_| Msg::ListUsersRequest(ListUsersRequest{filters: None}))>{ "Fetch users" }</button> <UserTable />
<p>{ format!("Users: {:?}", &self.users) }</p>
</div> </div>
} }
} }

View File

@ -1,6 +1,7 @@
#![recursion_limit = "256"] #![recursion_limit = "256"]
mod api; mod api;
mod app; mod app;
mod user_table;
use wasm_bindgen::prelude::{wasm_bindgen, JsValue}; use wasm_bindgen::prelude::{wasm_bindgen, JsValue};

79
app/src/user_table.rs Normal file
View File

@ -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<Self>,
ipservice: HostService,
task: Option<FetchTask>,
users: Option<Result<Vec<User>>>,
}
pub enum Msg {
ListUsersResponse(Result<Vec<User>>),
}
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>) -> 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! {
<p>{
match &self.users {
None => "Loading...".to_string(),
Some(Ok(users)) => format!("Users: {:?}", &users),
Some(Err(e)) => e.to_string(),
}
}</p>
}
}
}