Added surrealDB connection and signin; #1

This commit is contained in:
xeovalyte 2024-02-14 15:22:04 +01:00
parent 3d1a2e6117
commit 8bb1f62352
No known key found for this signature in database
4 changed files with 74 additions and 10 deletions

View File

@ -14,6 +14,8 @@ console_log = "1"
log = "0.4"
console_error_panic_hook = "0.1"
leptos-use = "0.10.2"
serde = "1.0.196"
serde_json = "1.0.113"
# utils
# strum = { version = "0.25", features = ["derive", "strum_macros"] }

View File

@ -1,11 +1,15 @@
use crate::util;
use leptos::*;
use leptos_router::*;
/// Navigation bar
#[component]
pub fn Navbar() -> impl IntoView {
let websocket = expect_context::<util::surrealdb::SurrealContext>();
view! {
<div class="navbar">
<p>{ move || websocket.ready_state.get().to_string()}</p>
<A href="/" active_class="route-active">Home</A>
<A href="/participants" active_class="route-active">Deelnemers</A>
<A href="/times" active_class="route-active">Tijden</A>

View File

@ -21,6 +21,7 @@ pub fn App() -> impl IntoView {
util::surrealdb::init_surrealdb();
let websocket = expect_context::<util::surrealdb::SurrealContext>();
let websocket2 = websocket.clone();
let participants = use_context::<util::surrealdb::ParticipantsContext>()
.expect("Could not find participants context");
@ -59,8 +60,9 @@ pub fn App() -> impl IntoView {
<Router>
<components::navbar::Navbar />
<p>{websocket.message}</p>
<p>{ format!("{:?}", participants.0.get() )}</p>
<button on:click=move |_| { websocket.send("BOE!") }>"Test Message"</button>
<p>{ move || format!("{:?}", participants.0.get() )}</p>
<button on:click=move |_| { websocket.signin() }>"Test Message"</button>
<button on:click=move |_| { websocket2.send("{ \"id\": 1, \"method\": \"create\", \"params\": [ \"person\", { \"name\": \"Mary Doe\" } ] }") }>"Test Message"</button>
<main>
<Routes>
<Route path="/" view=Home />

View File

@ -1,7 +1,30 @@
use leptos::*;
use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn};
use serde::Serialize;
use serde_json::json;
use std::rc::Rc;
#[derive(Serialize)]
struct SurrealRequest {
id: u32,
method: String,
params: Vec<SurrealParams>,
}
#[derive(Serialize)]
#[serde(untagged)]
enum SurrealParams {
Participant,
SigninParam(SigninParam),
String(String),
}
#[derive(Serialize)]
struct SigninParam {
user: String,
pass: String,
}
#[derive(Clone, Debug)]
pub enum HourKind {
A,
@ -30,17 +53,50 @@ pub struct ParticipantsContext(pub ReadSignal<Vec<Participant>>);
pub struct SurrealContext {
pub message: Signal<Option<String>>,
send: Rc<dyn Fn(&str)>,
pub ready_state: Signal<ConnectionReadyState>,
}
impl SurrealContext {
pub fn new(message: Signal<Option<String>>, send: Rc<dyn Fn(&str)>) -> Self {
Self { message, send }
pub fn new(
message: Signal<Option<String>>,
send: Rc<dyn Fn(&str)>,
ready_state: Signal<ConnectionReadyState>,
) -> Self {
Self {
message,
send,
ready_state,
}
}
#[inline(always)]
pub fn send(&self, message: &str) {
(self.send)(message)
}
pub fn signin(&self) {
let request = SurrealRequest {
id: 1,
method: String::from("signin"),
params: vec![SurrealParams::SigninParam(SigninParam {
user: String::from("root"),
pass: String::from("root"),
})],
};
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())
}
}
pub fn init_surrealdb() {
@ -49,7 +105,7 @@ pub fn init_surrealdb() {
message,
send,
..
} = use_websocket("wss://echo.websocket.events/");
} = use_websocket("ws://localhost:80/rpc");
let (participants, _set_participants) = create_signal::<Vec<Participant>>(vec![Participant {
name: "User1".to_string(),
@ -59,10 +115,10 @@ pub fn init_surrealdb() {
},
}]);
provide_context(SurrealContext::new(message, Rc::new(send.clone())));
provide_context(SurrealContext::new(
message,
Rc::new(send.clone()),
ready_state,
));
provide_context(ParticipantsContext(participants));
let _status = move || ready_state.get().to_string();
let _connected = move || ready_state.get() == ConnectionReadyState::Open;
}