diff --git a/application/src/lib.rs b/application/src/lib.rs
index e0c62c8..66a0f2b 100644
--- a/application/src/lib.rs
+++ b/application/src/lib.rs
@@ -66,8 +66,10 @@ pub fn App() -> impl IntoView {
-
+ "Connection to database..."
}>
+
+
+
}
}>
diff --git a/application/src/util/surrealdb.rs b/application/src/util/surrealdb.rs
index 2e49116..bf1d6a0 100644
--- a/application/src/util/surrealdb.rs
+++ b/application/src/util/surrealdb.rs
@@ -1,6 +1,12 @@
use crate::util;
use leptos::*;
-use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn};
+use leptos_use::{
+ core::ConnectionReadyState,
+ storage::use_local_storage,
+ use_websocket,
+ utils::{FromToStringCodec, StringCodec},
+ UseWebsocketReturn,
+};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::rc::Rc;
@@ -76,6 +82,8 @@ pub struct SurrealContext {
pub ready_state: Signal,
pub authenticated: ReadSignal,
pub set_authenticated: WriteSignal,
+ pub loading: ReadSignal,
+ pub set_loading: WriteSignal,
}
#[derive(Serialize, Deserialize, Clone)]
@@ -107,6 +115,8 @@ impl SurrealContext {
ready_state: Signal,
authenticated: ReadSignal,
set_authenticated: WriteSignal,
+ loading: ReadSignal,
+ set_loading: WriteSignal,
) -> Self {
Self {
message,
@@ -114,6 +124,8 @@ impl SurrealContext {
ready_state,
authenticated,
set_authenticated,
+ loading,
+ set_loading,
}
}
@@ -185,6 +197,7 @@ pub fn init_surrealdb() {
// Create reactive signals for the websocket connection to surrealDB
let (participants, set_participants) = create_signal::>(vec![]);
let (authenticated, set_authenticated) = create_signal::(false);
+ let (loading, set_loading) = create_signal::(true);
// Bind the websocket connection to a context
let websocket = SurrealContext::new(
@@ -193,14 +206,39 @@ pub fn init_surrealdb() {
ready_state,
authenticated,
set_authenticated,
+ loading,
+ set_loading,
);
- provide_context(websocket);
+ provide_context(websocket.clone());
provide_context(ParticipantsContext {
read: participants,
write: set_participants,
});
+ create_effect(move |prev_value| {
+ let status = ready_state.get();
+
+ if prev_value != Some(status.clone()) && status == ConnectionReadyState::Open {
+ let (token, _, _) = use_local_storage::("surrealdb-token");
+
+ if token.get().is_empty() {
+ set_loading.set(false);
+ return status;
+ }
+
+ let request = SurrealRequest {
+ id: SurrealId::Integer(0),
+ method: String::from("authenticate"),
+ params: vec![SurrealParams::String(token.get())],
+ };
+
+ websocket.send(&json!(request).to_string());
+ };
+
+ status
+ });
+
// Watch for a message recieved from the surrealDB connection
create_effect(move |prev_value| {
let msg = message.get();
@@ -245,15 +283,23 @@ fn surrealdb_response(response: String) {
}
/// Function to execute when DB signin is succesful
-fn use_surrealdb(_response: SurrealResponse) {
+fn use_surrealdb(response: SurrealResponse) {
util::toast::add_toast(
"Succesfully signed into DB".to_string(),
"success".to_string(),
);
+ let (_, set_token, _) = use_local_storage::("surrealdb-token");
+
+ match response.result {
+ SurrealResult::String(str) => set_token.set(str),
+ _ => (),
+ };
+
let websocket = expect_context::();
websocket.set_authenticated.set(true);
+ websocket.set_loading.set(false);
let request = SurrealRequest {
id: SurrealId::Integer(1),