Finished edit participant feature

This commit is contained in:
xeovalyte 2024-04-22 15:54:51 +02:00
parent 8fac34aafa
commit be342101dd
No known key found for this signature in database
4 changed files with 194 additions and 47 deletions

View File

@ -1,25 +1,61 @@
use crate::util::surrealdb::{client::Time, schemas}; use crate::util::surrealdb::{client::Time, schemas};
use leptos::*; 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<schemas::ParticipantRecord> = 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. /// Renders the home page of your application.
#[component] #[component]
pub fn Modal() -> impl IntoView { pub fn Modal() -> impl IntoView {
let participant_id = use_context::<RwSignal<Option<String>>>().unwrap(); let participant_id = use_context::<RwSignal<Option<String>>>().unwrap();
let participants = use_context::<schemas::ParticipantsContext>().unwrap(); let participants = use_context::<schemas::ParticipantsContext>().unwrap();
let participant = move || {
let x = participants
.get()
.into_iter()
.filter(|item| item.id == participant_id.get().unwrap_or("".to_string()))
.collect::<Vec<schemas::ParticipantSignal>>();
match x.get(0) {
Some(participant) => Some(participant.value),
None => None,
}
};
let time_lifesaver = create_rw_signal(Time { let time_lifesaver = create_rw_signal(Time {
minutes: 0, minutes: 0,
seconds: 0, seconds: 0,
@ -38,51 +74,120 @@ pub fn Modal() -> impl IntoView {
milliseconds: 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::<Vec<schemas::ParticipantSignal>>();
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! { view! {
{ move || match participant() { { move || match participant() {
Some(p) => view! { Some(_p) => view! {
<div class="modal-background"> <div class="modal-background">
<div class="modal"> <div class="modal">
<form> <form on:submit=form_submit>
<h2>"Deelnemer bewerken"</h2> <h2>"Deelnemer bewerken"</h2>
<label>Naam</label> <label>Naam</label>
<input type="text" name="name" autocomplete="off" /> <input
type="text"
autocomplete="off"
on:input=move |ev| {
name.set(event_target_value(&ev))
}
prop:value=name
/>
<label>Groep</label> <label>Groep</label>
<select name="group" autocomplete="off"> <select name="group" autocomplete="off">
<option value="A1">A1</option> <SelectOption value=group is="A1" />
<option value="A2">A2</option> <SelectOption value=group is="A2" />
<option value="A3">A3</option> <SelectOption value=group is="A3" />
<option value="A4">A4</option> <SelectOption value=group is="A4" />
<option value="A5">A5</option> <SelectOption value=group is="A5" />
<option value="A6">A6</option> <SelectOption value=group is="A6" />
<option value="B1">B1</option> <SelectOption value=group is="B1" />
<option value="B2">B2</option> <SelectOption value=group is="B2" />
<option value="B3">B3</option> <SelectOption value=group is="B3" />
<option value="B4">B4</option> <SelectOption value=group is="B4" />
<option value="B5">B5</option> <SelectOption value=group is="B5" />
<option value="B6">B6</option> <SelectOption value=group is="B6" />
<option value="C1">C1</option> <SelectOption value=group is="C1" />
<option value="C2">C2</option> <SelectOption value=group is="C2" />
<option value="C3">C3</option> <SelectOption value=group is="C3" />
<option value="C4">C4</option> <SelectOption value=group is="C4" />
<option value="C5">C5</option> <SelectOption value=group is="C5" />
<option value="C6">C6</option> <SelectOption value=group is="C6" />
<option value="D1">D1</option> <SelectOption value=group is="D1" />
<option value="D2">D2</option> <SelectOption value=group is="D2" />
<option value="D3">D3</option> <SelectOption value=group is="D3" />
<option value="D4">D4</option> <SelectOption value=group is="D4" />
<option value="D5">D5</option> <SelectOption value=group is="D5" />
<option value="D6">D6</option> <SelectOption value=group is="D6" />
<option value="Z1">Z1</option> <SelectOption value=group is="Z1" />
<option value="Z2">Z2</option> <SelectOption value=group is="Z2" />
<option value="Z3">Z3</option> <SelectOption value=group is="Z3" />
<option value="Z4">Z4</option> <SelectOption value=group is="Z4" />
<option value="Z5">Z5</option> <SelectOption value=group is="Z5" />
<option value="Z6">Z6</option> <SelectOption value=group is="Z6" />
</select> </select>
<label>Tijd Lifesaver</label> <label>Tijd Lifesaver</label>
<div class="time"> <div class="time">
@ -220,3 +325,15 @@ pub fn Modal() -> impl IntoView {
}} }}
} }
} }
#[component]
pub fn SelectOption(is: &'static str, value: RwSignal<String>) -> impl IntoView {
view! {
<option
value=is
selected=move || value.get() == is
>
{is}
</option>
}
}

View File

@ -23,6 +23,20 @@ impl Time {
format!("{minutes}:{seconds}:{milliseconds}") format!("{minutes}:{seconds}:{milliseconds}")
} }
pub fn from_milliseconds(mut time: u32) -> Self {
let milliseconds = (time % 1000) / 10;
time /= 1000;
let seconds = time % 60;
time /= 60;
let minutes = time;
Self {
milliseconds,
seconds,
minutes,
}
}
} }
pub fn sort_participants( pub fn sort_participants(

View File

@ -31,6 +31,23 @@ pub struct Participant {
pub events: Option<Events>, pub events: Option<Events>,
} }
impl Participant {
pub fn as_update(&self) -> ParticipantUpdate {
ParticipantUpdate {
name: self.name.clone(),
group: self.group.clone(),
events: self.events.clone(),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ParticipantUpdate {
pub name: String,
pub group: String,
pub events: Option<Events>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ParticipantSignal { pub struct ParticipantSignal {
pub id: String, pub id: String,

View File

@ -16,7 +16,6 @@
.modal { .modal {
width: 100%; width: 100%;
max-width: 500px; max-width: 500px;
height: 100%;
max-height: 800px; max-height: 800px;
overflow-y: auto; overflow-y: auto;
background-color: $secondary-bg-color; background-color: $secondary-bg-color;