From 277782f16bf70577d2725a4f33d44ee52261b863 Mon Sep 17 00:00:00 2001 From: Daan Vanoverloop Date: Mon, 5 Sep 2022 19:27:41 +0200 Subject: [PATCH] Temp --- web/dist/index.html | 6 +-- web/src/pages/users.rs | 119 ++++++++++++++++++----------------------- 2 files changed, 54 insertions(+), 71 deletions(-) diff --git a/web/dist/index.html b/web/dist/index.html index 2b5ef4d..b581e63 100644 --- a/web/dist/index.html +++ b/web/dist/index.html @@ -4,9 +4,9 @@ Yew App - - + + - \ No newline at end of file + \ No newline at end of file diff --git a/web/src/pages/users.rs b/web/src/pages/users.rs index 43ad946..3595b61 100644 --- a/web/src/pages/users.rs +++ b/web/src/pages/users.rs @@ -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> = use_state(|| Option::None); let current_score = use_state(|| String::new()); - let users: UseAsyncHandle, _> = use_async(async move { - api_request::<_, Vec>("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>("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 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]; - 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 user: &User = &users[i]; + api_request::<_, ()>("DELETE", &format!("/user/{}", user.name), Option::<()>::None).await.unwrap(); + 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::("POST", "/user", Some((*new_username).clone())).await.unwrap(); - let mut cloned = users_inner.clone(); - cloned.push(user.unwrap()); - users.update(cloned); - } + let user = api_request::("POST", "/user", Some((*new_username).clone())).await.unwrap(); + let mut cloned = (*users).clone(); + cloned.push(user.unwrap()); + users.set(cloned); }); }); }); @@ -83,43 +74,35 @@ pub fn users_page() -> Html { html! { - { if let Some(users) = &users.data { - {users.iter().enumerate().map(move |(i, user)| html! { - - - - - - }).collect::()} - } else { - html! { -
- -
- } - }} + {users.iter().enumerate().map(move |(i, user)| html! { + + + + + + }).collect::()}
{&user.name} - {if Some(i) == *score_edit.clone() { html! { - <> - - - - -
{&user.name} + {if Some(i) == *score_edit.clone() { html! { + <> + + + + +