Added reactive participants

This commit is contained in:
xeovalyte 2024-04-09 17:40:46 +02:00
parent 94cf11e1c2
commit 36c23f3740
No known key found for this signature in database
10 changed files with 120 additions and 36 deletions

View File

@ -11,8 +11,7 @@ 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 participants: RwSignal<Vec<util::surrealdb::schemas::Participant>> = let participants: util::surrealdb::schemas::ParticipantsContext = create_rw_signal(vec![]);
create_rw_signal(vec![]);
provide_context(participants); provide_context(participants);
util::surrealdb::init_participants(); util::surrealdb::init_participants();

View File

@ -1 +1,2 @@
pub mod header; pub mod header;
pub mod participants;

View File

@ -0,0 +1,45 @@
use crate::util::surrealdb::schemas;
use leptos::*;
/// Renders the home page of your application.
#[component]
pub fn Participants() -> impl IntoView {
let participants_context = use_context::<schemas::ParticipantsContext>().unwrap();
view! {
<div class="participants-container">
<table>
<tr>
<th>"Naam"</th>
<th>"Groep"</th>
<th>"Lifesaver"</th>
<th>"Hindernis"</th>
<th>"Popduiken"</th>
</tr>
<For
each=move || participants_context.get()
key=|state| state.id.clone()
let:child
>
<tr>
<td>{ move || child.value.get().name }</td>
<td>{ move || child.value.get().group }</td>
{ move || match child.value.get().events {
Some(events) => view! {
<td>{ events.lifesaver.unwrap_or(0) }</td>
<td>{ events.hindernis.unwrap_or(0) }</td>
<td>{ events.popduiken.unwrap_or(0) }</td>
},
None => view! {
<td>"0"</td>
<td>"0"</td>
<td>"0"</td>
}
}
}
</tr>
</For>
</table>
</div>
}
}

View File

@ -1,8 +1,6 @@
use leptos::*; use leptos::*;
use leptos_router::ActionForm; use leptos_router::ActionForm;
use crate::util::surrealdb::schemas::Participant;
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, schemas};
@ -29,11 +27,11 @@ async fn add_participant(name: String, group: String) -> Result<(), ServerFnErro
); );
let action = ParticipantsAction::Add { let action = ParticipantsAction::Add {
participant: Participant { participant: schemas::Participant {
name: participant.name.clone(), name: participant.name.clone(),
group: participant.group.clone(), group: participant.group.clone(),
id: participant.id.to_string(), id: participant.id.to_string(),
events: None, events: participant.events.clone(),
}, },
}; };

View File

