2022-09-15 11:46:20 +02:00
|
|
|
use crate::{
|
|
|
|
components::{messages::Messenger, Button, Table},
|
2022-09-15 16:32:42 +02:00
|
|
|
state::{EventsExt, UsersExt},
|
2022-09-15 11:46:20 +02:00
|
|
|
};
|
2022-09-15 16:32:42 +02:00
|
|
|
use lan_party_core::state::{Events, Users};
|
2022-09-07 15:03:47 +02:00
|
|
|
use sycamore::{futures::spawn_local_scoped, prelude::*};
|
|
|
|
|
|
|
|
#[component]
|
|
|
|
pub fn UsersPage<'a, G: Html>(cx: Scope<'a>) -> View<G> {
|
2022-09-15 11:46:20 +02:00
|
|
|
//let users = create_signal(cx, Vec::<User>::new());
|
|
|
|
let messenger = use_context::<Messenger>(cx);
|
|
|
|
let users = use_context::<Users>(cx);
|
2022-09-05 13:37:38 +02:00
|
|
|
let headers = vec!["Username".into(), "Score".into(), "".into()];
|
2022-09-04 19:43:16 +02:00
|
|
|
|
2022-09-07 15:03:47 +02:00
|
|
|
let score_edit = create_signal(cx, Option::<String>::None);
|
|
|
|
let new_score = create_signal(cx, String::new());
|
|
|
|
let new_username = create_signal(cx, String::new());
|
2022-09-04 19:43:16 +02:00
|
|
|
|
2022-09-15 11:46:20 +02:00
|
|
|
let reload = move || {
|
|
|
|
spawn_local_scoped(cx, async move {
|
|
|
|
messenger.add_result(
|
|
|
|
users.load().await,
|
|
|
|
Option::<String>::None,
|
|
|
|
Some("Failed to load users"),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
reload();
|
2022-09-04 19:43:16 +02:00
|
|
|
|
2022-09-07 15:03:47 +02:00
|
|
|
let ondelete = move |name: String| {
|
2022-09-15 11:46:20 +02:00
|
|
|
move |_| {
|
2022-09-07 15:03:47 +02:00
|
|
|
let name = name.clone();
|
|
|
|
spawn_local_scoped(cx, async move {
|
2022-09-15 11:46:20 +02:00
|
|
|
messenger.add_result(
|
|
|
|
users.delete(&name).await,
|
|
|
|
Some(format!("Deleted user {}", name)),
|
|
|
|
Some("Failed to delete user"),
|
|
|
|
);
|
2022-09-07 15:03:47 +02:00
|
|
|
});
|
2022-09-05 20:16:13 +02:00
|
|
|
}
|
2022-09-07 15:03:47 +02:00
|
|
|
};
|
2022-09-04 19:43:16 +02:00
|
|
|
|
2022-09-07 15:03:47 +02:00
|
|
|
let oncheck = move |_| {
|
|
|
|
spawn_local_scoped(cx, async move {
|
2022-09-15 11:46:20 +02:00
|
|
|
if let (Some(name), Ok(score)) = (score_edit.get().as_ref(), new_score.get().parse()) {
|
2022-09-07 15:03:47 +02:00
|
|
|
let score: i64 = score;
|
2022-09-15 11:46:20 +02:00
|
|
|
messenger.add_result(
|
|
|
|
users.update_score(&name, score).await,
|
|
|
|
Some(format!("Updated score for user {}", name)),
|
|
|
|
Some("Failed to delete user"),
|
|
|
|
);
|
2022-09-07 15:03:47 +02:00
|
|
|
}
|
|
|
|
score_edit.set(None);
|
|
|
|
})
|
|
|
|
};
|
2022-09-04 19:43:16 +02:00
|
|
|
|
2022-09-07 15:03:47 +02:00
|
|
|
let onadd = move |_| {
|
|
|
|
spawn_local_scoped(cx, async move {
|
2022-09-15 11:46:20 +02:00
|
|
|
messenger.add_result(
|
|
|
|
users.add(&new_username.get()).await,
|
|
|
|
Some(format!("Added new user {}", new_username.get())),
|
|
|
|
Some("Failed to add user"),
|
|
|
|
);
|
2022-09-07 15:03:47 +02:00
|
|
|
});
|
|
|
|
};
|
2022-09-05 16:48:55 +02:00
|
|
|
|
2022-09-07 15:03:47 +02:00
|
|
|
view! { cx,
|
2022-09-15 11:46:20 +02:00
|
|
|
Button(text="Reload".into(), icon="mdi-refresh".into(), onclick=move |_| reload())
|
2022-09-09 13:01:30 +02:00
|
|
|
Table(headers=headers) {
|
|
|
|
Keyed(
|
2022-09-15 11:46:20 +02:00
|
|
|
iterable=users.get(),
|
2022-09-09 13:01:30 +02:00
|
|
|
view=move |cx, user| {
|
|
|
|
let user = create_ref(cx, user);
|
|
|
|
view! { cx,
|
|
|
|
tr {
|
|
|
|
td { (user.name) }
|
|
|
|
td {
|
|
|
|
(if Some(&user.name) == (*score_edit.get()).as_ref() { view! { cx,
|
|
|
|
span(class="user-score") {
|
|
|
|
input(bind:value=new_score)
|
|
|
|
}
|
|
|
|
Button(icon="mdi-check".into(), onclick=oncheck)
|
|
|
|
}} else { view! { cx,
|
|
|
|
span(class="user-score") {
|
|
|
|
(user.score)
|
|
|
|
}
|
|
|
|
Button(
|
|
|
|
icon="mdi-pencil".into(),
|
|
|
|
onclick=move |_| {
|
|
|
|
score_edit.set(Some(user.name.clone()));
|
|
|
|
new_score.set(user.score.to_string());
|
2022-09-07 15:03:47 +02:00
|
|
|
}
|
2022-09-09 13:01:30 +02:00
|
|
|
)
|
|
|
|
}})
|
|
|
|
}
|
|
|
|
td {
|
|
|
|
Button(icon="mdi-delete".into(),onclick=ondelete(user.name.clone()))
|
2022-09-07 15:03:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 13:01:30 +02:00
|
|
|
},
|
|
|
|
key=|user| (user.name.clone(), user.score.clone()),
|
|
|
|
)
|
|
|
|
tr {
|
|
|
|
td {
|
|
|
|
span {
|
|
|
|
input(bind:value=new_username)
|
2022-09-07 15:03:47 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-09 13:01:30 +02:00
|
|
|
td {}
|
|
|
|
td {
|
|
|
|
Button(icon="mdi-plus".into(),onclick=onadd)
|
|
|
|
}
|
2022-09-07 15:03:47 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-04 19:43:16 +02:00
|
|
|
}
|
|
|
|
}
|