Started working on participants signal

This commit is contained in:
xeovalyte 2024-04-05 09:31:25 +02:00
parent fa455d5df0
commit a3ea2ec991
No known key found for this signature in database
3 changed files with 54 additions and 11 deletions

View File

@ -5,11 +5,16 @@ use leptos_router::*;
use crate::components;
use crate::pages;
use crate::util;
#[component]
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 },
);
view! {
// injects a stylesheet into the document <head>
@ -30,11 +35,16 @@ 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()
}}
</main>
</Router>
}

View File

@ -1,13 +1,18 @@
use leptos::*;
use leptos_router::*;
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>
}
}

View File

@ -1,3 +1,5 @@
use leptos::*;
pub mod schemas;
cfg_if::cfg_if! {
@ -6,7 +8,6 @@ cfg_if::cfg_if! {
use surrealdb::engine::remote::ws::{Client, Ws};
use surrealdb::opt::auth::Root;
use surrealdb::Surreal;
use leptos::{ServerFnError};
pub static DB: Lazy<Surreal<Client>> = Lazy::new(Surreal::init);
}
@ -26,3 +27,30 @@ pub async fn connect() -> Result<(), ServerFnError> {
Ok(())
}
#[server]
pub async fn get_participants() -> Result<Vec<schemas::Participant>, ServerFnError> {
let participant_records: Vec<schemas::ParticipantRecord> = DB.select("participant").await?;
let mut participants: Vec<schemas::Participant> = vec![];
participant_records.iter().for_each(|participant| {
participants.push(schemas::Participant {
id: participant.id.id.to_string(),
name: participant.name.clone(),
group: participant.group.clone(),
events: participant.events.clone(),
})
});
Ok(participants)
}
pub async fn init_participants() -> Result<bool, ServerFnError> {
let participants = get_participants().await?;
let participants_signal = create_rw_signal(participants);
provide_context(participants_signal);
Ok(true)
}