diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-12-18 15:33:02 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-12-18 15:33:02 -0500 |
commit | 27f74d8c366be675e7ab64ca746496a66b3cf024 (patch) | |
tree | 7bee8fee0b8753d1c3974e33bcb155b509bae100 /webapp/src/components | |
parent | 4512c5ead9406206de32e37490b7a4ac792d93bf (diff) |
Add bidding from webapp
Diffstat (limited to 'webapp/src/components')
-rw-r--r-- | webapp/src/components/table.rs | 79 |
1 files changed, 61 insertions, 18 deletions
diff --git a/webapp/src/components/table.rs b/webapp/src/components/table.rs index 7c0b3a3..17c37b1 100644 --- a/webapp/src/components/table.rs +++ b/webapp/src/components/table.rs @@ -1,31 +1,51 @@ +use std::future::Future; +use std::pin::Pin; + use crate::components::{BiddingBox, BiddingTable, Hand}; use crate::use_app_context; use crate::utils::ok_json; use anyhow::Context; use gloo_net::http::Request; use log::info; -use protocol::bridge_engine::{GameStatePlayerView, BiddingState, BiddingStatePlayerView}; +use protocol::bridge_engine::{ + Bid, BiddingState, BiddingStatePlayerView, GameStatePlayerView, +}; use yew::prelude::*; #[function_component(OnlineTable)] pub fn online_table(props: &OnlineTableProps) -> Html { let ctx = use_app_context(); - let table_state: UseStateHandle<Option<GameStatePlayerView>> = use_state(|| None); - { - // TODO update this from server state + let table_state: UseStateHandle<Option<GameStatePlayerView>> = + use_state(|| None); + + let update_table_state = { let table_state = table_state.clone(); let props = props.clone(); + || { + Box::pin(async move { + // let table_state = table_state.clone(); + let props = props.clone(); + let response = + Request::get(&format!("/api/table/{}", props.table.id)) + .send() + .await + .context("fetching table data")?; + let table = ok_json(response).await?; + table_state.set(Some(table)); + Ok(()) + }) + as Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>> + } + }; + + { let ctx = ctx.clone(); + let update_table_state = update_table_state.clone(); use_effect_with_deps( move |_| { ctx.spawn_async(async move { - let response = Request::get(&format!("/api/table/{}", props.table.id)) - .send() - .await - .context("fetching table data")?; - let table = ok_json(response).await?; - table_state.set(Some(table)); + update_table_state().await?; Ok(()) }); || () @@ -34,6 +54,29 @@ pub fn online_table(props: &OnlineTableProps) -> Html { ); } + let on_bid = { + let ctx = ctx.clone(); + let props = props.clone(); + let update_table_state = update_table_state.clone(); + Callback::from(move |bid| { + let update_table_state = update_table_state.clone(); + info!("Bid clicked: {:?}", bid); + ctx.spawn_async(async move { + let bid_response = Request::post(&format!( + "/api/table/{}/bid", + props.table.id + )) + .json(&bid)? + .send() + .await + .context("submitting bid")?; + let () = ok_json(bid_response).await?; + update_table_state().await?; + Ok(()) + }); + }) + }; + let leave_table = { let ctx = ctx.clone(); Callback::from(move |_| { @@ -48,7 +91,7 @@ pub fn online_table(props: &OnlineTableProps) -> Html { { "Leave table" } </button> if let Some(table_state) = &*table_state { - <Table table={ table_state.clone() }/> + <Table table={ table_state.clone() } { on_bid } /> } </> } @@ -66,16 +109,15 @@ pub fn table(props: &TableProps) -> Html { info!("Card clicked: {}", card); }) }; - let on_bid = { - Callback::from(move |bid| { - info!("Bid clicked: {:?}", bid); - }) - }; + let center = match &props.table { - GameStatePlayerView::Bidding(BiddingStatePlayerView { bidding, .. }) => html! { + GameStatePlayerView::Bidding(BiddingStatePlayerView { + bidding, + .. + }) => html! { <> <BiddingTable bidding={bidding.clone()} /> - <BiddingBox current_bid={bidding.highest_bid().clone()} { on_bid } /> + <BiddingBox current_bid={bidding.highest_bid().clone()} on_bid={ props.on_bid.clone() } /> { format!("It is {:?} to bid", bidding.current_bidder()) } </> }, @@ -100,4 +142,5 @@ pub fn table(props: &TableProps) -> Html { #[derive(PartialEq, Properties, Clone)] pub struct TableProps { pub table: GameStatePlayerView, + pub on_bid: Callback<Bid>, } |