summaryrefslogtreecommitdiff
path: root/webapp/src/components/bidding.rs
blob: e5950f67fb7cc9b5f52cd1f12fce67ddb591750e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::components::{BiddingBox, BiddingTable};
use log::error;
use protocol::{bridge_engine::{self, BiddingResult}, core::Player, contract::Contract};
use yew::prelude::*;

#[derive(PartialEq, Properties, Clone)]
pub struct BiddingProperties {
    pub dealer: Player,
    pub on_contract: Callback<(Option<Contract>, bridge_engine::Bidding)>,
}

#[function_component(Bidding)]
pub fn bidding(props: &BiddingProperties) -> Html {
    let bidding = use_state(|| bridge_engine::Bidding::new(props.dealer));

    {
        let bidding = bidding.clone();
        let dealer = props.dealer;
        use_effect_with_deps(
            move |_| {
                bidding.set(bridge_engine::Bidding::new(dealer));
                || ()
            },
            dealer,
        );
    }

    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);
            }
        })
    };

    html! {
        <>
            <p>{ "Bidding box" }</p>
            <BiddingBox
             current_bid={ bidding.highest_bid() }
             { on_bid }
            />
            <p>{ "Bidding table" }</p>
            <BiddingTable bidding={ (*bidding).clone() } />
        </>
    }
}