diff --git a/app/Cargo.toml b/app/Cargo.toml index c87b773..ea7014a 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -8,6 +8,9 @@ edition = "2018" yew = "0.17.4" wasm-bindgen = "0.2.73" lldap_model = { path = "../model" } +anyhow = "1.0.40" +web-sys = { version = "0.3", features = [ "console" ] } +serde_json = "1" [lib] crate-type = ["cdylib"] diff --git a/app/src/api.rs b/app/src/api.rs new file mode 100644 index 0000000..3ebb113 --- /dev/null +++ b/app/src/api.rs @@ -0,0 +1,29 @@ +use anyhow::{anyhow, Error}; +use lldap_model::*; + +use yew::callback::Callback; +use yew::format::Json; +use yew::services::fetch::{FetchService, FetchTask, Request, Response}; + +#[derive(Default)] +pub struct HostService {} + +impl HostService { + pub fn list_users(&mut self, request: ListUsersRequest, callback: Callback, Error>>) -> Result { + let url = format!("/api/users"); + let handler = move |response: Response, Error>>>| { + let (meta, Json(data)) = response.into_parts(); + if meta.status.is_success() { + callback.emit(data) + } else { + callback.emit(Err(anyhow!( + "{}: error getting users from /api/users", + meta.status + ))) + } + }; + let request = Request::post(url.as_str()).header("Content-Type", "application/json").body(Json(&request)).unwrap(); + FetchService::fetch(request, handler.into()) + } +} + diff --git a/app/src/app.rs b/app/src/app.rs index 89770a2..a4372d6 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -1,23 +1,61 @@ +use crate::api::HostService; +use anyhow::Error; use lldap_model::*; +use yew::format::Json; use yew::prelude::*; +use yew::services::{fetch::FetchTask, ConsoleService}; -pub struct App {} - -pub enum Msg { - BindRequest(BindRequest), - ListUsersRequest(ListUsersRequest), +pub struct App { + link: ComponentLink, + ipservice: HostService, + task: Option, + users: Option>, } +pub enum Msg { + //BindRequest(BindRequest), + ListUsersRequest(ListUsersRequest), + ListUsersResponse(Result, Error>), +} impl Component for App { type Message = Msg; type Properties = (); - fn create(_: Self::Properties, _: ComponentLink) -> Self { - App {} + fn create(_: Self::Properties, link: ComponentLink) -> Self { + 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 { + 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 } @@ -27,7 +65,11 @@ impl Component for App { fn view(&self) -> Html { html! { -

{ "Hello world!" }

+
+

{ "LLDAP" }

+ +

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

+
} } } diff --git a/app/src/lib.rs b/app/src/lib.rs index 0c9c58d..4b33a41 100644 --- a/app/src/lib.rs +++ b/app/src/lib.rs @@ -1,6 +1,8 @@ +#![recursion_limit = "256"] +mod api; mod app; -use wasm_bindgen::prelude::*; +use wasm_bindgen::prelude::{wasm_bindgen, JsValue}; #[wasm_bindgen] pub fn run_app() -> Result<(), JsValue> {