Add the ability to add users
All checks were successful
Build Crate / build (push) Successful in 3m25s

This commit is contained in:
Jacob Kassman
2023-12-09 21:49:14 -05:00
parent cb5da9b966
commit 01eaf059dd
6 changed files with 99 additions and 8 deletions

View File

@@ -7,9 +7,9 @@ use web_sys::Event;
cfg_if::cfg_if! {
if #[cfg(client)] {
use crate::data::pool_match::{PoolMatch, PoolMatchList};
use crate::data::pool_match::{PoolMatch, PoolMatchList, UserList};
use crate::templates::global_state::AppStateRx;
use crate::endpoints::MATCH;
use crate::endpoints::{MATCH, USER};
use crate::templates::get_api_path;
use chrono::Utc;
}
@@ -20,14 +20,15 @@ cfg_if::cfg_if! {
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
#[rx(alias = "PageStateRx")]
struct PageState {
name: String,
winner: String,
new_user: String,
}
fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> {
let handle_add_match = move |_event: Event| {
#[cfg(client)]
{
// state.name.get().as_ref().clone()
// state.winner.get().as_ref().clone()
spawn_local_scoped(cx, async move {
let new_match = PoolMatch::new(
MatchData {
@@ -53,10 +54,37 @@ fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStat
}
};
let handle_add_user = move |_event: Event| {
#[cfg(client)]
{
// state.winner.get().as_ref().clone()
spawn_local_scoped(cx, async move {
let client = reqwest::Client::new();
let new_users = client
.post(get_api_path(USER).as_str())
.body(state.new_user.get().as_ref().clone())
.send()
.await
.unwrap()
.json::<UserList>()
.await
.unwrap();
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
global_state.users.set(new_users);
})
}
};
view! { cx,
Layout(title = "Add Game Results") {
div (class = "flex flex-wrap") {
input (bind:value = state.name,
select {
option (value="red")
option (value="blue")
}
}
div (class = "flex flex-wrap") {
input (bind:value = state.winner,
class = "appearance-none block w-full bg-gray-200 text-gray-700 border \
border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none \
focus:bg-white",)
@@ -69,6 +97,20 @@ fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStat
"Add result"
}
}
div (class = "flex flex-wrap") {
input (bind:value = state.new_user,
class = "appearance-none block w-full bg-gray-200 text-gray-700 border \
border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none \
focus:bg-white",)
}
div (class = "flex flex-wrap") {
button(on:click = handle_add_user,
class = "flex-shrink-0 bg-teal-500 hover:bg-teal-700 border-teal-500 \
hover:border-teal-700 text-sm border-4 text-white py-1 px-2 rounded",
) {
"Add new user"
}
}
}
}
}
@@ -79,7 +121,8 @@ async fn get_request_state(
_req: Request,
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
Ok(PageState {
name: "Ferris".to_string(),
winner: "Ferris".to_string(),
new_user: "newguy".to_string(),
})
}

View File

@@ -1,6 +1,8 @@
// Not a page, global state that is shared between all pages
use crate::data::pool_match::PoolMatchList;
use crate::data::pool_match::UserList;
use perseus::{prelude::*, state::GlobalStateCreator};
use serde::{Deserialize, Serialize};
@@ -16,6 +18,7 @@ cfg_if::cfg_if! {
#[rx(alias = "AppStateRx")]
pub struct AppState {
pub matches: PoolMatchList,
pub users: UserList,
}
pub fn get_global_state_creator() -> GlobalStateCreator {
@@ -30,7 +33,11 @@ fn get_state() -> AppState {
.join()
.unwrap();
AppState { matches }
let users = thread::spawn(move || DATA.lock().unwrap().deref().users.clone())
.join()
.unwrap();
AppState { matches, users }
}
#[engine_only_fn]