Add API
Some checks failed
Build Crate / build (push) Failing after 6m25s

This commit is contained in:
2023-09-20 21:32:19 -04:00
parent ec06340def
commit 3d52cd0a24
8 changed files with 116 additions and 28 deletions

View File

@@ -1,21 +1,48 @@
use std::ops::Deref;
use crate::components::layout::Layout;
use perseus::prelude::*;
use serde::{Deserialize, Serialize};
use sycamore::prelude::*;
use crate::data::global_state::AppStateRx;
use web_sys::{window, Event};
use crate::data::pool_match::PoolMatch;
#[cfg(client)]
use perseus::utils::get_path_prefix_client;
use crate::templates::get_api_path;
// Reactive page
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
#[rx(alias = "PageStateRx")]
struct PageState {
struct PageState {}
}
fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> {
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
let api_path = get_api_path("/api/test");
let handle_add_match = move |event: Event| {
#[cfg(client)]
{
let path = get_api_path("/api/test");
println!("{}", path);
spawn_local_scoped(cx, async move {
reqwest::get(get_api_path("/api/test").as_str()).await.unwrap();
})
}
};
view! { cx,
Layout(title = "Add Game Results") {
// Anything we put in here will be rendered inside the `<main>` block of the layout
p { "Results" }
button(on:click=handle_add_match) {
"Add result"
}
p {
(api_path)
}
}
}
}
@@ -35,7 +62,6 @@ fn head(cx: Scope) -> View<SsrNode> {
}
}
// Template
pub fn get_template<G: Html>() -> Template<G> {

View File

@@ -2,3 +2,20 @@ pub mod index;
pub mod add_game_form;
pub mod one_v_one_board;
pub mod overall_board;
#[cfg(client)]
use perseus::utils::get_path_prefix_client;
pub fn get_api_path(path: &str) -> String {
#[cfg(engine)]
{
path.to_string()
}
#[cfg(client)]
{
let path = web_sys::window().unwrap().location().pathname().unwrap();
// let base_path = get_path_prefix_client();
// format!("{}{}", base_path, path)
path.to_string()
}
}

View File

@@ -6,6 +6,7 @@ use crate::data::store::DATA;
#[cfg(engine)]
use std::thread;
use sycamore::prelude::*;
use crate::data::global_state::AppStateRx;
use crate::data::pool_match::{
PoolMatchList, PoolMatch
@@ -16,16 +17,19 @@ use crate::data::pool_match::{
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
#[rx(alias = "PageStateRx")]
struct PageState {
matches: PoolMatchList,
}
fn overall_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> {
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
view! { cx,
Layout(title = "Overall Leaderboard") {
// Anything we put in here will be rendered inside the `<main>` block of the layout
ul {
(View::new_fragment(
state.matches.get()
global_state.store.get()
.matches
.pool_matches
.iter()
.rev()
@@ -50,20 +54,7 @@ async fn get_request_state(
_info: StateGeneratorInfo<()>,
req: Request,
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
let matches = thread::spawn(move || {
let mut db = DATA.lock().unwrap();
db.matches.pool_matches.push(PoolMatch {
players: vec![],
winner: "lol".to_string(),
});
db.write();
db.matches.clone()
}).join().unwrap();
Ok(PageState {
matches
})
Ok(PageState {})
}
#[engine_only_fn]