Merge new-person into main

This commit is contained in:
xeovalyte 2024-03-19 18:39:21 +01:00
parent 0cbbafb6ec
commit 59d6f7b37e
No known key found for this signature in database
4 changed files with 110 additions and 2 deletions

View File

@ -83,7 +83,7 @@ a {
background-color: $secondary-bg-color-light; background-color: $secondary-bg-color-light;
} }
input { input,select {
background-color: $secondary-bg-color-light; background-color: $secondary-bg-color-light;
border: none; border: none;
border-radius: 3px; border-radius: 3px;

View File

@ -1,5 +1,5 @@
use leptos::*;
use application::App; use application::App;
use leptos::*;
fn main() { fn main() {
// set up logging // set up logging

View File

@ -1,9 +1,90 @@
use leptos::*; use leptos::*;
#[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 {
let websocket = expect_context::<crate::util::surrealdb::SurrealContext>();
let (name, set_name) = create_signal(String::from(""));
let (group, set_group) = create_signal(String::from("A1"));
let (error, set_error) = create_signal(String::from(""));
let on_submit = move |ev: leptos::ev::SubmitEvent| {
ev.prevent_default();
set_error.set(String::from(""));
match websocket.add_person(name.get(), group.get()) {
Ok(_) => set_name.set(String::from("")),
Err(err) => set_error.set(err),
}
};
view! { view! {
<h1>"Deelnemer toevoegen"</h1> <h1>"Deelnemer toevoegen"</h1>
<form class="add" on:submit=on_submit>
<h1>"WRB Timings"</h1>
<select
on:change=move |ev| {
set_group.set(event_target_value(&ev));
}
>
<SelectOption value=group is="A1"/>
<SelectOption value=group is="A2"/>
<SelectOption value=group is="A3"/>
<SelectOption value=group is="A4"/>
<SelectOption value=group is="A5"/>
<SelectOption value=group is="A6"/>
<SelectOption value=group is="B1"/>
<SelectOption value=group is="B2"/>
<SelectOption value=group is="B3"/>
<SelectOption value=group is="B4"/>
<SelectOption value=group is="B5"/>
<SelectOption value=group is="B6"/>
<SelectOption value=group is="C1"/>
<SelectOption value=group is="C2"/>
<SelectOption value=group is="C3"/>
<SelectOption value=group is="C4"/>
<SelectOption value=group is="C5"/>
<SelectOption value=group is="C6"/>
<SelectOption value=group is="D1"/>
<SelectOption value=group is="D2"/>
<SelectOption value=group is="D3"/>
<SelectOption value=group is="D4"/>
<SelectOption value=group is="D5"/>
<SelectOption value=group is="D6"/>
<SelectOption value=group is="Z1"/>
<SelectOption value=group is="Z2"/>
<SelectOption value=group is="Z3"/>
<SelectOption value=group is="Z4"/>
<SelectOption value=group is="Z5"/>
<SelectOption value=group is="Z6"/>
</select>
<input type="text"
placeholder="Name"
on:input=move |ev| {
set_name.set(event_target_value(&ev));
}
prop:value=name
/>
<input type="submit" value="Submit" />
</form>
<p class="error">
{ error }
</p>
} }
} }

View File

@ -17,6 +17,7 @@ struct SurrealRequest {
enum SurrealParams { enum SurrealParams {
Participant, Participant,
SigninParam(SigninParam), SigninParam(SigninParam),
CreatePersonParam(CreatePersonParam),
String(String), String(String),
} }
@ -26,6 +27,12 @@ struct SigninParam {
pass: String, pass: String,
} }
#[derive(Serialize)]
struct CreatePersonParam {
name: String,
group: String,
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Participant { pub struct Participant {
name: String, name: String,
@ -95,6 +102,26 @@ impl SurrealContext {
self.send(&json!(request).to_string()); self.send(&json!(request).to_string());
} }
pub fn add_person(&self, name: String, group: String) -> Result<(), String> {
if name.is_empty() {
return Err(String::from("Name cannot be empty"));
}
let request = SurrealRequest {
id: 1,
method: String::from("create"),
params: vec![
SurrealParams::String(String::from("person")),
SurrealParams::CreatePersonParam(CreatePersonParam {
name,
group: "group:".to_owned() + &group,
}),
],
};
Ok(self.send(&json!(request).to_string()))
}
} }
pub fn init_surrealdb() { pub fn init_surrealdb() {