summaryrefslogtreecommitdiff
path: root/webapp/src/components/table.rs
blob: a874a93ac4a3ad2c7615525cf2ed2b60a06e77f3 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use gloo_net::http::Request;
use log::info;
use protocol::bridge_engine::GameStatePlayerView;
use yew::prelude::*;
use crate::use_app_context;
use crate::components::Hand;
use crate::utils::ok_json;
use anyhow::Context;

#[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 = table_state.clone();
        let props = props.clone();
        let ctx = ctx.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));
                    Ok(())
                });
                || ()
            },
            (),
        );
    }

    let leave_table = {
        let ctx = ctx.clone();
        Callback::from(move |_| {
            ctx.leave_table();
        })
    };

    html! {
    <>
        <p>{ format!("This is table {}", props.table.id) }</p>
        <button onclick={leave_table}>
            { "Leave table" }
        </button>
        if let Some(table_state) = &*table_state {
            <Table table={ table_state.clone() }/>
        }
    </>
    }
}

#[derive(PartialEq, Properties, Clone)]
pub struct OnlineTableProps {
    pub table: protocol::Table,
}

#[function_component(Table)]
pub fn table(props: &TableProps) -> Html {
    let on_card_clicked = {
        Callback::from(move |card| {
            info!("Card clicked: {}", card);
        })
    };
    html! {
        <>
            <div class="hand south">
                <Hand cards={ props.table.hand().clone() } on_card_clicked={ on_card_clicked.clone() } />
            </div>
            <h2>{ "Table view" }</h2>
            <pre>{ format!("{:#?}", props.table) }</pre>
        </>
    }
}

#[derive(PartialEq, Properties, Clone)]
pub struct TableProps {
    pub table: GameStatePlayerView,
}