Added system to add Times to database

This commit is contained in:
xeovalyte 2024-04-02 18:40:06 +02:00
parent 01e9a65463
commit edde7f6604
No known key found for this signature in database
2 changed files with 127 additions and 9 deletions

View File

@ -1,3 +1,4 @@
use crate::util::surrealdb;
use leptos::{ev::keydown, *}; use leptos::{ev::keydown, *};
use leptos_use::*; use leptos_use::*;
use strsim::normalized_damerau_levenshtein; use strsim::normalized_damerau_levenshtein;
@ -33,6 +34,18 @@ fn sort_participants(participants: Vec<Participant>, search: String) -> Vec<Part
.collect() .collect()
} }
#[component]
pub fn SelectOption(is: &'static str, value: ReadSignal<String>) -> impl IntoView {
view! {
<option
value=is
selected=move || value.get() == is
>
{is}
</option>
}
}
/// Navigation bar /// Navigation bar
#[component] #[component]
pub fn Add() -> impl IntoView { pub fn Add() -> impl IntoView {
@ -40,9 +53,14 @@ pub fn Add() -> impl IntoView {
let participants = expect_context::<crate::util::surrealdb::ParticipantsContext>(); let participants = expect_context::<crate::util::surrealdb::ParticipantsContext>();
let container_ref: NodeRef<html::Ul> = create_node_ref(); let container_ref: NodeRef<html::Ul> = create_node_ref();
let name_ref: NodeRef<html::Input> = create_node_ref();
let (name, set_name) = create_signal(String::from("")); let (name, set_name) = create_signal(String::from(""));
let (event, set_event) = create_signal(String::from("lifesaver"));
let (error, set_error) = create_signal(String::from("")); let (error, set_error) = create_signal(String::from(""));
let (minutes, set_minutes) = create_signal::<u64>(0);
let (seconds, set_seconds) = create_signal::<u64>(0);
let (miliseconds, set_miliseconds) = create_signal::<u64>(0);
let (selected, set_selected) = create_signal::<usize>(0); let (selected, set_selected) = create_signal::<usize>(0);
let participants_sorted = let participants_sorted =
@ -79,20 +97,49 @@ pub fn Add() -> impl IntoView {
let on_submit = move |ev: leptos::ev::SubmitEvent| { let on_submit = move |ev: leptos::ev::SubmitEvent| {
ev.prevent_default(); ev.prevent_default();
set_error.set(String::from("")); set_error.set(String::from(""));
/*
match websocket.add_person(name.get(), group.get()) { let person = &participants_sorted.get()[selected.get()];
Ok(_) => set_name.set(String::from("")),
match websocket.add_time(
surrealdb::CreateTimeParam::new(
person.clone().id,
minutes.get(),
seconds.get(),
miliseconds.get(),
),
event.get(),
) {
Ok(_) => {
set_name.set(String::from(""));
set_minutes.set(0);
set_seconds.set(0);
set_miliseconds.set(0);
let _ = name_ref.get().unwrap().focus();
}
Err(err) => set_error.set(err), Err(err) => set_error.set(err),
} }
*/
}; };
view! { view! {
<h1>"Tijd toevoegen"</h1> <h1>"Tijd toevoegen"</h1>
<form class="add" on:submit=on_submit> <form class="add" on:submit=on_submit>
<div class="time-input-container">
<span>"Onderdeel:"</span>
<select
on:change=move |ev| {
set_event.set(event_target_value(&ev));
}
>
<SelectOption value=event is="lifesaver"/>
<SelectOption value=event is="popduiken"/>
<SelectOption value=event is="hindernis"/>
</select>
</div>
<input type="text" <input type="text"
placeholder="Name" placeholder="Name"
tabindex=0 tabindex=0
node_ref=name_ref
on:input=move |ev| { on:input=move |ev| {
set_name.set(event_target_value(&ev)); set_name.set(event_target_value(&ev));
set_selected.set(0); set_selected.set(0);
@ -106,9 +153,51 @@ pub fn Add() -> impl IntoView {
</ul> </ul>
<div class="time-input-container"> <div class="time-input-container">
<span>"Tijd:"</span> <span>"Tijd:"</span>
<input type="number" tabindex=0 placeholder="mm" min=0 max=99 /> <input
<input type="number" tabindex=0 placeholder="ss" min=0 max=59 /> type="number"
<input type="number" tabindex=0 placeholder="ms" min=0 max=99 /> tabindex=0
placeholder="mm"
min=0
max=99
on:input=move |ev| {
let x = event_target_value(&ev).parse::<u64>();
match x {
Ok(x) => set_minutes.set(x),
Err(_) => set_minutes.set(0),
}
}
prop:value=minutes
/>
<input
type="number"
tabindex=0
placeholder="ss"
min=0
max=59
on:input=move |ev| {
let x = event_target_value(&ev).parse::<u64>();
match x {
Ok(x) => set_seconds.set(x),
Err(_) => set_seconds.set(0),
}
}
prop:value=seconds
/>
<input
type="number"
tabindex=0
placeholder="ms"
min=0
max=99
on:input=move |ev| {
let x = event_target_value(&ev).parse::<u64>();
match x {
Ok(x) => set_miliseconds.set(x),
Err(_) => set_miliseconds.set(0),
}
}
prop:value=miliseconds
/>
</div> </div>
<input type="submit" tabindex=0 value="Submit" /> <input type="submit" tabindex=0 value="Submit" />
</form> </form>

View File

@ -3,7 +3,7 @@ use leptos::*;
use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn}; use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
use std::{alloc::handle_alloc_error, rc::Rc}; use std::{alloc::handle_alloc_error, rc::Rc, time::Duration};
#[derive(Serialize)] #[derive(Serialize)]
#[serde(untagged)] #[serde(untagged)]
@ -25,6 +25,7 @@ enum SurrealParams {
Participant, Participant,
SigninParam(SigninParam), SigninParam(SigninParam),
CreatePersonParam(CreatePersonParam), CreatePersonParam(CreatePersonParam),
CreateTimeParam(CreateTimeParam),
String(String), String(String),
} }
@ -40,9 +41,24 @@ struct CreatePersonParam {
group: String, group: String,
} }
#[derive(Serialize)]
pub struct CreateTimeParam {
person_id: String,
time: u64,
}
impl CreateTimeParam {
pub fn new(person_id: String, minutes: u64, seconds: u64, miliseconds: u64) -> Self {
Self {
person_id,
time: minutes * 60 * 1000 + seconds * 1000 + miliseconds * 10,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Participant { pub struct Participant {
id: String, pub id: String,
pub name: String, pub name: String,
pub group: String, pub group: String,
} }
@ -142,6 +158,19 @@ impl SurrealContext {
Ok(self.send(&json!(request).to_string())) Ok(self.send(&json!(request).to_string()))
} }
pub fn add_time(&self, body: CreateTimeParam, event: String) -> Result<(), String> {
let request = SurrealRequest {
id: SurrealId::Integer(20),
method: String::from("create"),
params: vec![
SurrealParams::String(event),
SurrealParams::CreateTimeParam(body),
],
};
Ok(self.send(&json!(request).to_string()))
}
} }
pub fn init_surrealdb() { pub fn init_surrealdb() {