2021-05-09 11:26:50 +00:00
|
|
|
use crate::api::HostService;
|
|
|
|
use anyhow::Error;
|
2021-05-08 09:34:55 +00:00
|
|
|
use lldap_model::*;
|
2021-05-09 11:26:50 +00:00
|
|
|
use yew::format::Json;
|
2021-05-08 09:34:55 +00:00
|
|
|
use yew::prelude::*;
|
2021-05-09 11:26:50 +00:00
|
|
|
use yew::services::{fetch::FetchTask, ConsoleService};
|
2021-05-08 09:34:55 +00:00
|
|
|
|
2021-05-09 11:26:50 +00:00
|
|
|
pub struct App {
|
|
|
|
link: ComponentLink<Self>,
|
|
|
|
ipservice: HostService,
|
|
|
|
task: Option<FetchTask>,
|
|
|
|
users: Option<Vec<User>>,
|
|
|
|
}
|
2021-05-08 09:34:55 +00:00
|
|
|
|
|
|
|
pub enum Msg {
|
2021-05-09 11:26:50 +00:00
|
|
|
//BindRequest(BindRequest),
|
2021-05-08 09:34:55 +00:00
|
|
|
ListUsersRequest(ListUsersRequest),
|
2021-05-09 11:26:50 +00:00
|
|
|
ListUsersResponse(Result<Vec<User>, Error>),
|
2021-05-08 09:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for App {
|
|
|
|
type Message = Msg;
|
|
|
|
type Properties = ();
|
|
|
|
|
2021-05-09 11:26:50 +00:00
|
|
|
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
|
|
|
App {
|
|
|
|
link: link.clone(),
|
|
|
|
ipservice: HostService::default(),
|
|
|
|
task: None,
|
|
|
|
users: None,
|
|
|
|
}
|
2021-05-08 09:34:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 11:26:50 +00:00
|
|
|
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;
|
2021-05-10 08:21:24 +00:00
|
|
|
ConsoleService::log(format!("Error trying to fetch users: {}", e).as_str())
|
2021-05-09 11:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Msg::ListUsersResponse(Ok(users)) => {
|
|
|
|
self.users = Some(users);
|
|
|
|
ConsoleService::log(format!("Response: {:?}", Json(&self.users)).as_str());
|
|
|
|
}
|
|
|
|
Msg::ListUsersResponse(Err(e)) => {
|
|
|
|
self.task = None;
|
2021-05-10 08:21:24 +00:00
|
|
|
ConsoleService::log(format!("Error listing users: {}", e).as_str())
|
2021-05-09 11:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-08 09:34:55 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
|
|
|
html! {
|
2021-05-09 11:26:50 +00:00
|
|
|
<div>
|
|
|
|
<h1>{ "LLDAP" }</h1>
|
|
|
|
<button onclick=self.link.callback(|_| Msg::ListUsersRequest(ListUsersRequest{filters: None}))>{ "Fetch users" }</button>
|
|
|
|
<p>{ format!("Users: {:?}", &self.users) }</p>
|
|
|
|
</div>
|
2021-05-08 09:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|