This commit is contained in:
Daan Vanoverloop 2022-09-05 19:27:41 +02:00
parent 3ca53db260
commit 277782f16b
Signed by: Danacus
GPG Key ID: F2272B50E129FC5C
2 changed files with 54 additions and 71 deletions

6
web/dist/index.html vendored
View File

@ -4,9 +4,9 @@
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.9.96/css/materialdesignicons.min.css" rel="stylesheet">
<title>Yew App</title>
<link rel="preload" href="/index-167b3e062ee4dd95_bg.wasm" as="fetch" type="application/wasm" crossorigin="">
<link rel="modulepreload" href="/index-167b3e062ee4dd95.js"></head>
<link rel="preload" href="/index-d56dfc869fea14a4_bg.wasm" as="fetch" type="application/wasm" crossorigin="">
<link rel="modulepreload" href="/index-d56dfc869fea14a4.js"></head>
<body class="base theme-dark bg-gray-900 text-gray-400">
<script type="module">import init from '/index-167b3e062ee4dd95.js';init('/index-167b3e062ee4dd95_bg.wasm');</script></body></html>
<script type="module">import init from '/index-d56dfc869fea14a4.js';init('/index-d56dfc869fea14a4_bg.wasm');</script></body></html>

View File

@ -6,7 +6,6 @@ use crate::{
use lan_party_core::user::User;
use wasm_bindgen_futures::spawn_local;
use yew::prelude::*;
use yew_hooks::*;
#[function_component(UsersPage)]
pub fn users_page() -> Html {
@ -15,30 +14,28 @@ pub fn users_page() -> Html {
let new_username = use_state(|| String::new());
let score_edit: UseStateHandle<Option<usize>> = use_state(|| Option::None);
let current_score = use_state(|| String::new());
let users: UseAsyncHandle<Vec<User>, _> = use_async(async move {
api_request::<_, Vec<User>>("GET", "/user", Option::<()>::None)
.await
.map(|inner| inner.unwrap())
.map_err(|_| "failed to load users")
});
let users = use_state(|| Vec::new());
clone!(users; use_effect_with_deps(move |_| {
if users.data.is_none() {
users.run();
}
spawn_local(async move {
users.set(api_request::<_, Vec<User>>("GET", "/user", Option::<()>::None)
.await
.map(|inner| inner.unwrap())
.unwrap());
});
|| ()
}, ()));
let oncheck = clone_cb!(score_edit, current_score, users; move |_| {
clone!(score_edit, users, current_score; {
spawn_local(async move {
if let (Some(score_edit), Some(users_inner)) = (*score_edit, &users.data) {
if let Some(score_edit) = *score_edit {
if let Ok(score) = current_score.parse() {
let user: &User = &users_inner[score_edit];
let user: &User = &users[score_edit];
api_request::<_, ()>("POST", &format!("/user/{}/score", user.name), Some(score)).await.unwrap();
let mut cloned = users_inner.clone();
let mut cloned = (*users).clone();
cloned[score_edit].score = score;
users.update(cloned);
users.set(cloned);
}
}
score_edit.set(None);
@ -47,22 +44,18 @@ pub fn users_page() -> Html {
});
let onedit = clone_cb!(current_score, score_edit, users; i => move |_| {
if let Some(users) = &users.data {
let user: &User = &users[i];
current_score.set(user.score.to_string());
score_edit.set(Some(i));
}
});
let ondelete = clone_cb!(users; i => move |_| {
clone!(users; {
spawn_local(async move {
if let Some(users_inner) = &users.data {
let user: &User = &users_inner[i];
let user: &User = &users[i];
api_request::<_, ()>("DELETE", &format!("/user/{}", user.name), Option::<()>::None).await.unwrap();
let cloned = users_inner.iter().cloned().filter(|u| u.name != user.name).collect();
users.update(cloned);
}
let cloned = users.iter().cloned().filter(|u| u.name != user.name).collect();
users.set(cloned);
});
});
});
@ -70,12 +63,10 @@ pub fn users_page() -> Html {
let onadd = clone_cb!(new_username, users; move |_| {
clone!(new_username, users; {
spawn_local(async move {
if let Some(users_inner) = &users.data {
let user = api_request::<String, User>("POST", "/user", Some((*new_username).clone())).await.unwrap();
let mut cloned = users_inner.clone();
let mut cloned = (*users).clone();
cloned.push(user.unwrap());
users.update(cloned);
}
users.set(cloned);
});
});
});
@ -83,7 +74,6 @@ pub fn users_page() -> Html {
html! {
<Page>
<Table headers={headers.clone()} loading=false rows={vec![]}>
{ if let Some(users) = &users.data {
{users.iter().enumerate().map(move |(i, user)| html! {
<tr>
<td class="whitespace-nowrap px-3 py-4 text-sm text-slate-400">{&user.name}</td>
@ -113,13 +103,6 @@ pub fn users_page() -> Html {
</td>
</tr>
}).collect::<Html>()}
} else {
html! {
<div class="grid place-items-center">
<Loading />
</div>
}
}}
<tr>
<td class="whitespace-nowrap px-3 text-sm text-slate-400">
<span class="inline-block">