From fe5a35208e9d4732f3951da856d4c4fa3d7cadb8 Mon Sep 17 00:00:00 2001 From: xeovalyte Date: Mon, 19 Feb 2024 10:30:12 +0100 Subject: [PATCH] Added login function to UI; #1 --- application/public/styles.scss | 27 +++++++ application/src/lib.rs | 27 ++++--- application/src/pages/login.rs | 32 ++++++++ application/src/pages/mod.rs | 1 + application/src/util/surrealdb.rs | 125 ++++++++++++++++++++---------- 5 files changed, 161 insertions(+), 51 deletions(-) create mode 100644 application/src/pages/login.rs diff --git a/application/public/styles.scss b/application/public/styles.scss index 50af49b..4e7c9a2 100644 --- a/application/public/styles.scss +++ b/application/public/styles.scss @@ -75,3 +75,30 @@ a { background-color: $secondary-bg-color-light; } +input { + background-color: $secondary-bg-color-light; + border: none; + border-radius: 3px; + padding: 5px 10px; + font-size: 0.9em; + color: $text-color; + margin-bottom: 20px; +} + +input[type=password] { + margin-top: 60px; +} + +input[type=submit]:hover { + background-color: $secondary-bg-color-lighter; + cursor: pointer; +} + +form { + display: flex; + flex-direction: column; + text-align: center; + margin: 40px auto 0 auto; + width: 400px; +} + diff --git a/application/src/lib.rs b/application/src/lib.rs index 6d15b57..8e501a2 100644 --- a/application/src/lib.rs +++ b/application/src/lib.rs @@ -9,6 +9,7 @@ mod util; // Top-Level pages use crate::pages::home::Home; +use crate::pages::login; use crate::pages::not_found::NotFound; use crate::pages::participants; use crate::pages::times; @@ -21,8 +22,7 @@ pub fn App() -> impl IntoView { util::surrealdb::init_surrealdb(); let websocket = expect_context::(); - let websocket2 = websocket.clone(); - let participants = use_context::() + let _participants = use_context::() .expect("Could not find participants context"); view! { @@ -59,17 +59,22 @@ pub fn App() -> impl IntoView { > -

{websocket.message}

-

{ move || format!("{:?}", participants.0.get() )}

- -
- - - - - + + + + } + }> + + + + + +
diff --git a/application/src/pages/login.rs b/application/src/pages/login.rs new file mode 100644 index 0000000..af04096 --- /dev/null +++ b/application/src/pages/login.rs @@ -0,0 +1,32 @@ +use crate::util; +use leptos::*; + +/// Login page +#[component] +pub fn Login() -> impl IntoView { + let websocket = expect_context::(); + + let input_element: NodeRef = create_node_ref(); + + let on_submit = move |ev: leptos::ev::SubmitEvent| { + ev.prevent_default(); + + let value = input_element + .get() + .expect(" should be mounted") + .value(); + + websocket.signin(value) + }; + + view! { + + } +} diff --git a/application/src/pages/mod.rs b/application/src/pages/mod.rs index f5ed568..7dd382e 100644 --- a/application/src/pages/mod.rs +++ b/application/src/pages/mod.rs @@ -1,4 +1,5 @@ pub mod home; +pub mod login; pub mod not_found; pub mod participants; pub mod times; diff --git a/application/src/util/surrealdb.rs b/application/src/util/surrealdb.rs index 9e7a663..c778440 100644 --- a/application/src/util/surrealdb.rs +++ b/application/src/util/surrealdb.rs @@ -1,6 +1,6 @@ use leptos::*; use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn}; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use serde_json::json; use std::rc::Rc; @@ -25,25 +25,10 @@ struct SigninParam { pass: String, } -#[derive(Clone, Debug)] -pub enum HourKind { - A, - B, - C, - D, - Z, -} - -#[derive(Clone, Debug)] -pub struct GroupKind { - hour: HourKind, - lane: u8, -} - #[derive(Clone, Debug)] pub struct Participant { name: String, - group: GroupKind, + group: String, } #[derive(Clone, Debug)] @@ -54,6 +39,21 @@ pub struct SurrealContext { pub message: Signal>, send: Rc, pub ready_state: Signal, + pub authenticated: ReadSignal, + pub set_authenticated: WriteSignal, +} + +#[derive(Serialize, Deserialize)] +struct SurrealResponse { + id: u32, + result: SurrealResult, +} + +#[derive(Serialize, Deserialize)] +#[serde(untagged)] +enum SurrealResult { + String(String), + Null, } impl SurrealContext { @@ -61,11 +61,15 @@ impl SurrealContext { message: Signal>, send: Rc, ready_state: Signal, + authenticated: ReadSignal, + set_authenticated: WriteSignal, ) -> Self { Self { message, send, ready_state, + authenticated, + set_authenticated, } } @@ -74,28 +78,19 @@ impl SurrealContext { (self.send)(message) } - pub fn signin(&self) { + pub fn signin(&self, pass: String) { + log::debug!("Signing into surrealdb"); + let request = SurrealRequest { - id: 1, + id: 0, method: String::from("signin"), params: vec![SurrealParams::SigninParam(SigninParam { user: String::from("root"), - pass: String::from("root"), + pass, })], }; self.send(&json!(request).to_string()); - - let request = SurrealRequest { - id: 1, - method: String::from("use"), - params: vec![ - SurrealParams::String(String::from("wrb")), - SurrealParams::String(String::from("timings")), - ], - }; - - self.send(&json!(request).to_string()) } } @@ -107,18 +102,68 @@ pub fn init_surrealdb() { .. } = use_websocket("ws://localhost:80/rpc"); - let (participants, _set_participants) = create_signal::>(vec![Participant { - name: "User1".to_string(), - group: GroupKind { - hour: HourKind::A, - lane: 1, - }, - }]); + let (participants, _set_participants) = create_signal::>(vec![]); + let (authenticated, set_authenticated) = create_signal::(false); - provide_context(SurrealContext::new( + let websocket = SurrealContext::new( message, Rc::new(send.clone()), ready_state, - )); + authenticated, + set_authenticated, + ); + + provide_context(websocket); provide_context(ParticipantsContext(participants)); + + create_effect(move |prev_value| { + let msg = message.get(); + + if prev_value != Some(msg.clone()) { + match msg { + Some(ref text) => { + log::debug!("{:?}", text); + surrealdb_response(text.to_string()); + } + None => (), + } + } + + msg + }); +} + +/// Function to execute when SurrealDB returns a message +fn surrealdb_response(response: String) { + let response: SurrealResponse = match serde_json::from_str(&response) { + Ok(res) => res, + Err(err) => { + log::warn!("{}", err); + return; + } + }; + + match response.id { + 0 => use_surrealdb(response), + 1 => log::debug!("Succesfully execute use wrb timings"), + _ => return, + } +} + +/// Function to execute when DB signin is succesful +fn use_surrealdb(_response: SurrealResponse) { + let websocket = expect_context::(); + + websocket.set_authenticated.set(true); + + let request = SurrealRequest { + id: 1, + method: String::from("use"), + params: vec![ + SurrealParams::String(String::from("wrb")), + SurrealParams::String(String::from("timings")), + ], + }; + + websocket.send(&json!(request).to_string()) }