From d4650a3160d52d289686fb59efbf8f0a436b71eb Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sun, 13 Nov 2022 16:07:27 -0500 Subject: Add create/leave table options --- webapp/src/main.rs | 65 +++++++++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 37 deletions(-) (limited to 'webapp/src/main.rs') diff --git a/webapp/src/main.rs b/webapp/src/main.rs index b275b49..577b720 100644 --- a/webapp/src/main.rs +++ b/webapp/src/main.rs @@ -2,29 +2,21 @@ use std::rc::Rc; #[allow(unused_imports)] use log::{debug, error, info, warn}; -use uuid::Uuid; use yew::prelude::*; use yew_router::prelude::*; pub mod bridge_engine; pub mod card; pub mod components; -use components::{AppContextProvider, AppContext, Game, ErrorInfo, Table}; +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 uuid::Uuid; // Use `wee_alloc` as the global allocator. #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; -#[derive(Clone, Routable, PartialEq)] - -enum Route { - #[at("/")] - Home, - #[at("/playground")] - Playground, - #[at("/table/:id")] - Table { id: Uuid, }, -} fn main() { std::panic::set_hook(Box::new(console_error_panic_hook::hook)); @@ -47,53 +39,52 @@ pub fn app() -> Html { #[function_component(Home)] fn home() -> Html { - let ctx = use_context::>().unwrap(); + let ctx = use_app_context(); - info!("User: {:#?}", ctx.user); + info!("User: {:#?}", ctx.user()); - let user = match &ctx.user { + let user = match &ctx.user() { Some(userinfo) => html! { -

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

- }, +

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

+ }, None => html! {

{ "Log in" }

}, }; - if let Some(table) = ctx.user.as_ref().and_then(|u| u.table.as_ref() ) { - let history = use_history().unwrap(); - history.push(Route::Table { id: table.id }); + if let Some(table) = ctx.user().as_ref().and_then(|u| u.table.as_ref()) { + let history = use_history().unwrap(); + history.push(Route::Table { id: table.id }); } - let create_table = Callback::from(|_| { - wasm_bindgen_futures::spawn_local(async move { - let response = Request::post("/api/table").send().await.unwrap(); - let table_id: Uuid = response.json().await.unwrap(); - info!("Created table {table_id}"); - }); - }); + let create_table = { + let ctx = ctx.clone(); + Callback::from(move |_| { + ctx.create_table(); + }) + }; html! { <> - if let Some(error) = &ctx.error { - + if let Some(error) = &ctx.error() { + } -
    -
  • { user }
  • +
      +
    • { user }
    • to={Route::Playground}>{ "Playground" }>
    • -
    • -
    +
  • +
} } fn switch(routes: &Route) -> Html { match routes { - Route::Home => html!{ }, + Route::Home => html! { }, Route::Playground => html! {
}, - Route::Table { id } => html! { - - }, + Route::Table { id } => html! { +
+ }, } } -- cgit v1.2.3