Compare commits

..

No commits in common. "d9ed4ba6265a00ccb0faf63bd2c8d74353125ec2" and "14bad374b050df6e34717eefd42cf05d71188e63" have entirely different histories.

2 changed files with 5 additions and 53 deletions

View File

@ -66,11 +66,9 @@ pub fn App() -> impl IntoView {
<Route path="/" view=move || { <Route path="/" view=move || {
view! { view! {
// only show the outlet if the signin was successfull // only show the outlet if the signin was successfull
<Show when=move || !websocket.loading.get() fallback=|| view! { <p>"Connection to database..."</p>}>
<Show when=move || websocket.authenticated.get() fallback=login::Login> <Show when=move || websocket.authenticated.get() fallback=login::Login>
<Outlet/> <Outlet/>
</Show> </Show>
</Show>
} }
}> }>
<Route path="/" view=Home /> <Route path="/" view=Home />

View File

@ -1,12 +1,6 @@
use crate::util; use crate::util;
use leptos::*; use leptos::*;
use leptos_use::{ use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn};
core::ConnectionReadyState,
storage::use_local_storage,
use_websocket,
utils::{FromToStringCodec, StringCodec},
UseWebsocketReturn,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
use std::rc::Rc; use std::rc::Rc;
@ -82,8 +76,6 @@ pub struct SurrealContext {
pub ready_state: Signal<ConnectionReadyState>, pub ready_state: Signal<ConnectionReadyState>,
pub authenticated: ReadSignal<bool>, pub authenticated: ReadSignal<bool>,
pub set_authenticated: WriteSignal<bool>, pub set_authenticated: WriteSignal<bool>,
pub loading: ReadSignal<bool>,
pub set_loading: WriteSignal<bool>,
} }
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
@ -115,8 +107,6 @@ impl SurrealContext {
ready_state: Signal<ConnectionReadyState>, ready_state: Signal<ConnectionReadyState>,
authenticated: ReadSignal<bool>, authenticated: ReadSignal<bool>,
set_authenticated: WriteSignal<bool>, set_authenticated: WriteSignal<bool>,
loading: ReadSignal<bool>,
set_loading: WriteSignal<bool>,
) -> Self { ) -> Self {
Self { Self {
message, message,
@ -124,8 +114,6 @@ impl SurrealContext {
ready_state, ready_state,
authenticated, authenticated,
set_authenticated, set_authenticated,
loading,
set_loading,
} }
} }
@ -197,7 +185,6 @@ pub fn init_surrealdb() {
// Create reactive signals for the websocket connection to surrealDB // Create reactive signals for the websocket connection to surrealDB
let (participants, set_participants) = create_signal::<Vec<Participant>>(vec![]); let (participants, set_participants) = create_signal::<Vec<Participant>>(vec![]);
let (authenticated, set_authenticated) = create_signal::<bool>(false); let (authenticated, set_authenticated) = create_signal::<bool>(false);
let (loading, set_loading) = create_signal::<bool>(true);
// Bind the websocket connection to a context // Bind the websocket connection to a context
let websocket = SurrealContext::new( let websocket = SurrealContext::new(
@ -206,39 +193,14 @@ pub fn init_surrealdb() {
ready_state, ready_state,
authenticated, authenticated,
set_authenticated, set_authenticated,
loading,
set_loading,
); );
provide_context(websocket.clone()); provide_context(websocket);
provide_context(ParticipantsContext { provide_context(ParticipantsContext {
read: participants, read: participants,
write: set_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::<String, FromToStringCodec>("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 // Watch for a message recieved from the surrealDB connection
create_effect(move |prev_value| { create_effect(move |prev_value| {
let msg = message.get(); let msg = message.get();
@ -283,23 +245,15 @@ fn surrealdb_response(response: String) {
} }
/// Function to execute when DB signin is succesful /// Function to execute when DB signin is succesful
fn use_surrealdb(response: SurrealResponse) { fn use_surrealdb(_response: SurrealResponse) {
util::toast::add_toast( util::toast::add_toast(
"Succesfully signed into DB".to_string(), "Succesfully signed into DB".to_string(),
"success".to_string(), "success".to_string(),
); );
let (_, set_token, _) = use_local_storage::<String, FromToStringCodec>("surrealdb-token");
match response.result {
SurrealResult::String(str) => set_token.set(str),
_ => (),
};
let websocket = expect_context::<SurrealContext>(); let websocket = expect_context::<SurrealContext>();
websocket.set_authenticated.set(true); websocket.set_authenticated.set(true);
websocket.set_loading.set(false);
let request = SurrealRequest { let request = SurrealRequest {
id: SurrealId::Integer(1), id: SurrealId::Integer(1),