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 {
// 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<Vec<util::surrealdb::schemas::Participant>> =
create_rw_signal(vec![]);
provide_context(participants);
util::surrealdb::init_participants();
view! {
// injects a stylesheet into the document <head>
@ -35,16 +36,11 @@ pub fn App() -> impl IntoView {
}>
<components::header::Header />
<main>
{move || match once.get() {
None => view! { <p>"Loading"</p> }.into_view(),
Some(_) => view! {
<Routes>
<Route path="" view=pages::index::HomePage/>
<Route path="/add-participant" view=pages::add_participant::AddParticipant />
<Route path="/add-time" view=pages::add_time::AddTime />
</Routes>
}.into_view()
}}
<Routes>
<Route path="/" view=pages::index::HomePage />
<Route path="/add-participant" view=pages::add_participant::AddParticipant />
<Route path="/add-time" view=pages::add_time::AddTime />
</Routes>
</main>
</Router>
}

View File

@ -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::<RwSignal<Vec<schemas::Participant>>>();
let form_submit = create_server_action::<AddTime>();
view! {

View File

@ -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::<RwSignal<Vec<schemas::Participant>>>().unwrap();
view! {
<div class="actions-container">
<A href="/add-participant">Deelnemer toevoegen</A>
<A href="/add-time">Tijd toevoegen</A>
// <p>{ format!("{:?}", participants.get()) }</p>
</div>
}
<div class="actions-container">
<A href="/add-participant">Deelnemer toevoegen</A>
<A href="/add-time">Tijd toevoegen</A>
</div>
}
}

View File

@ -46,11 +46,14 @@ pub async fn get_participants() -> Result<Vec<schemas::Participant>, ServerFnErr
Ok(participants)
}
pub async fn init_participants() -> Result<bool, ServerFnError> {
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<schemas::Participant>| {
let participants_context =
use_context::<RwSignal<Vec<schemas::Participant>>>().unwrap();
participants_context.set(data.to_vec());
});
});
}