use crate::util::surrealdb::{client::Time, schemas}; use leptos::*; cfg_if::cfg_if! { if #[cfg(feature = "ssr")] { use crate::util::surrealdb::{DB, schemas::Participant}; use crate::util::websocket::{server, ParticipantsAction}; use leptos::logging; } } #[server(ModifyParticipant)] async fn modify_participant( mut participant: schemas::ParticipantUpdate, id: String, ) -> Result<(), ServerFnError> { let websocket_state = &server::WEBSOCKET_STATE; println!("Participant: {:?}", participant); let updated: Option = DB .update(("participant", &id)) .content(&participant) .await?; match updated { Some(participant_updated) => { logging::log!( "Updated participant: {} ({})", participant_updated.name, participant_updated.group ); let action = ParticipantsAction::Replace { participant: Participant { name: participant_updated.name.clone(), group: participant_updated.group.clone(), 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( "Could not update participant", ))), } } /// Renders the home page of your application. #[component] pub fn Modal() -> impl IntoView { let participant_id = use_context::>>().unwrap(); let participants = use_context::().unwrap(); let time_lifesaver = create_rw_signal(Time { minutes: 0, seconds: 0, milliseconds: 0, }); let time_hindernis = create_rw_signal(Time { minutes: 0, seconds: 0, milliseconds: 0, }); let time_popduiken = create_rw_signal(Time { minutes: 0, seconds: 0, milliseconds: 0, }); let name = create_rw_signal(String::from("")); let group = create_rw_signal(String::from("")); let participant = move || { let x = participants .get() .into_iter() .filter(|item| item.id == participant_id.get().unwrap_or("".to_string())) .collect::>(); match x.get(0) { Some(participant) => { let participant_clone = participant.value.get_untracked(); name.set(participant_clone.name); group.set(participant_clone.group); time_lifesaver.set(Time::from_milliseconds(match participant_clone.events { Some(ref events) => events.lifesaver.unwrap_or(0), None => 0, })); time_hindernis.set(Time::from_milliseconds(match participant_clone.events { Some(ref events) => events.hindernis.unwrap_or(0), None => 0, })); time_popduiken.set(Time::from_milliseconds(match participant_clone.events { Some(ref events) => events.popduiken.unwrap_or(0), None => 0, })); Some(participant.value) } None => None, } }; let modify_participant_action = create_action(|input: &(schemas::ParticipantUpdate, String)| { let input = input.to_owned(); async move { modify_participant(input.0, input.1).await } }); let form_submit = move |ev: ev::SubmitEvent| { ev.prevent_default(); let p = schemas::ParticipantUpdate { name: name.get(), group: group.get(), events: Some(schemas::Events { lifesaver: Some(time_lifesaver.get().as_milliseconds()), hindernis: Some(time_hindernis.get().as_milliseconds()), popduiken: Some(time_popduiken.get().as_milliseconds()), }), }; modify_participant_action.dispatch((p, participant().unwrap().get().id)); participant_id.set(None); }; view! { { move || match participant() { Some(_p) => view! { }.into_view(), None => view! {}.into_view() }} } } #[component] pub fn SelectOption(is: &'static str, value: RwSignal) -> impl IntoView { view! { } }