Added client participants signal

This commit is contained in:
xeovalyte 2024-04-05 14:34:13 +02:00
parent a3ea2ec991
commit b9d5425ac5
No known key found for this signature in database
4 changed files with 28 additions and 29 deletions

View File

@ -11,10 +11,11 @@ use crate::util;
pub fn App() -> impl IntoView { pub fn App() -> impl IntoView {
// Provides context that manages stylesheets, titles, meta tags, etc. // Provides context that manages stylesheets, titles, meta tags, etc.
provide_meta_context(); provide_meta_context();
let once = create_resource( let participants: RwSignal<Vec<util::surrealdb::schemas::Participant>> =
|| (), create_rw_signal(vec![]);
|_| async move { util::surrealdb::init_participants().await },
); provide_context(participants);
util::surrealdb::init_participants();
view! { view! {
// injects a stylesheet into the document <head> // injects a stylesheet into the document <head>
@ -35,16 +36,11 @@ pub fn App() -> impl IntoView {
}> }>
<components::header::Header /> <components::header::Header />
<main> <main>
{move || match once.get() {
None => view! { <p>"Loading"</p> }.into_view(),
Some(_) => view! {
<Routes> <Routes>
<Route path="" view=pages::index::HomePage/> <Route path="/" view=pages::index::HomePage />
<Route path="/add-participant" view=pages::add_participant::AddParticipant /> <Route path="/add-participant" view=pages::add_participant::AddParticipant />
<Route path="/add-time" view=pages::add_time::AddTime /> <Route path="/add-time" view=pages::add_time::AddTime />
</Routes> </Routes>
}.into_view()
}}
</main> </main>
</Router> </Router>
} }

View File

@ -1,9 +1,10 @@
use crate::util::surrealdb::schemas;
use leptos::*; use leptos::*;
use leptos_router::ActionForm; use leptos_router::ActionForm;
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(feature = "ssr")] { if #[cfg(feature = "ssr")] {
use crate::util::surrealdb::{DB, schemas}; use crate::util::surrealdb::{DB};
use leptos::logging; use leptos::logging;
} }
} }
@ -33,6 +34,8 @@ async fn add_time(name: String, group: String) -> Result<(), ServerFnError> {
/// Renders the home page of your application. /// Renders the home page of your application.
#[component] #[component]
pub fn AddTime() -> impl IntoView { pub fn AddTime() -> impl IntoView {
let participants = use_context::<RwSignal<Vec<schemas::Participant>>>();
let form_submit = create_server_action::<AddTime>(); let form_submit = create_server_action::<AddTime>();
view! { view! {

View File

@ -6,13 +6,10 @@ use crate::util::surrealdb::schemas;
/// Renders the home page of your application. /// Renders the home page of your application.
#[component] #[component]
pub fn HomePage() -> impl IntoView { pub fn HomePage() -> impl IntoView {
// let participants = use_context::<RwSignal<Vec<schemas::Participant>>>().unwrap();
view! { view! {
<div class="actions-container"> <div class="actions-container">
<A href="/add-participant">Deelnemer toevoegen</A> <A href="/add-participant">Deelnemer toevoegen</A>
<A href="/add-time">Tijd toevoegen</A> <A href="/add-time">Tijd toevoegen</A>
// <p>{ format!("{:?}", participants.get()) }</p>
</div> </div>
} }
} }

View File

@ -46,11 +46,14 @@ pub async fn get_participants() -> Result<Vec<schemas::Participant>, ServerFnErr
Ok(participants) Ok(participants)
} }
pub async fn init_participants() -> Result<bool, ServerFnError> { pub fn init_participants() {
let participants = get_participants().await?; let participants = create_local_resource(|| (), |_| async move { get_participants().await });
let participants_signal = create_rw_signal(participants); create_effect(move |_| {
provide_context(participants_signal); participants.and_then(|data: &Vec<schemas::Participant>| {
let participants_context =
Ok(true) use_context::<RwSignal<Vec<schemas::Participant>>>().unwrap();
participants_context.set(data.to_vec());
});
});
} }