summaryrefslogtreecommitdiff
path: root/webapp/src/components/bidding.rs
diff options
context:
space:
mode:
Diffstat (limited to 'webapp/src/components/bidding.rs')
-rw-r--r--webapp/src/components/bidding.rs78
1 files changed, 43 insertions, 35 deletions
diff --git a/webapp/src/components/bidding.rs b/webapp/src/components/bidding.rs
index b8fd893..26a4426 100644
--- a/webapp/src/components/bidding.rs
+++ b/webapp/src/components/bidding.rs
@@ -1,50 +1,58 @@
-use crate::bridge_engine::{Bid, BiddingResult, Player};
+use crate::bridge_engine::{self, Bid, BiddingResult, Contract, Player};
use crate::components::{BiddingBox, BiddingTable};
+use log::{info, error};
use yew::prelude::*;
-#[derive(Debug)]
-pub enum Msg {
- Bid(Bid),
-}
-
-pub struct Bidding {
- bidding: BiddingResult,
-}
-#[derive(PartialEq, Properties)]
+#[derive(PartialEq, Properties, Clone)]
pub struct BiddingProperties {
pub dealer: Player,
+ pub on_contract: Callback<(Contract, bridge_engine::Bidding)>,
}
-impl Component for Bidding {
- type Message = Msg;
- type Properties = BiddingProperties;
+#[function_component(Bidding)]
+pub fn bidding(props: &BiddingProperties) -> Html {
+ let bidding = use_state(|| bridge_engine::Bidding::new(props.dealer));
- fn create(ctx: &Context<Self>) -> Self {
- Self {
- bidding: BiddingResult::new(ctx.props().dealer),
- }
+ {
+ let bidding = bidding.clone();
+ let dealer = props.dealer.clone();
+ use_effect_with_deps(
+ move |_| {
+ bidding.set(bridge_engine::Bidding::new(dealer));
+ || ()
+ },
+ dealer,
+ );
}
- fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
- match msg {
- Msg::Bid(bid) => {
- self.bidding = self.bidding.bidding().clone().bid(bid).unwrap();
- true
+ let on_bid = {
+ let bidding = bidding.clone();
+ let on_contract = props.on_contract.clone();
+ Callback::from(move |bid| {
+ match (*bidding).clone().bid(bid) {
+ Ok(BiddingResult::Contract(contract, bidding)) => {
+ on_contract.emit((contract, bidding));
+ },
+ Ok(BiddingResult::InProgress(new_bidding)) => {
+ bidding.set(new_bidding);
+ }
+ Err(err) => {
+ error!("Failed to place bid: {:?}", err);
+ }
}
- }
- }
+ })
+ };
- fn view(&self, ctx: &Context<Self>) -> Html {
- html! {
- <>
- <p>{ "Bidding table" }</p>
- <BiddingTable bidding={ self.bidding.bidding().clone() } />
- <p>{ "Bidding box" }</p>
- <BiddingBox
- current_bid={ self.bidding.bidding().highest_bid().clone() }
- on_bid={ ctx.link().callback(|bid| Msg::Bid(bid)) } />
- </>
- }
+ html! {
+ <>
+ <p>{ "Bidding table" }</p>
+ <BiddingTable bidding={ (*bidding).clone() } />
+ <p>{ "Bidding box" }</p>
+ <BiddingBox
+ current_bid={ bidding.highest_bid().clone() }
+ { on_bid }
+ />
+ </>
}
}