diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-11-13 16:07:27 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-11-13 16:07:27 -0500 |
commit | d4650a3160d52d289686fb59efbf8f0a436b71eb (patch) | |
tree | 0b38d0143824a3a7cab83e189fe1a2302f9685a7 /webapp/src/main.rs | |
parent | 0012c25c9ab94754f3d6396e91dc3ae63d19ac9c (diff) |
Add create/leave table options
Diffstat (limited to 'webapp/src/main.rs')
-rw-r--r-- | webapp/src/main.rs | 65 |
1 files changed, 28 insertions, 37 deletions
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::<Rc<AppContext>>().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! { - <p>{ format!("Logged in as {}", userinfo.username) }</p> - }, + <p>{ format!("Logged in as {}", userinfo.username) }</p> + }, None => html! { <p><a href="/api/login">{ "Log in" }</a></p> }, }; - 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 { - <ErrorInfo ..error.clone()/> + if let Some(error) = &ctx.error() { + <ErrorInfo ..(*error).clone()/> } - <ul> - <li>{ user }</li> + <ul> + <li>{ user }</li> <li><Link<Route> to={Route::Playground}>{ "Playground" }</Link<Route>></li> - <li><button onclick={create_table}>{ "Create table" }</button></li> - </ul> + <li><button onclick={create_table}>{ "Create table" }</button></li> + </ul> </> } } fn switch(routes: &Route) -> Html { match routes { - Route::Home => html!{ <Home/> }, + Route::Home => html! { <Home/> }, Route::Playground => html! { <div class="game-layout"><Game /></div> }, - Route::Table { id } => html! { - <Table table={ protocol::Table { id: *id } } /> - }, + Route::Table { id } => html! { + <Table table={ protocol::Table { id: *id } } /> + }, } } |