Add a method to fetch users from the client

This commit is contained in:
Valentin Tolmer 2021-05-09 13:26:50 +02:00
parent 2416eede6f
commit 25027f7614
4 changed files with 86 additions and 10 deletions

View File

@ -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"]

29
app/src/api.rs Normal file
View File

@ -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<Result<Vec<User>, Error>>) -> Result<FetchTask, Error> {
let url = format!("/api/users");
let handler = move |response: Response<Json<Result<Vec<User>, 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())
}
}

View File

@ -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<Self>,
ipservice: HostService,
task: Option<FetchTask>,
users: Option<Vec<User>>,
}
pub enum Msg {
//BindRequest(BindRequest),
ListUsersRequest(ListUsersRequest),
ListUsersResponse(Result<Vec<User>, Error>),
}
impl Component for App {
type Message = Msg;
type Properties = ();
fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
App {}
fn create(_: Self::Properties, link: ComponentLink<Self>) -> 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! {
<p>{ "Hello world!" }</p>
<div>
<h1>{ "LLDAP" }</h1>
<button onclick=self.link.callback(|_| Msg::ListUsersRequest(ListUsersRequest{filters: None}))>{ "Fetch users" }</button>
<p>{ format!("Users: {:?}", &self.users) }</p>
</div>
}
}
}

View File

@ -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> {