From 4c0109a8c40012f75e3d0d900c0ef41893cfb4bb Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Mon, 14 Nov 2022 19:49:52 -0500 Subject: Fetch table state from server --- webapp/src/components/app_context_provider.rs | 6 +++-- webapp/src/components/table.rs | 22 +++++++++++----- webapp/src/main.rs | 38 ++++++++++++++++++--------- 3 files changed, 45 insertions(+), 21 deletions(-) (limited to 'webapp') diff --git a/webapp/src/components/app_context_provider.rs b/webapp/src/components/app_context_provider.rs index 19e7611..0b490fc 100644 --- a/webapp/src/components/app_context_provider.rs +++ b/webapp/src/components/app_context_provider.rs @@ -1,3 +1,4 @@ +use log::error; use crate::routing::Route; use gloo_net::http::Request; use log::info; @@ -26,13 +27,14 @@ pub struct AppContext { } impl AppContext { - fn spawn_async(&self, f: F) + pub fn spawn_async(&self, f: F) where F: Future> + 'static, { let error = self.state.error.clone(); spawn_local(async move { if let Err(err) = f.await { + error!("Error occurred: {err:?}"); error.set(Some(ErrorInfoProperties { message: format!("Some error occured: {:?}", err), })); @@ -47,7 +49,7 @@ impl AppContext { pub fn error(&self) -> Option<&ErrorInfoProperties> { self.state.error.as_ref() } - + pub fn create_table(&self) { let user = self.state.user.clone(); let history = self.history.clone(); diff --git a/webapp/src/components/table.rs b/webapp/src/components/table.rs index 829441e..b8da804 100644 --- a/webapp/src/components/table.rs +++ b/webapp/src/components/table.rs @@ -1,3 +1,4 @@ +use log::info; use yew::prelude::*; use yew_router::prelude::*; @@ -6,21 +7,30 @@ use crate::use_app_context; #[function_component(Table)] pub fn table(props: &TableProps) -> Html { let ctx = use_app_context(); - let history = use_history().unwrap(); + + let table_state: UseStateHandle> = use_state(|| None); + { + let table_state = table_state.clone(); + let ctx = ctx.clone(); + ctx.spawn_async(async move { + info!("Getting table state"); + Err(anyhow::anyhow!("Not implemented yet")) + }); + } let leave_table = { let ctx = ctx.clone(); Callback::from(move |_| { - ctx.leave_table(); - }) + ctx.leave_table(); + }) }; html! { <>

{ format!("This is table {}", props.table.id) }

- + } } diff --git a/webapp/src/main.rs b/webapp/src/main.rs index 577b720..35e69fd 100644 --- a/webapp/src/main.rs +++ b/webapp/src/main.rs @@ -11,7 +11,7 @@ use components::{AppContext, AppContextProvider, ErrorInfo, Game, Table}; use gloo_net::http::Request; extern crate wee_alloc; pub mod routing; -use crate::{routing::Route, components::use_app_context}; +use crate::{components::use_app_context, routing::Route}; use uuid::Uuid; // Use `wee_alloc` as the global allocator. @@ -41,8 +41,6 @@ pub fn app() -> Html { fn home() -> Html { let ctx = use_app_context(); - info!("User: {:#?}", ctx.user()); - let user = match &ctx.user() { Some(userinfo) => html! {

{ format!("Logged in as {}", userinfo.username) }

@@ -56,28 +54,35 @@ fn home() -> Html { } let create_table = { - let ctx = ctx.clone(); - Callback::from(move |_| { - ctx.create_table(); - }) + let ctx = ctx.clone(); + Callback::from(move |_| { + ctx.create_table(); + }) }; + info!("home(): Error is {:?}", ctx.error()); + html! { - <> - if let Some(error) = &ctx.error() { - - }
  • { user }
  • to={Route::Playground}>{ "Playground" }>
- + } +} + +#[function_component(Header)] +fn header() -> Html { + let ctx = use_app_context(); + html! { + if let Some(error) = &ctx.error() { + + } } } fn switch(routes: &Route) -> Html { - match routes { + let main = match routes { Route::Home => html! { }, Route::Playground => html! {
@@ -85,6 +90,13 @@ fn switch(routes: &Route) -> Html { Route::Table { id } => html! { }, + }; + + html! { + <> +
+ { main } + } } -- cgit v1.2.3