From b9d5425ac5f98a1c48f19904e8d356044de03d0e Mon Sep 17 00:00:00 2001 From: xeovalyte Date: Fri, 5 Apr 2024 14:34:13 +0200 Subject: [PATCH] Added client participants signal --- application/src/app.rs | 24 ++++++++++-------------- application/src/pages/add_time.rs | 5 ++++- application/src/pages/index.rs | 13 +++++-------- application/src/util/surrealdb.rs | 15 +++++++++------ 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/application/src/app.rs b/application/src/app.rs index b3b4429..e6a2841 100644 --- a/application/src/app.rs +++ b/application/src/app.rs @@ -11,10 +11,11 @@ use crate::util; pub fn App() -> impl IntoView { // Provides context that manages stylesheets, titles, meta tags, etc. provide_meta_context(); - let once = create_resource( - || (), - |_| async move { util::surrealdb::init_participants().await }, - ); + let participants: RwSignal> = + create_rw_signal(vec![]); + + provide_context(participants); + util::surrealdb::init_participants(); view! { // injects a stylesheet into the document @@ -35,16 +36,11 @@ pub fn App() -> impl IntoView { }>
- {move || match once.get() { - None => view! {

"Loading"

}.into_view(), - Some(_) => view! { - - - - - - }.into_view() - }} + + + + +
} diff --git a/application/src/pages/add_time.rs b/application/src/pages/add_time.rs index b7418ab..0411a2b 100644 --- a/application/src/pages/add_time.rs +++ b/application/src/pages/add_time.rs @@ -1,9 +1,10 @@ +use crate::util::surrealdb::schemas; use leptos::*; use leptos_router::ActionForm; cfg_if::cfg_if! { if #[cfg(feature = "ssr")] { - use crate::util::surrealdb::{DB, schemas}; + use crate::util::surrealdb::{DB}; use leptos::logging; } } @@ -33,6 +34,8 @@ async fn add_time(name: String, group: String) -> Result<(), ServerFnError> { /// Renders the home page of your application. #[component] pub fn AddTime() -> impl IntoView { + let participants = use_context::>>(); + let form_submit = create_server_action::(); view! { diff --git a/application/src/pages/index.rs b/application/src/pages/index.rs index fcd525d..029f757 100644 --- a/application/src/pages/index.rs +++ b/application/src/pages/index.rs @@ -6,13 +6,10 @@ use crate::util::surrealdb::schemas; /// Renders the home page of your application. #[component] pub fn HomePage() -> impl IntoView { - // let participants = use_context::>>().unwrap(); - view! { -
- Deelnemer toevoegen - Tijd toevoegen - //

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

-
- } + + } } diff --git a/application/src/util/surrealdb.rs b/application/src/util/surrealdb.rs index 382b679..8c9edba 100644 --- a/application/src/util/surrealdb.rs +++ b/application/src/util/surrealdb.rs @@ -46,11 +46,14 @@ pub async fn get_participants() -> Result, ServerFnErr Ok(participants) } -pub async fn init_participants() -> Result { - let participants = get_participants().await?; +pub fn init_participants() { + let participants = create_local_resource(|| (), |_| async move { get_participants().await }); - let participants_signal = create_rw_signal(participants); - provide_context(participants_signal); - - Ok(true) + create_effect(move |_| { + participants.and_then(|data: &Vec| { + let participants_context = + use_context::>>().unwrap(); + participants_context.set(data.to_vec()); + }); + }); }