@ -6,7 +6,8 @@ use web_sys::ScrollIntoViewOptions;
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(feature = "ssr")] { if #[cfg(feature = "ssr")] {
use crate::util::surrealdb::{DB}; use crate::util::surrealdb::{DB, schemas::Participant};
use crate::util::websocket::{server, ParticipantsAction};
use surrealdb::opt::PatchOp; use surrealdb::opt::PatchOp;
use leptos::logging; use leptos::logging;
} }
@ -31,19 +32,33 @@ async fn add_time(
event: String, event: String,
time: u32, time: u32,
) -> Result<(), ServerFnError> { ) -> Result<(), ServerFnError> {
let websocket_state = &server::WEBSOCKET_STATE;
let updated: Option<schemas::ParticipantRecord> = DB let updated: Option<schemas::ParticipantRecord> = DB
.update(("participant", participant.id)) .update(("participant", &participant.id))
.patch(PatchOp::replace(&("/events/".to_owned() + &event), time)) .patch(PatchOp::replace(&("/events/".to_owned() + &event), time))
.await?; .await?;
match updated { match updated {
Some(participant) => { Some(participant_updated) => {
logging::log!( logging::log!(
"Updated participant: {} ({})", "Updated participant: {} ({})",
participant.name, participant_updated.name,
participant.group participant_updated.group
); );
Ok(()) let action = ParticipantsAction::Replace {
participant: Participant {
name: participant_updated.name.clone(),
group: participant_updated.group.clone(),
id: participant.id,
events: participant_updated.events.clone(),
},
};
match websocket_state.apply(action) {
Ok(_) => Ok(()),
Err(_) => Err(ServerFnError::new("Error sending websocket action")),
}
} }
None => Err(ServerFnError::ServerError(String::from( None => Err(ServerFnError::ServerError(String::from(
"Could not update participant", "Could not update participant",
@ -54,7 +69,7 @@ async fn add_time(
/// 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>>>().unwrap(); let participants = use_context::<schemas::ParticipantsContext>().unwrap();
let container_ref: NodeRef<html::Ul> = create_node_ref(); let container_ref: NodeRef<html::Ul> = create_node_ref();
let name_input_ref: NodeRef<html::Input> = create_node_ref(); let name_input_ref: NodeRef<html::Input> = create_node_ref();
@ -82,7 +97,7 @@ pub fn AddTime() -> impl IntoView {
let participant = &participants_sorted.get()[selected_index.get()]; let participant = &participants_sorted.get()[selected_index.get()];
add_time_action.dispatch(( add_time_action.dispatch((
participant.clone(), participant.clone(),
String::from("lifesaver"), event.get(),
time.get().as_milliseconds(), time.get().as_milliseconds(),
)); ));
@ -213,16 +228,16 @@ pub fn SelectOption(is: &'static str, value: ReadSignal<String>) -> impl IntoVie
} }
} }
fn sort_participants( fn sort_participants(
participants: Vec<schemas::Participant>, participants: Vec<schemas::ParticipantSignal>,
search: String, search: String,
) -> Vec<schemas::Participant> { ) -> Vec<schemas::Participant> {
let mut filtered_sorted_list: Vec<(schemas::Participant, f64)> = participants let mut filtered_sorted_list: Vec<(schemas::Participant, f64)> = participants
.into_iter() .into_iter()
.map(|participant| { .map(|participant| {
( (
participant.clone(), participant.value.get(),
normalized_damerau_levenshtein( normalized_damerau_levenshtein(
&participant.name.to_lowercase(), &participant.value.get().name.to_lowercase(),
&search.to_lowercase(), &search.to_lowercase(),
), ),
) )

View File

@ -1,3 +1,4 @@
use crate::components;
use leptos::*; use leptos::*;
use leptos_router::*; use leptos_router::*;
@ -9,5 +10,6 @@ pub fn HomePage() -> impl IntoView {
<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>
</div> </div>
<components::participants::Participants />
} }
} }

View File

@ -51,9 +51,18 @@ pub fn init_participants() {
create_effect(move |_| { create_effect(move |_| {
participants.and_then(|data: &Vec<schemas::Participant>| { participants.and_then(|data: &Vec<schemas::Participant>| {
let participants_context = let participants_context = use_context::<schemas::ParticipantsContext>().unwrap();
use_context::<RwSignal<Vec<schemas::Participant>>>().unwrap();
participants_context.set(data.to_vec()); let mut participants_new: Vec<schemas::ParticipantSignal> = vec![];
for participant in data {
participants_new.push(schemas::ParticipantSignal {
id: participant.id.clone(),
value: create_rw_signal(participant.clone()),
})
}
participants_context.set(participants_new);
}); });
}); });
} }

View File

@ -4,13 +4,14 @@ cfg_if::cfg_if! {
} }
} }
use leptos::RwSignal;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Hash, Eq)]
pub struct Events { pub struct Events {
lifesaver: Option<u32>, pub lifesaver: Option<u32>,
hindernis: Option<u32>, pub hindernis: Option<u32>,
popduiken: Option<u32>, pub popduiken: Option<u32>,
} }
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
@ -30,6 +31,14 @@ pub struct Participant {
pub events: Option<Events>, pub events: Option<Events>,
} }
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParticipantSignal {
pub id: String,
pub value: RwSignal<Participant>,
}
pub type ParticipantsContext = RwSignal<Vec<ParticipantSignal>>;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct NewParticipant { pub struct NewParticipant {
pub name: String, pub name: String,

View File

@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
use crate::util::surrealdb::schemas::Participant; use crate::util::surrealdb::schemas::Participant;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Debug)]
pub enum ParticipantsAction { pub enum ParticipantsAction {
Add { participant: Participant }, Add { participant: Participant },
Replace { participant: Participant }, Replace { participant: Participant },

View File

@ -35,23 +35,29 @@ fn handle_message(message: &str) {
} }
}; };
let participants_context = use_context::<RwSignal<Vec<schemas::Participant>>>().unwrap(); logging::log!("Recieved action: {:?}", action);
let participants_context = use_context::<schemas::ParticipantsContext>().unwrap();
match action { match action {
ParticipantsAction::Add { participant } => { ParticipantsAction::Add { participant } => participants_context.update(|participants| {
participants_context.update(|participants| participants.push(participant)) participants.push(schemas::ParticipantSignal {
} id: participant.id.clone(),
value: create_rw_signal(participant),
})
}),
ParticipantsAction::Remove { id } => participants_context ParticipantsAction::Remove { id } => participants_context
.update(|participants| participants.retain(|participant| participant.id != id)), .update(|participants| participants.retain(|participant| participant.id != id)),
ParticipantsAction::Replace { participant } => { ParticipantsAction::Replace { participant } => {
participants_context.update(|participants| { participants_context.with(|participants| {
if let Some(index) = participants for participant_signal in participants {
.iter() if participant_signal.id == participant.id {
.position(|item| item.id == participant.id) participant_signal.value.update(|value| {
{ *value = participant.clone();
let _ = std::mem::replace(&mut participants[index], participant);
}
}) })
} }
} }
});
}
}
} }