From 5e4fd97acaf7bb052c8a6efced1cdb0ed957e657 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 18 Feb 2025 23:18:23 -0500 Subject: [PATCH] Set up some callbacks to handle the login page state --- visions/ui/Cargo.toml | 3 +++ visions/ui/src/client.rs | 33 +++++++++++++++++++++---- visions/ui/src/main.rs | 52 +++++++++++++++++++++++++++++++++------- 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/visions/ui/Cargo.toml b/visions/ui/Cargo.toml index 35c0c2c..e47279b 100644 --- a/visions/ui/Cargo.toml +++ b/visions/ui/Cargo.toml @@ -6,6 +6,9 @@ edition = "2021" [dependencies] gloo-net = "0.6.0" serde = { version = "1.0.217", features = ["derive"] } +serde-wasm-bindgen = "0.6.5" +serde_json = "1.0.138" wasm-bindgen-futures = "0.4.50" +web-sys = "0.3.77" yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] } diff --git a/visions/ui/src/client.rs b/visions/ui/src/client.rs index cd9d015..982140c 100644 --- a/visions/ui/src/client.rs +++ b/visions/ui/src/client.rs @@ -1,5 +1,12 @@ -use gloo_net::http::Request; -use serde::Deserialize; +use gloo_net::http::{Request, Response}; +use serde::{Deserialize, Serialize}; +use wasm_bindgen_futures::wasm_bindgen::{self, JsValue}; + +#[derive(Serialize)] +struct AuthRequest { + username: String, + password: String, +} #[derive(Deserialize)] enum AuthResponse { @@ -12,6 +19,7 @@ struct SessionId(String); enum ClientError { Unauthorized, + Err(u16), } #[derive(Deserialize)] @@ -28,12 +36,29 @@ trait Client { async fn list_users(session_id: SessionId) -> Result, ClientError>; } +pub struct Connection; + +impl Connection { + pub fn new() -> Self { Self } +} + impl Client for Connection { async fn auth(username: String, password: String) -> Result { - let request = Request::post("http://localhost:8001") - .body().unwrap(); + let response: Response = Request::post("http://localhost:8001") + .body(serde_wasm_bindgen::to_value(&AuthRequest{ username, password }).unwrap()) + .unwrap() + .send() + .await + .unwrap(); + + if response.ok() { + Ok(serde_json::from_slice(&response.binary().await.unwrap()).unwrap()) + } else { + Err(ClientError::Err(response.status())) + } } async fn list_users(session_id: SessionId) -> Result, ClientError> { + todo!() } } diff --git a/visions/ui/src/main.rs b/visions/ui/src/main.rs index e305174..26a584b 100644 --- a/visions/ui/src/main.rs +++ b/visions/ui/src/main.rs @@ -1,5 +1,6 @@ use std::rc::Rc; +use web_sys::HtmlInputElement; use yew::prelude::*; mod client; @@ -36,23 +37,57 @@ impl Reducible for AuthInfo { #[derive(Properties, PartialEq)] struct LoginProps { - on_click: Callback<()>, + on_login: Callback<(String, String)>, } #[function_component] -fn Login(LoginProps { on_click }: &LoginProps) -> Html { +fn Login(LoginProps { on_login }: &LoginProps) -> Html { + let username_node_ref = use_node_ref(); + let password_node_ref = use_node_ref(); + + let username = use_state(|| "".to_owned()); + let password = use_state(|| "".to_owned()); + let on_click = { - let on_click = on_click.clone(); - Callback::from(move |_| on_click.emit(())) + let on_login = on_login.clone(); + let username = username.clone(); + let password = password.clone(); + Callback::from(move |_| on_login.emit((username.to_string(), password.to_string()))) }; + + let on_username_changed = { + let username = username.clone(); + let username_node_ref = username_node_ref.clone(); + Callback::from(move |_| { + let input = username_node_ref.cast::().unwrap(); + println!("username changed: {}", input.value()); + username.set(input.value()); + }) + }; + + let on_password_changed = { + let password = password.clone(); + let password_node_ref = password_node_ref.clone(); + Callback::from(move |_| { + let input = password_node_ref.cast::().unwrap(); + println!("password changed: {}", input.value()); + password.set(input.value()); + }) + }; + html! { } } @@ -60,16 +95,17 @@ fn Login(LoginProps { on_click }: &LoginProps) -> Html { #[function_component] fn App() -> Html { let auth_info = use_reducer(AuthInfo::default); + let client = Connection::new(); let on_login = { let auth_info = auth_info.clone(); - Callback::from(move |_| auth_info.dispatch(AuthAction::Auth("abcdefg".into()))) + Callback::from(move |(username, password)| auth_info.dispatch(AuthAction::Auth(username))) }; if auth_info.session_id.is_some() { html! {

{ "this is just a thing" }

} } else { - html! { } + html! { } } }